Skip to content

Commit fae1db0

Browse files
authored
Readme updates (#174)
1 parent c6f3d41 commit fae1db0

File tree

1 file changed

+85
-12
lines changed

1 file changed

+85
-12
lines changed

README.md

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Configure inside your `webpack.config.js`:
3131
...
3232
{
3333
test: /\.(html|svelte)$/,
34-
exclude: /node_modules/,
3534
use: 'svelte-loader'
3635
},
3736
{
@@ -65,15 +64,12 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
6564

6665
```javascript
6766
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
68-
const mode = process.env.NODE_ENV || 'development';
69-
const prod = mode === 'production';
7067
...
7168
module: {
7269
rules: [
7370
...
7471
{
7572
test: /\.(html|svelte)$/,
76-
exclude: /node_modules/,
7773
use: {
7874
loader: 'svelte-loader',
7975
options: {
@@ -84,11 +80,11 @@ const prod = mode === 'production';
8480
{
8581
test: /\.css$/,
8682
use: [
87-
prod ? MiniCssExtractPlugin.loader :'style-loader',
83+
MiniCssExtractPlugin.loader,
8884
{
8985
loader: 'css-loader',
9086
options: {
91-
url: false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
87+
url: false, // necessary if you use url('/path/to/some/asset.png|jpg|gif')
9288
}
9389
}
9490
]
@@ -104,10 +100,6 @@ const prod = mode === 'production';
104100
...
105101
```
106102

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-
111103
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.
112104

113105
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.
@@ -144,7 +136,7 @@ module.exports = {
144136
{
145137
test: /\.css$/,
146138
use: [
147-
prod ? MiniCssExtractPlugin.loader :'style-loader',
139+
MiniCssExtractPlugin.loader,
148140
{
149141
loader: 'css-loader',
150142
options: {
@@ -203,7 +195,6 @@ module.exports = {
203195
...
204196
{
205197
test: /\.(html|svelte)$/,
206-
exclude: /node_modules/,
207198
use: {
208199
loader: 'svelte-loader',
209200
options: {
@@ -276,6 +267,88 @@ module.exports = {
276267
}
277268
```
278269

270+
### CSS @import in components
271+
272+
It is advised to inline any css `@import` in component's style tag before it hits `css-loader`.
273+
274+
This ensures equal css behavior when using HMR with `emitCss: false` and production.
275+
276+
Install `svelte-preprocess`, `postcss`, `postcss-import`, `postcss-load-config`.
277+
278+
Configure `svelte-preprocess`:
279+
280+
```javascript
281+
const sveltePreprocess = require('svelte-preprocess');
282+
...
283+
module.exports = {
284+
...
285+
module: {
286+
rules: [
287+
...
288+
{
289+
test: /\.(html|svelte)$/,
290+
use: {
291+
loader: 'svelte-loader',
292+
options: {
293+
preprocess: sveltePreprocess({
294+
postcss: true
295+
})
296+
}
297+
}
298+
}
299+
...
300+
]
301+
},
302+
plugins: [
303+
new webpack.HotModuleReplacementPlugin(),
304+
...
305+
]
306+
}
307+
...
308+
```
309+
310+
Create `postcss.config.js`:
311+
312+
```javascript
313+
module.exports = {
314+
plugins: [
315+
require('postcss-import')
316+
]
317+
}
318+
```
319+
320+
If you are using autoprefixer for `.css`, then it is better to exclude emitted css, because it was already processed with `postcss` through `svelte-preprocess` before emitting.
321+
322+
```javascript
323+
...
324+
module: {
325+
rules: [
326+
...
327+
{
328+
test: /\.css$/,
329+
exclude: /svelte\.\d+\.css/,
330+
use: [
331+
MiniCssExtractPlugin.loader,
332+
'css-loader',
333+
'postcss-loader'
334+
]
335+
},
336+
{
337+
test: /\.css$/,
338+
include: /svelte\.\d+\.css/,
339+
use: [
340+
MiniCssExtractPlugin.loader,
341+
'css-loader'
342+
]
343+
},
344+
...
345+
]
346+
},
347+
...
348+
```
349+
350+
This ensures that global css is being processed with `postcss` through webpack rules, and svelte component's css is being processed with `postcss` through `svelte-preprocess`.
351+
279352
## License
280353

281354
MIT

0 commit comments

Comments
 (0)