Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
svelte-loader
Advanced tools
npm install --save svelte svelte-loader
Configure inside your webpack.config.js
:
...
resolve: {
// see below for an explanation
alias: {
svelte: path.resolve('node_modules', 'svelte')
},
extensions: ['.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
},
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: 'svelte-loader'
},
{
// required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
test: /node_modules\/svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false
}
}
...
]
}
...
Check out the example project.
The resolve.alias
option is used to make sure that only one copy of the Svelte runtime is bundled in the app, even if you are npm link
ing in dependencies with their own copy of the svelte
package. Having multiple copies of the internal scheduler in an app, besides being inefficient, can also cause various problems.
Webpack's resolve.mainFields
option determines which fields in package.json are used to resolve identifiers. If you're using Svelte components installed from npm, you should specify this option so that your app can use the original component source code, rather than consuming the already-compiled version (which is less efficient).
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 MiniCssExtractPlugin to the mix to output the css to a separate file.
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const mode = process.env.NODE_ENV || 'development';
const prod = mode === 'production';
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
},
},
},
{
test: /\.css$/,
use: [
prod ? MiniCssExtractPlugin.loader :'style-loader',
{
loader: 'css-loader',
options: {
url: false, //necessary if you use url('/path/to/some/asset.png|jpg|gif')
}
}
]
},
...
]
},
...
plugins: [
new MiniCssExtractPlugin('styles.css'),
...
]
...
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.
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.
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.
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.
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use
...
use: {
loader: 'svelte-loader',
options: {
compilerOptions: {
css: false
}
},
},
...
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", // any "source-map"-like devtool is possible
...
module: {
rules: [
...
{
test: /\.css$/,
use: [
prod ? MiniCssExtractPlugin.loader :'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true
}
}
]
},
...
]
},
...
plugins: [
new MiniCssExtractPlugin('styles.css'),
...
]
...
};
This should create an additional styles.css.map
file.
You can specify additional arbitrary compilation options with the compilerOptions
config key, which are passed directly to the underlying Svelte compiler:
...
use: {
loader: 'svelte-loader',
options: {
compilerOptions: {
// additional compiler options here
generate: 'ssr', // for example, SSR can be enabled here
}
},
},
...
This loader supports component-level HMR via the community supported svelte-hmr package. This package serves as a testbed and early access for Svelte HMR, while we figure out how to best include HMR support in the compiler itself (which is tricky to do without unfairly favoring any particular dev tooling). Feedback, suggestion, or help to move HMR forward is welcomed at svelte-hmr (for now).
Configure inside your webpack.config.js
:
// It is recommended to adjust svelte options dynamically, by using
// environment variables
const mode = process.env.NODE_ENV || 'development';
const prod = mode === 'production';
module.exports = {
...
module: {
rules: [
...
{
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: {
loader: 'svelte-loader',
options: {
compilerOptions: {
// NOTE Svelte's dev mode MUST be enabled for HMR to work
dev: !prod, // Default: false
},
// NOTE emitCss: true is currently not supported with HMR
// Enable it for production to output separate css file
emitCss: prod, // Default: false
// Enable HMR only for dev mode
hotReload: !prod, // Default: false
// Extra HMR options, the defaults are completely fine
// You can safely omit hotOptions altogether
hotOptions: {
// Prevent preserving local component state
preserveLocalState: false,
// If this string appears anywhere in your component's code, then local
// state won't be preserved, even when noPreserveState is false
noPreserveStateKey: '@!hmr',
// Prevent doing a full reload on next HMR update after fatal error
noReload: false,
// Try to recover after runtime errors in component init
optimistic: false,
// --- Advanced ---
// Prevent adding an HMR accept handler to components with
// accessors option to true, or to components with named exports
// (from <script context="module">). This have the effect of
// recreating the consumer of those components, instead of the
// component themselves, on HMR updates. This might be needed to
// reflect changes to accessors / named exports in the parents,
// depending on how you use them.
acceptAccessors: true,
acceptNamedExports: true,
}
}
}
}
...
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
...
]
}
You also need to add the HotModuleReplacementPlugin. There are multiple ways to achieve this.
If you're using webpack-dev-server, you can just pass it the hot
option to add the plugin automatically.
Otherwise, you can add it to your webpack config directly:
const webpack = require('webpack');
module.exports = {
...
plugins: [
new webpack.HotModuleReplacementPlugin(),
...
]
}
MIT
3.0.0
compilerOptions
(#159)style
, script
, and markup
options, which are now located under the preprocess
configuration (#158)webpack.config.js
to load the Svelte runtime correctlyrixo/svelte-loader-hot
(#156)Thanks to @non25, @Smittyvb, @rixo, and the numerous others who contributed to this major release!
FAQs
A webpack loader for svelte
The npm package svelte-loader receives a total of 25,408 weekly downloads. As such, svelte-loader popularity was classified as popular.
We found that svelte-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.