【问题标题】:Bundle Angular2 AoT with systemjs-builder and rollup tree shaking将 Angular2 AoT 与 systemjs-builder 和汇总树抖动捆绑在一起
【发布时间】:2017-02-04 04:01:10
【问题描述】:

我花了很多时间让我的 Angular 2 项目使用 SystemJS,现在使用 AoT 编译,如下所述:https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

我正在使用 SystemJS 运行 plugin-typescript 以在开发期间在浏览器中转换 typescript。我有 systemjs builder 使用 plugin-typescript 从 AoT (ngc) 输出中生成 sfx 包。

我想不通的是如何让 systemjs-builder 对 Angular AoT 代码进行树摇动。据我所知,systemjs-builder 似乎应该支持汇总(例如https://github.com/systemjs/builder/issues/709),但是当我在 buildStatic 选项中设置 rollup:true 时,我认为汇总不会为我运行。

我的typescriptOptions来自systemjs.config.js

transpiler: 'ts',
typescriptOptions: {
  target: 'es5',
  module: 'es6',
  emitDecoratorMetadata: true,
  experimentalDecorators: true
}

我的buildStatic 电话:

 builder.buildStatic('app/main-aot.ts', paths.wwwroot + 'dst/build-aot.sfx.min.js', {
            minify: true,
            rollup: true
        }).then(function (output) {
            console.log(output.inlineMap);
        }, function (err) {
            console.log(err);
        }),

我从这个 github 票中得到了关于 output.inlineMap 的文章:https://github.com/systemjs/builder/issues/639

但我不清楚我应该期待什么。现在它只是输出{}

这对我来说真的很新鲜,我可能完全误解了一些东西。寻找任何人来帮助我确定汇总是否正在运行,确认它应该是可能的,如果是的话,希望能解决这个问题。

【问题讨论】:

  • 我自己也遇到过同样的问题。并且想知道您是否找到了原始问题的答案。
  • @N.Schipper 我发布了一个解决方案,经过大量试验和错误后,我找到了该解决方案。不幸的是,我从来没有遇到过对 inlineMap 内容的深入定义,但我能够很好地满足我自己的问题,然后继续前进。

标签: angular typescript systemjs rollup systemjs-builder


【解决方案1】:

我能够看出,当 rollup 正确执行时,构建器结果上的 inlineMap 属性将是数组的名称值集合:

{ "key1": [], "key2": [] ... }

每个键都将输入文件的名称反映到buildStatic。我不使用任何 glob 模式,我通过为我的 Angular 应用程序提供单个入口点(主)文件来运行 buildStatic,因此我在地图中获得了一个键。

builder.buildStatic("main.ts", "out.sfx.min.js").then((output) => {
   console.log(output.inlineMap); // => { "main.ts": [...] }
});

数组中映射到键的项目数我解释为在汇总过程中进行的优化数...我不确定这在技术上是否 100% 准确,但我在我的建立输出以确信汇总过程确实做了一些事情 - 数字越大越好。

为了后代 - 我使用以下 gulp 函数来构建静态和漂亮的打印结果...

const builder = require("systemjs-builder");
const filesize = require("gulp-check-filesize");

let opts = { runtime: false, minify: true, mangle: true, rollup: true }
let inFile = "main.ts";
let outFile = "main.sfx.min.js";
builder.buildStatic(infile, outFile, opts).then((output) => {
    output = output || {};

    console.log(" ");
    console.log(`Build Summary: ${inFile.toLowerCase()}`);
    console.log("---------------------------");
    return new Promise((resolve, reject) => {
        // prints output file name, size and gzip size.
        gulp.src(outFile).pipe(filesize({ enableGzip: true }))
            .on("error", reject)
            .on("end", () => {
                // build rollup sumary.
                const map = result.inlineMap || {};
                if (Object.keys(map).length <= 0) console.log("No rollup optimizations performed.");
                Object.keys(map).forEach((key) => {
                    console.log(`Rollup '${key}': ${(map[key] && map[key].length) || 0} optimizations.`);
                });

            console.log("---------------------------");
            console.log(" ");
            resolve();
        });
    });
});

【讨论】:

  • 啊,谢谢,下周我会在工作中研究这个!
猜你喜欢
  • 2017-06-05
  • 2017-05-05
  • 1970-01-01
  • 2016-09-16
  • 2020-05-02
  • 2021-10-22
  • 2018-12-19
  • 2016-10-09
相关资源
最近更新 更多