-
Notifications
You must be signed in to change notification settings - Fork 72
Refactor code, drop more legacy stuff #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
b71db9f
0570c91
834fc95
7e9bbb2
15caf43
012cc29
b4d3ee7
b5bdf6b
e5759ff
8eef80b
fb34982
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,34 +17,34 @@ npm install --save svelte svelte-loader | |
Configure inside your `webpack.config.js`: | ||
|
||
```javascript | ||
... | ||
resolve: { | ||
// see below for an explanation | ||
alias: { | ||
svelte: path.resolve('node_modules', 'svelte') | ||
}, | ||
extensions: ['.mjs', '.js', '.svelte'], | ||
mainFields: ['svelte', 'browser', 'module', 'main'] | ||
... | ||
resolve: { | ||
// see below for an explanation | ||
alias: { | ||
svelte: path.resolve('node_modules', 'svelte') | ||
}, | ||
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 | ||
} | ||
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](https://github.com/sveltejs/template-webpack). | ||
|
@@ -61,42 +61,71 @@ Webpack's [`resolve.mainFields`](https://webpack.js.org/configuration/resolve/#r | |
|
||
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](https://github.com/webpack-contrib/extract-text-webpack-plugin) to the mix to output the css to a separate file. | ||
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. | ||
|
||
```javascript | ||
... | ||
module: { | ||
rules: [ | ||
... | ||
{ | ||
test: /\.(html|svelte)$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'svelte-loader', | ||
options: { | ||
emitCss: true, | ||
}, | ||
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: ExtractTextPlugin.extract({ | ||
fallback: 'style-loader', | ||
use: 'css-loader', | ||
}), | ||
}, | ||
... | ||
] | ||
}, | ||
... | ||
plugins: [ | ||
new ExtractTextPlugin('styles.css'), | ||
}, | ||
{ | ||
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'), | ||
... | ||
] | ||
... | ||
``` | ||
|
||
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`. | ||
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 if it's included in your svelte components. | ||
non25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Alternatively, if you're handling styles in some other way and just want to prevent the CSS being added to your JavaScript bundle, use | ||
|
||
```javascript | ||
... | ||
use: { | ||
loader: 'svelte-loader', | ||
options: { | ||
compilerOptions: { | ||
css: false | ||
} | ||
}, | ||
}, | ||
... | ||
``` | ||
|
||
### Source maps | ||
|
||
|
@@ -106,38 +135,33 @@ To enable CSS source maps, you'll need to use `emitCss` and pass the `sourceMap` | |
|
||
```javascript | ||
module.exports = { | ||
... | ||
devtool: "source-map", // any "source-map"-like devtool is possible | ||
... | ||
module: { | ||
rules: [ | ||
... | ||
{ | ||
test: /\.(html|svelte)$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'svelte-loader', | ||
... | ||
devtool: "source-map", // any "source-map"-like devtool is possible | ||
... | ||
module: { | ||
rules: [ | ||
... | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
prod ? MiniCssExtractPlugin.loader :'style-loader', | ||
{ | ||
loader: 'css-loader', | ||
options: { | ||
emitCss: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
test: /\.css$/, | ||
use: ExtractTextPlugin.extract({ | ||
fallback: 'style-loader', | ||
use: [{ loader: 'css-loader', options: { sourceMap: true } }], | ||
}), | ||
}, | ||
... | ||
] | ||
}, | ||
... | ||
plugins: [ | ||
new ExtractTextPlugin('styles.css'), | ||
sourceMap: true | ||
} | ||
} | ||
] | ||
}, | ||
... | ||
] | ||
}, | ||
... | ||
plugins: [ | ||
new MiniCssExtractPlugin('styles.css'), | ||
... | ||
] | ||
... | ||
}; | ||
``` | ||
|
||
|
@@ -231,30 +255,6 @@ module.exports = { | |
} | ||
``` | ||
|
||
#### External Dependencies | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why was this section removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because externalDependencies option was removed ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right you are 😄 |
||
|
||
If you rely on any external dependencies (files required in a preprocessor for example) you might want to watch these files for changes and re-run svelte compile. | ||
|
||
Webpack allows [loader dependencies](https://webpack.js.org/contribute/writing-a-loader/#loader-dependencies) to trigger a recompile. svelte-loader exposes this API via `options.externalDependencies`. | ||
For example: | ||
|
||
```js | ||
... | ||
const variables = path.resolve('./variables.js'); | ||
... | ||
{ | ||
test: /\.(html|svelte)$/, | ||
use: [ | ||
{ | ||
loader: 'svelte-loader', | ||
options: { | ||
externalDependencies: [variables] | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was easier to paste into your webpack config when indented since it'd be indented there. I'd probably revert the indentation changes here and below