I use the html-webpack-plugin plugin to insert <script> tags into the template file for the pieces that webpack generates. I also have some global style sheets that I want to dynamically add to the <head> section of my HTML template. For this, I also use html-webpack-plugin as follows (in this case, the app entry / chunk is relevant).
module.exports = { entry: { 'polyfills': './ts/polyfills.ts', 'vendor': './ts/vendor.ts', 'app': './ts/app.ts' }, module: { loaders: [ { test: /\.ts$/, loaders: ['ts', 'angular2-template-loader'] }, { test: /\.html$/, loader: 'html-loader' }, { test: /\.css$/, loaders: ['to-string-loader', ExtractTextPlugin.extract('style-loader', 'css-loader')] } ] }, plugins: [ new ExtractTextPlugin('css/[name].[contenthash].css'), new webpack.ProvidePlugin({ bootstrap: 'bootstrap' }), new webpack.optimize.CommonsChunkPlugin({ name: 'app', filename: 'js/[name].[chunkhash].min.js', chunks: ['app'] }), new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'js/[name].[chunkhash].min.js', chunks: ['vendor'] }), new webpack.optimize.CommonsChunkPlugin({ name: 'polyfills', filename: 'js/[name].[chunkhash].min.js', chunks: ['polyfills'] }), new HtmlWebpackPlugin({ template: 'my-template.html', filename: 'my-template.html', inject: 'body', hash: false }), ] };
Below is the app.ts file, where I added a require statement for each stylesheet that I would like to include in my template (they are combined together into one stylesheet).
require('main.css'); require('misc.css'); import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule);
Essentially, this works well, but the trick is that I use this configuration for an Angular2 application, which consists of components that may have related stylesheets, as in this example.
@Component({ styleUrls: [ 'my-stylesheet.css' ] }) export class MyComponent { ... }
Building an application will usually result in a ton of HTTP calls for these stylesheets for the components, so to solve this problem I pulled the stylesheets directly into the components using angular2-template-loader . This bootloader works as follows.
angular2 -template-loader looks for url templates and styleUrls declarations inside the metadata of the Angular 2 component and replaces the paths with the corresponding requirement.
The problem is that in addition to the two stylesheets added to the app.ts entry app.ts , webpack also includes all the stylesheets that my Angular2 components reference. As it goes through the application dependencies and finds the require statements added by angular2 -template-loader. This is undesirable because I want these style sheets to be local to the components to which they belong, i.e. Be embedded in JS.
Therefore, I only need to include the stylesheets that I require directly in my entry points, possibly somehow excluding component stylesheets. The problem is that in both cases the file has a .css extension, so they both pass the test for extract-text-plugin . I looked at the bootloader / plugin documentation, but I could not find anything that could solve my problem.
So I want to add the app.[contenthash].css file to my template file (this part works), but excluding the stylesheets referenced by my Angular2 components (included because of angular2 -template-loader).
How can I prevent these Angular2 stylesheets from being included in the stylesheet that is added to my HTML template?