網站搜尋

angular - AOT 與 Roll-Up:只打包 main.js,其他不打包


我正在嘗試按照 https://angular.io/docs/ts/latest/cookbook/aot-compiler.html#!#tree-shaking 實現 AOT 和 Rollup

我執行 rollup 使用:"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
  • Angular:2.4.0
  • 節點:v5.5.0
  • Typescript:2.0.10
  • rollup: 0.41.4
  • rollup-plugin-node-resolve: 2.0.0
  • rollup 插件-commonjs:7.0.0
  • rollup-plugin-uglify: 1.0.1

答案#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",
   }

答案#2

我自己也在跟隨官方的 Angular2 AOT 指南,因為缺乏一個簡單可用的範例,我創建了這個 GitHub 儲存庫。我希望它能幫助你。

如果你是 Windows 使用者,你一定能在 git-bash 終端機中讓它運作(由我測試過)。