Angular 4 AOT构建不与Lodash Library一起使用

时间:2017-04-05 13:20:22

标签: angular systemjs angular2-aot aot

Angular 4 AOT版本无法与Lodash Library一起使用。

我设法让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("'此'关键字相当于' undefined'")> -1){return;

来自:

if(warning.indexOf("'此'关键字相当于' undefined'")> -1){return;

结果:

这似乎可以很好地运行汇总,但是当我执行" npm运行服务:aot "我在build.js文件中遇到了以下控制台错误:

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

这可能与添加的lodash lib有关吗?

这是我的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>

注意:

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

我正在为此版本使用angular.io快速入门种子应用:https://angular.io/docs/ts/latest/guide/setup.html

在运行 npm run build:aot 的情况下抓取git错误,将lodash设置为&#34;&#39; lodash&#39;:&#39; npm:lodash / lodash / core的.js&#34;在systemjs.config.js文件中:

enter image description here

2 个答案:

答案 0 :(得分:1)

在systemjs中修改此行

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

或者你可以使用lodash cdn,这使得工作变得轻松

答案 1 :(得分:0)

前一段时间我有一个类似的问题,我正在导入一些库,所以在绝望的尝试中我删除了onWarn函数:

onwarn:function(警告){

// 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编译没有错误,目前生产工作正常,现在我知道这不是一个推荐的方法由于各种原因但没有伤害没有犯规我想如果这有帮助我告诉我。如果你仍然陷入困境,我总是可以在当地实施一些东西。