Skip to content

Commit b0dc87b

Browse files
committed
Updated readme to reflect the new minicss plugin
1 parent d4e2c20 commit b0dc87b

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
@@ -54,9 +54,11 @@ Webpack's [`resolve.mainFields`](https://webpack.js.org/configuration/resolve/#r
5454

5555
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.
5656

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

5959
```javascript
60+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
61+
const dev = mode == 'development';
6062
...
6163
module: {
6264
rules: [
@@ -73,22 +75,33 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
7375
},
7476
{
7577
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+
]
8087
},
8188
...
8289
]
8390
},
8491
...
8592
plugins: [
86-
new ExtractTextPlugin('styles.css'),
93+
new MiniCssExtractPlugin('styles.css'),
8794
...
8895
]
8996
...
9097
```
9198

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+
92105
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`.
93106

94107
### Source maps

0 commit comments

Comments
 (0)