Skip to content

Commit 90eb7cd

Browse files
non25syvb
andcommitted
Support Webpack 5, drop copypasted webpack-virtual-modules
Fixes #139, Fixes #131, Fixes #126 Co-authored-by: Smittyvb <[email protected]>
1 parent 0c64534 commit 90eb7cd

File tree

5 files changed

+26
-199
lines changed

5 files changed

+26
-199
lines changed

README.md

+7
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

+14-19
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(this.resource);
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

-89
This file was deleted.

lib/virtual.js

-89
This file was deleted.

test/loader.spec.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ describe('loader', () => {
3232

3333
const callbackSpy = spy(cb);
3434

35+
const nameParts = fileName.split(/[/\\]/g);
36+
3537
loader.call(
3638
{
3739
cacheable: cacheableSpy,
3840
async: () => callbackSpy,
3941
resourcePath: fileName,
4042
version,
41-
query
43+
query,
44+
resource: nameParts[nameParts.length - 1],
4245
},
4346
fileContents,
4447
null
@@ -216,7 +219,7 @@ describe('loader', () => {
216219
function(err, code, map) {
217220
expect(err).not.to.exist;
218221

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

0 commit comments

Comments
 (0)