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
Copy file name to clipboardexpand all lines: README.md
+85
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,91 @@ Configure inside your `webpack.config.js`:
27
27
28
28
Checkout [example setup](./example).
29
29
30
+
### Extracting CSS
31
+
32
+
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.
33
+
34
+
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.
35
+
36
+
```javascript
37
+
...
38
+
module: {
39
+
rules: [
40
+
...
41
+
{
42
+
test:/\.(html|svelte)$/,
43
+
exclude:/node_modules/,
44
+
use: {
45
+
loader:'svelte-loader',
46
+
options: {
47
+
emitCss:true,
48
+
},
49
+
},
50
+
},
51
+
{
52
+
test:/\.css$/,
53
+
use:ExtractTextPlugin.extract({
54
+
fallback:'style-loader',
55
+
use:'css-loader',
56
+
}),
57
+
},
58
+
...
59
+
]
60
+
},
61
+
...
62
+
plugins: [
63
+
newExtractTextPlugin('styles.css'),
64
+
...
65
+
]
66
+
...
67
+
```
68
+
69
+
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`.
70
+
71
+
### Source maps
72
+
73
+
JavaScript source maps are enabled by default, you just have to use an appropriate [webpack devtool](https://webpack.js.org/configuration/devtool/).
74
+
75
+
To enable CSS source maps, you'll need to use `emitCss` and pass the `sourceMap` option to the `css-loader`. The above config should look like this:
76
+
77
+
```javascript
78
+
module.exports= {
79
+
...
80
+
devtool:"source-map", // any "source-map"-like devtool is possible
0 commit comments