Angular 2 AOT构建内联和文件

时间:2016-12-02 21:45:05

标签: angular gulp rollupjs angular2-aot

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

我通过aot编译器CLI和汇总CLI获得了构建工作,并且我得到了我期待的输出。 我的bundle.js文件是~2k。,并且附带了源地图文件。 YAY!

接下来,我尝试将汇总合并到我现有的gulp构建中。除了之外,所有内容都在中,源地图在内容中内联,使其超过2MB。此外,还会生成源地图文件。当我手动删除内联源地图时,我的包大小下降到~2k。

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

我尝试过的事情:搜索SO,搜索Angular2 docsrollup-stream docsgulp-sourcemap docs。我还没找到任何具体解决这个问题的东西。

以下是相关的配置文件

特定于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

gulp文件

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']);

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

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);
    });
});

1 个答案:

答案 0 :(得分:1)

我认为您只需更改tsconfig.json中的"sourceMap": false,如下所示:

{
  "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
  }
}