1

我正在为 Angular 2 应用程序进行生产构建,并尝试使用 Angular 2 AoT 构建过程来减小包大小(在此处记录)

我通过 aot 编译器 CLI 和汇总 CLI 使构建工作,并且得到了我期望的输出。我的 bundle.js 文件约为 2k。并且有一个随附的源映射文件。耶!

接下来,我尝试将汇总合并到我现有的 gulp 构建中。一切正常,只是源映射在捆绑包中内联,使其超过 2MB。此外,还会生成一个源映射文件。当我手动删除内联源映射时,我的包大小下降到 ~2k。

我的问题是如何在没有内联源映射的情况下生成我的 bundle.js?

我尝试过的事情:搜索 SO、搜索Angular2 文档汇总流文档gulp-sourcemap 文档。我还没有找到任何专门解决这个问题的东西。

下面是相关的配置文件

AoT 特定的 tsconfig 文件(tsconfig-aot.json):

{
  "compilerOptions": {
   "target": "es5",
   "module": "es2015",
   "moduleResolution": "node",
   "sourceMap": true,
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
   "lib": [ "es2015", "dom" ],
   "removeComments": false,
   "noImplicitAny": true,
   "suppressImplicitAnyIndexErrors": true,
   "typeRoots": [
      "node_modules/@types/"
    ]
  },
  "include": [
    "app/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ],

  "angularCompilerOptions": {
    "genDir": "aot",
    "skipMetadataEmit": true
  }
}

汇总配置(我尝试注释掉 dest 和 sourceMapFile 文件路径以查看是否会导致 gulp 关闭,但没有效果):

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

let config = {
    entry: 'app/app-aot.js',
    dest: 'wwwroot/dist/bundle.js', // output a single application bundle
    sourceMap: true,
    sourceMapFile: 'wwwroot/dist/bundle.js.map',
    format: 'iife',
    plugins: [
        nodeResolve({jsnext: true, module: true}),
        commonjs({
            include: ['node_modules/rxjs/**']
        }),
        uglify()
    ]
}

//paths are relative to the execution path
export default config

吞咽文件

var gulp = require('gulp');
var del = require('del');
var helpers = require('./config/helpers');
var exec = require('child_process').exec;
var merge = require('merge-stream');
var rename = require('gulp-rename');
var rollup = require('rollup-stream');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var sourcemap = require('gulp-sourcemaps');

var paths = {
    app: helpers.root('app/**/*'),
    bootstrap: helpers.root('app/assets/bootstrap/'),
    images: helpers.root('app/assets/images/')
};

gulp.task('clean', function () {
    return del(['wwwroot/**/*']);
});

gulp.task('clean-aot', ['clean'], function() {
    return del(['aot/**/*']);
});

gulp.task('bundle-aot', ['clean', 'clean-aot'], function(callBack) {
    exec('\"node_modules/.bin/ngc\" -p tsconfig-aot.json', function(err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        callBack(err);
    });
});

gulp.task('bundle-rollup', ['bundle-aot'], function() {
    return rollup('rollup-config.js')
        .pipe(source('bundle.js'))
        .pipe(buffer())
        .pipe(sourcemap.init({ loadMaps: true }))
        .pipe(sourcemap.write('.'))
        .pipe(gulp.dest('wwwroot/dist'));
});

gulp.task('bundle-copy-files', ['bundle-rollup'], function() {
    var bsCss = gulp.src(paths.bootstrap + '/css/*.min.css').pipe(gulp.dest('wwwroot/assets/bootstrap/css/'));
    var bsFonts = gulp.src(paths.bootstrap + '/fonts/*').pipe(gulp.dest('wwwroot/assets/bootstrap/fonts/'));
    var images = gulp.src(paths.images + '*').pipe(gulp.dest('wwwroot/assets/images/'));
    var shim = gulp.src('node_modules/core-js/client/shim.min.js').pipe(gulp.dest('wwwroot/'));
    var zone = gulp.src('node_modules/zone.js/dist/zone.min.js').pipe(gulp.dest('wwwroot/'));
    var index = gulp.src('app/index-aot.html').pipe(rename('index.html')).pipe(gulp.dest('wwwroot/'));

    return merge(bsCss, bsFonts, images, shim, zone, index);
});

gulp.task('bundle', ['clean', 'clean-aot', 'bundle-aot', 'bundle-rollup', 'bundle-copy-files']);

编辑:我确实找到了一个解决方法,让 gulp 使用 CLI 执行汇总。不过,我仍然想知道我在 gulp 或 rollup 配置中做错了什么。

gulp.task('bundle-rollup', ['bundle-aot'], function(callBack) {

    exec('\"node_modules/.bin/rollup\" -c rollup-config.js', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        callBack(err);
    });
});
4

1 回答 1

1

我认为您只需要像这样更改"sourceMap": falsetsconfig.json 中的内容:

{
  "compilerOptions": {
   "target": "es5",
   "module": "es2015",
   "moduleResolution": "node",
   "sourceMap": false,          /// <<< This is the change you need
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
   "lib": [ "es2015", "dom" ],
   "removeComments": false,
   "noImplicitAny": true,
   "suppressImplicitAnyIndexErrors": true,
   "typeRoots": [
      "node_modules/@types/"
    ]
  },
  "include": [
    "app/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ],

  "angularCompilerOptions": {
    "genDir": "aot",
    "skipMetadataEmit": true
  }
}
于 2017-11-03T07:04:03.890 回答