Skip to content

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

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

Closed
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ This plugin can be used as a drop-in replacement for `svelte-loader`. It aims to
Configure inside your `webpack.config.js`:

```javascript
// required for emitting css through webpack-virtual-modules plugin
const SveltePlugin = require('svelte-loader-hot').plugin;

module.exports = {
...
module: {
Expand Down Expand Up @@ -82,12 +85,20 @@ module.exports = {
}
}
}
},
{
// required to prevent errors from Svelte on Webpack 5+
test: /svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false
}
}
...
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new SveltePlugin(),
...
]
}
Expand Down
44 changes: 34 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 posixify = require('./lib/posixify');

const {
Expand Down Expand Up @@ -68,13 +68,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 @@ -142,9 +138,26 @@ 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()
]
`.split('\n').map(s => s.slice(5)).join('\n')
);
}

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

callback(null, js.code, js.map);
Expand All @@ -153,4 +166,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.

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

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-loader-hot",
"version": "0.3.1",
"version": "1.0.0",
"author": "Nico Rehwaldt <[email protected]>",
"maintainers": [
{
Expand All @@ -27,7 +27,8 @@
"dependencies": {
"loader-utils": "^1.1.0",
"svelte-dev-helper": "^1.1.9",
"svelte-hmr": "^0.10.3"
"svelte-hmr": "^0.10.3",
"webpack-virtual-modules": "^0.4.1"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
13 changes: 0 additions & 13 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,6 @@ describe('loader', () => {
{}
)
);

it(
'should configure emitCss=true',
testLoader(
'test/fixtures/css.html',
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.match(/import '.+\.css';/);
},
{ emitCss: true }
)
);
});

describe('preprocess', () => {
Expand Down