svelte-loader
A webpack loader for svelte.
Usage
Configure inside your webpack.config.js
:
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: 'svelte-loader'
}
...
]
}
...
Checkout example setup.
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.
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 to the mix to output the css to a separate file.
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
}),
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
...
]
...
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
.
Source maps
JavaScript source maps are enabled by default, you just have to use an appropriate webpack devtool.
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:
module.exports = {
...
devtool: "source-map",
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{ loader: 'css-loader', options: { sourceMap: true } }],
}),
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
...
]
...
};
This should create an additional styles.css.map
file.
License
MIT