Skip to content

Commit fb71641

Browse files
non25syvb
andauthored
Support Webpack 5. Drop copy-pasted webpack-virtual-modules (#151)
Co-authored-by: Smittyvb <me@smitop.com>
1 parent 0c64534 commit fb71641

File tree

5 files changed

+22
-198
lines changed

5 files changed

+22
-198
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ Configure inside your `webpack.config.js`:
3333
test: /\.(html|svelte)$/,
3434
exclude: /node_modules/,
3535
use: 'svelte-loader'
36+
},
37+
{
38+
// required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
39+
test: /node_modules\/svelte\/.*\.mjs$/,
40+
resolve: {
41+
fullySpecified: false
42+
}
3643
}
3744
...
3845
]

index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { basename, extname, relative } = require('path');
22
const { getOptions } = require('loader-utils');
3-
const VirtualModules = require('./lib/virtual');
43

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

@@ -96,20 +95,22 @@ function deprecatePreprocessOptions(options) {
9695
options.preprocess = options.preprocess || preprocessOptions;
9796
}
9897

99-
const virtualModuleInstances = new Map();
98+
const virtualModules = new Map();
99+
let index = 0;
100100

101101
module.exports = function(source, map) {
102-
if (this._compiler && !virtualModuleInstances.has(this._compiler)) {
103-
virtualModuleInstances.set(this._compiler, new VirtualModules(this._compiler));
104-
}
105-
106-
const virtualModules = virtualModuleInstances.get(this._compiler);
107-
108102
this.cacheable();
109-
103+
110104
const options = Object.assign({}, getOptions(this));
111105
const callback = this.async();
112106

107+
if (options.cssPath) {
108+
const css = virtualModules.get(options.cssPath);
109+
virtualModules.delete(options.cssPath);
110+
callback(null, css);
111+
return;
112+
}
113+
113114
const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr');
114115
const isProduction = this.minimize || process.env.NODE_ENV === 'production';
115116

@@ -161,17 +162,11 @@ module.exports = function(source, map) {
161162
}
162163

163164
if (options.emitCss && css.code) {
164-
const cssFilepath = compileOptions.filename.replace(
165-
/\.[^/.]+$/,
166-
`.svelte.css`
167-
);
168-
165+
const resource = posixify(compileOptions.filename);
166+
const cssPath = `${resource}.${index++}.css`;
169167
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
170-
js.code = js.code + `\nimport '${posixify(cssFilepath)}';\n`;
171-
172-
if (virtualModules) {
173-
virtualModules.writeModule(cssFilepath, css.code);
174-
}
168+
js.code += `\nimport '${cssPath}!=!svelte-loader?cssPath=${cssPath}!${resource}'\n;`;
169+
virtualModules.set(cssPath, css.code);
175170
}
176171

177172
callback(null, js.code, js.map);

lib/virtual-stats.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

lib/virtual.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

test/loader.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ describe('loader', () => {
216216
function(err, code, map) {
217217
expect(err).not.to.exist;
218218

219-
expect(code).to.match(/import '.+\.css';/);
219+
expect(code).to.match(/!=!svelte-loader\?cssPath=/);
220220
},
221221
{ emitCss: true }
222222
)

0 commit comments

Comments
 (0)