0

It's my first application with Angular 2.

We run aot and rollup in order to generate a bundle. But we must always add polyfills (shim, reflect-metadata and zone.js) to the index.html with script HTML element.

Is it possible to add this polyfills to the bundle?

Another question: How add external JS libraries to the bundle. Actually, like polyfills we must add them to the index.html

lecogiteur
  • 307
  • 1
  • 7
  • 16

3 Answers3

1

You can create a separate bundle for globals and polyfills with something like gulp or webpack and include it your index.html. e.g. with gulp you could do

let globalJs = gulp.series(
    function cleanGlobalJs() {
        return del('dist/global*.js')
    },
    function BuildGlobalJs() {

        return gulp.src([
            'node_modules/core-js/client/shim.min.js',
            'node_modules/zone.js/dist/zone.min.js'
        ])
            .pipe(concat('global.js'))
            .pipe(rev())
            .pipe(gulp.dest('dist'));
    }
);

This is taken from an angular build I setup with rollup here https://github.com/robianmcd/hello-angular-rollup/blob/master/gulpfile.js#L125-L139

If you really wanted one bundle you could just concatenate this bundle with the one from rollup.

rob
  • 17,995
  • 12
  • 69
  • 94
0

I use gulp this way:

gulp.task('bundle:vendor', function() {
    return gulp
        .src([
            "node_modules/core-js/client/shim.min.js",
            "node_modules/zone.js/dist/zone.js"
        ])
        .pipe(concat("vendor.bundle.js"))
        .pipe(gulp.dest('dist'));

});
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
0

Add the rollup-plugin-node-resolve plugin, and you'll be able to bundle any dependencies that live in your node_modules folder including polyfills. You may also need to include rollup-plugin-commonjs.

Rich Harris
  • 28,091
  • 3
  • 84
  • 99