Skip to content

Commit a78d75c

Browse files
committed
Merge remote-tracking branch 'nergmada/master' into refactor
2 parents 16ed5c1 + b0dc87b commit a78d75c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ Webpack's [`resolve.mainFields`](https://webpack.js.org/configuration/resolve/#r
6161

6262
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.
6363

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.
6565

6666
```javascript
67+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
68+
const dev = mode == 'development';
6769
...
6870
module: {
6971
rules: [
@@ -80,22 +82,33 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
8082
},
8183
{
8284
test: /\.css$/,
83-
use: ExtractTextPlugin.extract({
84-
fallback: 'style-loader',
85-
use: 'css-loader',
86-
}),
85+
use: [
86+
dev ? 'style-loader' : MiniCssExtractPlugin.loader,
87+
{
88+
loader: 'css-loader',
89+
options: {
90+
url: false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
91+
}
92+
}
93+
]
8794
},
8895
...
8996
]
9097
},
9198
...
9299
plugins: [
93-
new ExtractTextPlugin('styles.css'),
100+
new MiniCssExtractPlugin('styles.css'),
94101
...
95102
]
96103
...
97104
```
98105

106+
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.
107+
108+
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.
109+
110+
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.
111+
99112
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`.
100113

101114
### Source maps

0 commit comments

Comments
 (0)