【问题标题】:AOT & Roll-Up: Only bundles main.js and nothing elseAOT & Roll-Up:只捆绑 main.js,没有别的
【发布时间】:2017-01-30 03:18:48
【问题描述】:

我正在尝试按照https://angular.io/docs/ts/latest/cookbook/aot-compiler.html#!#tree-shaking 实施 AOT 和汇总

我运行汇总 per:"node_modules/.bin/rollup" -c scripts/rollup-config.js

结果是一个 build.js 文件,其中只有入口文件,没有其他内容。没有错误,只有一个警告:“'default' 是从外部模块 'rollup' 导入的,但从未使用过”

“aot”文件夹确实包含所有相关的已编译 ngfactory 文件。

以下是入口文件:

import { platformBrowser }    from '@angular/platform-browser';
import { AppModuleNgFactory } from '../aot/app/app.module.ngfactory';

platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);

rollup-js.config:

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

export default {
    entry: 'scripts/app/main.js',
    dest: 'scripts/app/build.js', // output a single application bundle
    sourceMap: true,
    sourceMapFile: 'scripts/app/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()
    ]
}

还有我的 systemjs 以防万一:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'libs/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'approot',
            appjit: 'approot',
            // 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',
            'jquery': 'npm:jquery/dist/jquery.min.js',
            'moment': 'npm:moment/moment.js',
            'ng2-bootstrap/ng2-bootstrap': 'npm:ng2-bootstrap/bundles/ng2-bootstrap.umd.js',
            'ng2-select/ng2-select': 'npm:ng2-select/ng2-select.js',
            "ng2-popover": "libs/ng2-popover",
            'angular2-ui-switch': 'libs/angular2-ui-switch/dist/index.js',
            "angular2-text-mask": "libs/angular2-text-mask/dist",
            "text-mask-core": "libs/text-mask-core/"
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './app/main.js',
                defaultExtension: 'js'
            },
            appjit: {
                main: './app/main-jit.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            },
            'libs/ng2-select': {
                defaultExtension: 'js'
            },
            'libs/ng2-popover': {
                main: "index.js",
                defaultExtension: 'js'
            },
            'libs/angular2-text-mask/dist': {
                main: "angular2TextMask.js",
                defaultExtension: 'js'
            },
            'libs/text-mask-core/': {
                main: "textMaskCore.js",
                defaultExtension: 'js'
            },
            'libs/angular2-ui-switch': {
                main: "index.js",
                defaultExtension: 'js'
            }
        }
    });
})(this);

我的文件夹结构如下:

  • [Visual Studio 项目]/scripts/aot
  • [Visual Studio 项目]/scripts/app
  • [Visual Studio 项目]/scripts/app/main.js
  • [Visual Studio 项目]/scripts/rollup-config.js
  • [Visual Studio 项目]/scripts/tsconfig-aot.json

我的环境:

  • VS 2015
  • 角度:2.4.0
  • 节点:v5.5.0
  • 打字稿:2.0.10
  • 汇总:0.41.4
  • 汇总插件节点解析:2.0.0
  • rollup-plugin-commonjs: 7.0.0
  • rollup-plugin-uglify:1.0.1

【问题讨论】:

  • 问题解决了吗?
  • 抱歉,没有解决。公司让我休息一下,没有时间测试给出的解决方案。
  • 啊,好吧:/我也遇到了类似的问题。。非常努力地编写“aot友好”的角度代码:-)
  • 好吧,忽略我之前的评论。对于任何未使用 es2015 编译的外部脚本,您可以将它们添加到汇总的 commonjs 包含数组中,如下所示: commonjs({ include: ['../node_modules/rxjs/**', '../node_modules/angular2-text-掩码/**' ] }),

标签: angular typescript rollup angular2-aot


【解决方案1】:

在您的 [visual studio 项目]/scripts/aot 文件夹中,应该有一个 main.ts 文件,Visual Studio 将使用 tsconfig.json 而不是 tsconfig-aot.json 将其编译为 main.js,检查你的 main.js 文件,如果它看起来像

"use strict";
 var platform_browser_1 = require("@angular/platform-browser");
 var app_module_ngfactory_1 = require("./app.module.ngfactory");
 platform_browser_1.platformBrowser().bootstrapModuleFactory(app_module_ngfactory_1.AppModuleNgFactory);

您需要在 tsconfig.json 中更新:

    {
     "module": "commonjs",
    }

   {
     "module": "es2015",
   }

【讨论】:

  • 这为我解决了这个问题。当我遵循食谱时,我错过了将 tsconfig-aot.json 中的模块从“commonjs”更改为“es2015”。
  • 我的 main.js 文件使用的是 es2015 标准,仍然有同样的错误。
  • 我将把它标记为答案,因为它有点相关。不仅 main.js,而且 AOT 中的所有外部/内部文件都需要 es2015 投诉。
【解决方案2】:

我自己也在关注官方的 angular2 AOT guilde,由于缺乏简单的工作示例,我创建了这个 github repository。希望对你有帮助。

如果你是 windows 用户,你肯定可以在git-bash 终端上运行它(经我测试)。

【讨论】:

    【解决方案3】:

    要使 AOT 正常工作,您需要确保生成的是 es6 模块加载语法:

    Rollup 只能 Tree Shake 具有导入和导出语句的 ES2015 模块。

    tsconfig.json:

    {
        "compilerOptions" {
            "module": "es6",
             ...
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 1970-01-01
      • 2021-10-19
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      相关资源
      最近更新 更多