Skip to content
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

Make it work with Webpack 5, use webpack-virtual-modules as a dependency #146

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# svelte-loader changelog

## 3.0.0

* **Breaking change**: if `emitCss` is enabled, you have to add a `SveltePlugin` to your `webpack.config.js` like so: ([#146](https://github.com/sveltejs/svelte-loader/pull/146))
```js
const SveltePlugin = require('svelte-loader').plugin; // at top of config
...
module.exports = {
...
plugins: [
...
new SveltePlugin(),
...
]
...
};
```
* Webpack 5 is now supported
* Fix intermittent crashes when using `cache-loader` in front of `svelte-loader` ([#125](https://github.com/sveltejs/svelte-loader/pull/125))

## 2.13.6

* Check whether the filesystem implements `purge` before calling it ([#81](https://github.com/sveltejs/svelte-loader/pull/81))
Expand Down
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Configure inside your `webpack.config.js`:
test: /\.(html|svelte)$/,
exclude: /node_modules/,
use: 'svelte-loader'
},
{
// required to prevent errors from Svelte on Webpack 5+
test: /node_modules\/svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false
}
}
...
]
Expand All @@ -54,9 +61,13 @@ 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://webpack.js.org/plugins/mini-css-extract-plugin/) to the mix to output the css to a separate file.

Make sure to add SveltePlugin like shown below, which allows emitting virtual CSS files. It is a wrapper around webpack-virtual-modules, connected to the svelte-loader.

```javascript
const SveltePlugin = require('svelte-loader').plugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
...
module: {
rules: [
Expand All @@ -73,17 +84,20 @@ A better option is to extract the CSS into a separate file. Using the `emitCss`
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
}),
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new SveltePlugin(),
...
]
...
Expand Down Expand Up @@ -115,19 +129,26 @@ module.exports = {
},
},
},

{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{ loader: 'css-loader', options: { sourceMap: true } }],
}),
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { sourceMap: true }
}
]
},
...
]
},
...
plugins: [
new ExtractTextPlugin('styles.css'),
new MiniCssExtractPlugin({
filename: '[name].css'
}),
new SveltePlugin(),
...
]
...
Expand Down
46 changes: 36 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { basename, extname, relative } = require('path');
const { getOptions } = require('loader-utils');
const VirtualModules = require('./lib/virtual');
const VirtualModules = require('webpack-virtual-modules');

const hotApi = require.resolve('./lib/hot-api.js');

Expand Down Expand Up @@ -96,13 +96,9 @@ function deprecatePreprocessOptions(options) {
options.preprocess = options.preprocess || preprocessOptions;
}

const virtualModuleInstances = new Map();

module.exports = function(source, map) {
if (this._compiler && !virtualModuleInstances.has(this._compiler)) {
virtualModuleInstances.set(this._compiler, new VirtualModules(this._compiler));
}
let virtualModuleInstances = new WeakMap();

function loader(source, map) {
const virtualModules = virtualModuleInstances.get(this._compiler);

this.cacheable();
Expand Down Expand Up @@ -169,9 +165,28 @@ module.exports = function(source, map) {
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
js.code = js.code + `\nimport '${posixify(cssFilepath)}';\n`;

if (virtualModules) {
virtualModules.writeModule(cssFilepath, css.code);
if (!virtualModules) {
throw new Error(
`

To be able to use emitCss: true, add

const SveltePlugin = require('svelte-loader').plugin;

to the top of your webpack.config.js and
SveltePlugin to the plugins array like this

plugins: [
...
new SveltePlugin()
]

If you are running Webpack in watch mode, restart the server to apply these changes.
`.split('\n').map(s => s.slice(5)).join('\n')
non25 marked this conversation as resolved.
Show resolved Hide resolved
);
}

virtualModules.writeModule(cssFilepath, css.code);
}

callback(null, js.code, js.map);
Expand All @@ -180,4 +195,15 @@ module.exports = function(source, map) {
// context when logging to console
callback(new Error(`${err.name}: ${err.toString()}`));
});
};
}

class SveltePlugin extends VirtualModules {
apply(compiler) {
virtualModuleInstances.set(compiler, this);
super.apply(compiler);
}
}

loader.plugin = SveltePlugin;

module.exports = loader;
89 changes: 0 additions & 89 deletions lib/virtual-stats.js

This file was deleted.

89 changes: 0 additions & 89 deletions lib/virtual.js

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-loader",
"version": "2.13.6",
"version": "3.0.0",
"author": "Nico Rehwaldt <git_nikku@nixis.de>",
"description": "A webpack loader for svelte",
"license": "MIT",
Expand All @@ -16,7 +16,8 @@
],
"dependencies": {
"loader-utils": "^1.1.0",
"svelte-dev-helper": "^1.1.9"
"svelte-dev-helper": "^1.1.9",
"webpack-virtual-modules": "^0.4.1"
},
"devDependencies": {
"chai": "^4.1.2",
Expand All @@ -25,7 +26,7 @@
"mocha": "^5.2.0",
"sinon": "^6.1.5",
"sinon-chai": "^3.2.0",
"svelte": "^3.0.0-beta.5"
"svelte": "^3.0.0"
},
"peerDependencies": {
"svelte": ">1.44.0"
Expand Down
Loading