How to build angular2 app for production

柔情痞子 提交于 2020-01-03 05:38:08

问题


I've complete the angular2 tour of heroes. And I wonder what if I'd like to build this project for production. I want to add only 2 files to index.html: appScripts.js and vendorScripts.js. I've compiled my ts files with option outFile so I have my appScripts.js but it doesn't work. Here is some code in my gulpfile:

gulp.task('compile', ['clean'], function() {

    var tsProject = typescript.createProject('tsconfig.json', {
        outFile: 'app.js'
    });

    var tsResult = gulp.src(['app/**/*.ts'])
        .pipe(typescript(tsProject));
    return tsResult.js
        .pipe(gulp.dest('./dist'));
});

I think it's because, we have loaded some angular models like @angular/core in project ts files, and because of some systemjs stuff..

Is there any solutions?


回答1:


I've found out the solution. You should compile your ts files to js files and then bundle them via systemjs-builder. Here's my gulp task:

var Builder  = require("systemjs-builder");
gulp.task("builder", function() {

        var builder = new Builder(); 

        builder.loadConfig('./systemjs.config.js')
            .then(function() {
                builder.buildStatic('dist/main.js', 'public/appScript.js')
                    .then(function () {
                        console.log('Build complete');
                    })
                    .catch(error);
            });
});

buildStatic method creates the final javascript file for your application.



来源:https://stackoverflow.com/questions/37254002/how-to-build-angular2-app-for-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!