Книга: Angular 2 Cookbook
Назад: Migrating the minimum viable application to Webpack bundling
Дальше: HTML generation with html-webpack-plugin

Incorporating shims and polyfills into Webpack

So far, this has been a much cleaner implementation, but you still have the two dangling shims inside the index.html file. You've pared down index.html such that it is now requesting only a handful of JS files instead of each module target individually, but you can go even further and bundle all the JS files into a single file.

The challenge in this is that browser shims aren't delivered via modules; in other words, there aren't any other files that will import these to use them. They just assume their use is available. Therefore, the standard Webpack bundling won't pick up these targets and include them in the bundled file.

Note

The code, links, and a live example of this are available at .

Getting ready

You should complete the Migrating the minimum viable application to Webpack bundling recipe first, which will give you all the source files needed for this recipe.

How to do it...

There are a number of ways to go about doing this, including some that involve the addition of Webpack plugins, but there's an extremely simple way as well: just add the imports manually.

Create a new polyfills.ts:

[src/polyfills.ts]      import "reflect-metadata";   import "zone.js";   

Import this module from main.ts:

[src/main.ts]      import './polyfills';   import {platformBrowserDynamic}      from '@angular/platform-browser-dynamic';   import {AppModule} from './app/app.module';      platformBrowserDynamic().bootstrapModule(AppModule);   

Finally, clean up index.html:

[index.html]      <html>   <head>     <title>Angular 2 Minimum Viable Application</title>   <body>     <app-root></app-root>     <script src="dist/bundle.js"></script>   </body>   </html>   

Now, Webpack should be able to resolve the shim imports, and all the needed files will be included inside bundle.js.

How it works...

The only reason that the polyfills are not discovered by Webpack is because they are not required anywhere in the application. Rather, anywhere they are used leads to the assumption that the exposed targets, such as Zone, have previously been made available. Therefore, it is easy for you to simply import them at the very top of your application, which has a well-defined point in the code. With this Webpack, you will be able to discover the existence of polyfills and incorporate them into the generated bundle.

See also

  • Migrating the minimum viable Angular 2 application to Webpack bundling describes how to integrate Webpack into your Angular application build process
  • HTML generation with html-webpack-plugin shows you how you can configure an npm package to add compiled files to your HTML automatically
Назад: Migrating the minimum viable application to Webpack bundling
Дальше: HTML generation with html-webpack-plugin

thank you
Flame
cant read the code since it is all on a single line. Also this comments section is russian
Rakuneque
DATA COLLECTION AND ANALYSIS Two reviewers extracted data and assessed methodological quality independently lasix torsemide conversion Many others were in that space already