You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If your Svelte components contain `<style>` tags, by default the compiler will add JavaScript that injects those styles into the page when the component is rendered. That's not ideal, because it adds weight to your JavaScript, prevents styles from being fetched in parallel with your code, and can even cause CSP violations.
56
56
57
-
A better option is to extract the CSS into a separate file. Using the `emitCss` option as shown below would cause a virtual CSS file to be emitted for each Svelte component. The resulting file is then imported by the component, thus following the standard Webpack compilation flow. Add [ExtractTextPlugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) to the mix to output the css to a separate file.
57
+
A better option is to extract the CSS into a separate file. Using the `emitCss` option as shown below would cause a virtual CSS file to be emitted for each Svelte component. The resulting file is then imported by the component, thus following the standard Webpack compilation flow. Add [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to the mix to output the css to a separate file.
@@ -73,22 +75,33 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
73
75
},
74
76
{
75
77
test:/\.css$/,
76
-
use:ExtractTextPlugin.extract({
77
-
fallback:'style-loader',
78
-
use:'css-loader',
79
-
}),
78
+
use: [
79
+
dev ?'style-loader':MiniCssExtractPlugin.loader,
80
+
{
81
+
loader:'css-loader',
82
+
options: {
83
+
url:false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
84
+
}
85
+
}
86
+
]
80
87
},
81
88
...
82
89
]
83
90
},
84
91
...
85
92
plugins: [
86
-
newExtractTextPlugin('styles.css'),
93
+
newMiniCssExtractPlugin('styles.css'),
87
94
...
88
95
]
89
96
...
90
97
```
91
98
99
+
Note that the configuration shown above switches off `MiniCssExtractPlugin` in development mode in favour of using CSS javascript injection. This is recommended by `MiniCssExtractPlugin` because it does not support hot reloading. `dev` is some boolean flag indicating that the build is development.
100
+
101
+
Additionally, if you're using multiple entrypoints, you may wish to change `new MiniCssExtractPlugin('styles.css')` for `new MiniCssExtractPlugin('[name].css')` to generate one CSS file per entrypoint.
102
+
103
+
Warning, in production, if you have set `sideEffects: false` in your `package.json`, `MiniCssExtractPlugin` has a tendency to drop CSS, regardless of if it's included in your svelte components.
104
+
92
105
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use `css: false`.
0 commit comments