• embedded stylesheets
    • installation
    • configuration
    • using css
  • separate css bundle
    • plugin installation
    • general
    • styles from initial chunks into separate css output file
    • all styles in separate css output file
    • styles in commons chunk

    embedded stylesheets

    Through using the style-loader and the css-loader it’s possible to embed stylesheets into a webpack javascript bundle. This way you can modularize your stylesheets with your other modules. This way stylesheets are as easy as require("./stylesheet.css").

    installation

    Install the loaders from npm.

    1. npm install style-loader css-loader --save-dev

    configuration

    Here is a configuration example that enables require() css:

    1. {
    2. // ...
    3. module: {
    4. loaders: [
    5. { test: /\.css$/, loader: "style-loader!css-loader" }
    6. ]
    7. }
    8. }

    For compile-to-css languages see the according loaders for configuration examples. You can pipe them…

    Keep in mind that it’s difficult to manage the execution order of modules, so design your stylesheets so that order doesn’t matter. (But you can rely on the order in one css file.)

    using css

    1. // in your modules just require the stylesheet
    2. // This has the side effect that a <style>-tag is added to the DOM.
    3. require("./stylesheet.css");

    separate css bundle

    In combination with the extract-text-webpack-plugin it’s possible to generate a native css output file.

    With Code Splitting we can use two different modes:

    • Create one css file per initial chunk (see [[Code Splitting]]) and embed stylesheets into additional chunks. (recommended)
    • Create one css file per initial chunk which also contains styles from additional chunks.

    The first mode is recommended because it’s optimal in regards to initial page loading time. In small apps with multiple entry points the second mode could be better because of HTTP request overheads and caching.

    plugin installation

    Install the plugin from npm.

    1. npm install extract-text-webpack-plugin --save

    general

    To use the plugin you need to flag modules that should be moved into the css file with a special loader. After the compilation in the optimizing phase of webpack the plugin checks which modules are relevant for extraction (in the first mode only these that are in an initial chunk). These modules are compiled for node.js usage and executed to get the content. Additionally the modules are recompiled in the original bundle and replaced with an empty module.

    A new asset is created for the extracted modules.

    styles from initial chunks into separate css output file

    This examples shows multiple entry points, but also works with a single entry point.

    1. // webpack.config.js
    2. var ExtractTextPlugin = require("extract-text-webpack-plugin");
    3. module.exports = {
    4. // The standard entry point and output config
    5. entry: {
    6. posts: "./posts",
    7. post: "./post",
    8. about: "./about"
    9. },
    10. output: {
    11. filename: "[name].js",
    12. chunkFilename: "[id].js"
    13. },
    14. module: {
    15. loaders: [
    16. // Extract css files
    17. {
    18. test: /\.css$/,
    19. loader: ExtractTextPlugin.extract("style-loader", "css-loader")
    20. },
    21. // Optionally extract less files
    22. // or any other compile-to-css language
    23. {
    24. test: /\.less$/,
    25. loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
    26. }
    27. // You could also use other loaders the same way. I. e. the autoprefixer-loader
    28. ]
    29. },
    30. // Use the plugin to specify the resulting filename (and add needed behavior to the compiler)
    31. plugins: [
    32. new ExtractTextPlugin("[name].css")
    33. ]
    34. }

    You’ll get these output files:

    • posts.js posts.css
    • post.js post.css
    • about.js about.css
    • 1.js 2.js (contain embedded styles)

    all styles in separate css output file

    To use the second mode you just need to set the option allChunks to true:

    1. // ...
    2. module.exports = {
    3. // ...
    4. plugins: [
    5. new ExtractTextPlugin("style.css", {
    6. allChunks: true
    7. })
    8. ]
    9. }

    You’ll get these output files:

    • posts.js posts.css
    • post.js post.css
    • about.js about.css
    • 1.js 2.js (don’t contain embedded styles)

    styles in commons chunk

    You can use a separate css file in combination with the CommonsChunkPlugin. In this case a css file for the commons chunk is emitted too.

    1. // ...
    2. module.exports = {
    3. // ...
    4. plugins: [
    5. new webpack.optimize.CommonsChunkPlugin("commons", "commons.js"),
    6. new ExtractTextPlugin("[name].css")
    7. ]
    8. }

    You’ll get these output files:

    • commons.js commons.css
    • posts.js posts.css
    • post.js post.css
    • about.js about.css
    • 1.js 2.js (contain embedded styles)

    or with allChunks: true

    • 1.js 2.js (don’t contain embedded styles)