【问题标题】:Angular 4 Ahead-of-Time - lazyload doesn't workAngular 4 Ahead-of-Time - 延迟加载不起作用
【发布时间】:2018-10-21 15:18:05
【问题描述】:

我有一个 SystemJS 项目。我使用 NGC 和 Rollup 进行 AOT 编译。一切正常,但用于路由的延迟加载不起作用。 例如,这是我的 tsconfig-aot.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "baseUrl": ".",
        "paths": {
            "app/*": [ "../src/app/*" ]
        }
  },
  "typeRoots": [
    "node_modules/@types"
  ],
  "files": [
    "../src/app/app.module.aot.ts"
  ],
  "exclude": [
    "node_modules",
    "typings"
  ],
  "angularCompilerOptions": {
    "genDir": "../",
    "skipMetadataEmit": false,
    "skipTemplateCodegen": false
  }
}

rollup.config.app.js

'use strict';

import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs    from 'rollup-plugin-commonjs';
import uglify      from 'rollup-plugin-uglify'

export default {
    entry: './src/app/app.module.aot.js',
    dest:  './src/dist/app.module.min.js',
    sourceMap: true,
    format: 'iife',
    onwarn: function(warning) {
        // Skip certain warnings
        if ( warning.message.indexOf('\'default\' is not exported by rollup') === -1) { return; }
        if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
        if ( warning.code === 'EVAL' ) { return; }
        console.warn( warning.message );
    },
    plugins: [
        nodeResolve({jsnext: true, module: true}),
        commonjs({
            include: [
                './node_modules/rxjs/**'
            ]
        }),
        uglify()
    ]
}

使用汇总运行 AOT 后一切正常,但当我尝试对我的应用使用延迟加载时,它不起作用。

const routes: Routes = [
  {
    path: "test/:id",
    loadChildren: "./src/app/app.module#TestModule"
  }
];

AOT 构建通过没有任何错误,在使用 AOT 运行应用程序后,我在开发工具中看不到任何错误。但是延迟加载也不起作用。

UPD 在 JIT 编译中,延迟加载一切正常。

知道如何解决这个问题吗?

【问题讨论】:

  • Angular AOT 不适用于Rollup。为什么不迁移到 webpack?
  • @RitwickDey 我不能在 web pack 上移动这个项目,因为它是一个非常大的带有 asp.net 的应用程序。此外,带有 Rollup 的 AOT 工作正常,但lazyload 不起作用。所有其他的东西看起来都不错。您对如何解决此问题有任何想法吗?
  • 据我所知,AOT 不适用于rollup。你可以查看这个站点dzurico.com/angular-aot-webpack-lazy-loading ......这个站点正在展示如何使用 Webpack 做同样的事情(几乎相同的配置 - 试一试)
  • @RitwickDey 有趣的是,我花了更多时间来配置 Rollup 和 AOT (NGC),它对我有用,我希望 :) 我会尝试从 System.js 转移到网络包。另外,我认为有人可以知道如何在 System.js 上做到这一点

标签: angular lazy-loading angular-routing angular2-aot rollup


【解决方案1】:

(只是从我的评论中回答,如果它有效 - 请随时投票/接受答案)

您必须使用 webpack 进行 AOT 和延迟加载。 Rollup 将不起作用(至少目前如此)。

使用@ngtools/webpack配置AOT+延迟加载。

webpack.config.js

const ngToolsWebpack = require('@ngtools/webpack');
var webpack = require('webpack');

module.exports = {
  resolve: {
    extensions: ['.ts', '.js']
  },
  entry: './app/main.aot.ts',
  output: {
    path: './dist',
    publicPath: 'dist/',
    filename: 'app.main.js'
  },
  plugins: [
    new ngToolsWebpack.AotPlugin({
      tsConfigPath: './tsconfig.json'
    }),
        new webpack.LoaderOptionsPlugin({
            minimize: true,
            debug: false
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false
            },
            output: {
                comments: false
            },
            sourceMap: true
        })
  ],
  module: {
    loaders: [
      { test: /\.scss$/, loaders: ['raw-loader', 'sass-loader'] },
      { test: /\.css$/, loader: 'raw-loader' },
      { test: /\.html$/, loader: 'raw-loader' },
      { test: /\.ts$/, loader: '@ngtools/webpack' }
    ]
  },
  devServer: {
    historyApiFallback: true
  }
};

tsconfig.json

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": true,
    "mapRoot": "",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2015",
      "dom"
    ],
    "outDir": "lib",
    "skipLibCheck": true,
    "rootDir": "."
  },
  "angularCompilerOptions": {
    "genDir": "./app/ngfactory",
    "entryModule": "app/app.module#AppModule"
  }
}

参考:http://www.dzurico.com/angular-aot-webpack-lazy-loading/

【讨论】:

  • 感谢您的回答,但我不使用 Webpack,我的应用程序使用 System.js 和 NGC+Rollup 以及下一个问题 “如何解决延迟加载问题而不继续webpack 或类似的打包模块”?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多