【问题标题】:How can you bundle Angular 2 using System JS Builder?如何使用 System JS Builder 捆绑 Angular 2?
【发布时间】:2016-10-05 09:09:39
【问题描述】:

我目前正在使用 System JS 和 System JS Builder 来捆绑我的应用程序、它的资产和它引用的库。我的问题是我可以捆绑在 index.html 中明确引用的库,例如:

<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

但是,我不知道如何捆绑 Angular 2 本身,或者至少是 Angular 2 所需的模块,因为它们实际上并没有在 HTML 中引用。如何做到这一点?

【问题讨论】:

    标签: angular bundling-and-minification systemjs


    【解决方案1】:

    您可以使用 Webpack 或 Rollup 之类的捆绑程序(我的偏好是因为它会摇树)。

    Angular 团队似乎正在围绕 Rollup 整合一些出色的工具,以便及时发布完整版本。今年ng-conf的day 2 keynote讨论并展示了离线编译器。 Start watching this 25:30。

    【讨论】:

    • 嗨,加里,感谢您的回复。这不是 SystemJS Bundler 应该做的吗?如果我要使用 Webpack,那么根据我的理解,我不需要使用 SystemJS Bundler。
    • 是的,不同的工具通常用于相同的工作。 systemjs builder 和 webpack 在包含 es6 模块和保持有效负载小方面都做得很好。 Rollup 是令人兴奋的 IMO,它带有 treeshaking。我在想我需要创建自定义脚本才能在我的 ng2 项目中使用汇总。当 Rob Wormald 做那个 ng-conf 演示时,我非常兴奋! Angular 2 将具有内置支持。
    • 这是一个good article,关于打包 Angular 2 应用程序。
    • 我认为systemjs-builder现在默认包含摇树,如果你看一下package.json,rollup是一个依赖
    【解决方案2】:

    使用 systemjs-builder,您可以将 Angular 2 与您的应用代码捆绑在一起,并单独捆绑您的其他库。

    我将我将直接在 HTML 中引用的任何库捆绑到 vendor.min.js 中,并将通过我的 system.config.js 引用的任何库以及应用程序代码捆绑到 app.min.js 中。在这个example中可以看到,《英雄之旅》中的所有依赖都在source code)中加载到页面中。

    // Bundle dependencies into vendors file
    gulp.task('bundle:libs', function () {
        return gulp.src([
            'node_modules/jquery/dist/jquery.min.js',
            'node_modules/bootstrap/dist/js/bootstrap.min.js',
            'node_modules/semantic-ui/dist/semantic.min.js',
            'node_modules/es6-shim/es6-shim.min.js',
            'node_modules/es6-promise/dist/es6-promise.min.js',
            'node_modules/systemjs/dist/system.src.js',
            'system.config.js',
          ])
            .pipe(concat('vendors.min.js'))
            .pipe(uglify())
            .pipe(gulp.dest('public/lib/js'));
    });
    
    // Compile TypeScript to JS
    gulp.task('compile:ts', function () {
      return gulp
        .src([
            "src/**/*.ts",
            "typings/*.d.ts"
        ])
        .pipe(sourcemaps.init())
        .pipe(tsc({
            "module": "system",
            "moduleResolution": "node",
            "outDir": "public/dist/js",
            "target": "ES5"
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('public/dist'));
    });
    
    // Generate systemjs-based builds
    gulp.task('bundle:js', function() {
      var builder = new sysBuilder('public', './system.config.js');
      return builder.buildStatic('app', 'public/dist/js/app.min.js');
    });
    
    // Minify JS bundle
    gulp.task('minify:js', function() {
      return gulp
        .src('public/dist/js/app.min.js')
        .pipe(uglify())
        .pipe(gulp.dest('public/dist/js'));
    });
    
    gulp.task('scripts', ['compile:ts', 'bundle:js', 'minify:js']);
    

    【讨论】:

    • 有趣,你能指出这个项目的 github repo 吗?
    • 为什么使用 uglify 而不是传递给buildStatic 的 systemjs minify 选项?
    • 我克隆了这个 repo 编译失败。
    • @tsuz master 分支目前已部署到 Heroku 并按原样工作,但如果您在 Windows 上运行,则存在已知问题。
    • @Steely 我刚刚克隆了 repo 并尝试构建它,但它引发了编译错误。我在 MacOSX 上
    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多