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.
63
63
64
-
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.
64
+
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.
@@ -80,23 +83,49 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
80
83
},
81
84
{
82
85
test:/\.css$/,
83
-
use:ExtractTextPlugin.extract({
84
-
fallback:'style-loader',
85
-
use:'css-loader',
86
-
}),
86
+
use: [
87
+
prod ?MiniCssExtractPlugin.loader:'style-loader',
88
+
{
89
+
loader:'css-loader',
90
+
options: {
91
+
url:false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
92
+
}
93
+
}
94
+
]
87
95
},
88
96
...
89
97
]
90
98
},
91
99
...
92
100
plugins: [
93
-
newExtractTextPlugin('styles.css'),
101
+
newMiniCssExtractPlugin('styles.css'),
94
102
...
95
103
]
96
104
...
97
105
```
98
106
99
-
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`.
107
+
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.
108
+
109
+
`prod` indicates, that `NODE_ENV=production` has been set from `package.json` or manually (`NODE_ENV=production npx webpack`) for production builds. We can rely on that to make dynamic adjustments to the config.
110
+
111
+
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.
112
+
113
+
Warning: in production, if you have set `sideEffects: false` in your `package.json`, `MiniCssExtractPlugin` has a tendency to drop CSS, regardless of whether it's included in your svelte components.
114
+
115
+
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use
If you rely on any external dependencies (files required in a preprocessor for example) you might want to watch these files for changes and re-run svelte compile.
237
-
238
-
Webpack allows [loader dependencies](https://webpack.js.org/contribute/writing-a-loader/#loader-dependencies) to trigger a recompile. svelte-loader exposes this API via `options.externalDependencies`.
0 commit comments