Angular 4 AOT 构建不适用于 Lodash 库

Angular 4 AOT build not working with Lodash Library

Angular 4 AOT 构建不适用于 Lodash 库。

我设法让 lodash 在 JIT 中构建和服务。 但是我在构建 AOT 时遇到了问题。

当我没有 lodash 时,AOT 工作正常,但我想让它与 lodash 一起工作。

这是我使用的命令:

"build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup-config.js",

错误 git bash:

warning.indexOf 不是函数

尝试解决汇总错误:

我将 rollup-config.js 文件更新为:

if ( warning.message.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return;

来自:

if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return;

结果:

这似乎 运行 汇总很好,但是当我执行“npm 运行 serve:aot”时,我得到了以下信息build.js 文件的控制台错误:

错误:未捕获(承诺):TypeError:void 0 不是函数。

这可能与添加的 lodash 库有关?

这是我的 tsconfig:

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

这是我的systemjs.config:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'lodash':                    'node_modules/lodash/lodash.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      },
      lodash: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

这是我的汇总配置:

import rollup      from 'rollup'
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs    from 'rollup-plugin-commonjs';
import uglify      from 'rollup-plugin-uglify'
//paths are relative to the execution path
export default {
  entry: 'src/main.js',
  dest: 'aot/dist/build.js', // output a single application bundle
  sourceMap: true,
  sourceMapFile: 'aot/dist/build.js.map',
  format: 'iife',
  onwarn: function(warning) {
    // Skip certain warnings
    // should intercept ... but doesn't in some rollup versions
    if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
    // intercepts in some rollup versions
    if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return; }
    // console.warn everything else
    console.warn( warning.message );
  },
  plugins: [
    nodeResolve({jsnext: true, module: true}),
    commonjs({
      include: ['node_modules/rxjs/**']
    }),
    uglify()
  ]
}

这是我的index.html:

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart - aot</title>
    <base href="/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
  </head>

  <body>
    <my-app>Loading...</my-app>
  </body>

  <script src="build.js"></script>

</html>

注:

我提到我在地图中为 systemjs.config 文件使用了 lodash。并在此文件的包部分。

我正在为此构建使用 angular.io 快速启动种子应用程序:https://angular.io/docs/ts/latest/guide/setup.html

运行ning npm 运行 build:aot 的 git 错误屏幕截图,lodash 设置为“'lodash':'npm:lodash/lodash/core.js" in systemjs.config.js 文件:

在你的 systemjs 中修改这一行

 'lodash':                    'npm:lodash/lodash/core.js'

或者你可以使用 lodash cdn,这让工作变得更容易

不久前我在导入的一些库中遇到了类似的问题,所以在绝望的尝试中我删除了 onWarn 函数:

onwarn:函数(警告){

// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
// intercepts in some rollup versions
if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return; }
// console.warn everything else
console.warn( warning.message );

}, 从 rollup-config 文件和应用程序 AOT 编译,没有错误,目前在生产中工作正常,现在我知道出于各种原因这不是推荐的方法,但没有伤害也没有犯规我想如果这有帮助请告诉我。如果您仍然卡住,我总是可以在本地实现一些东西并尝试一下。