Skip to content

Commit ed9fd2d

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 ed9fd2d

File tree

5 files changed

+38
-211
lines changed

5 files changed

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

+17-14
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,
39-
resourcePath: fileName,
4041
version,
41-
query
42+
query,
43+
resourcePath: fileName,
44+
resource: nameParts[nameParts.length - 1],
4245
},
4346
fileContents,
4447
null
@@ -208,20 +211,20 @@ describe('loader', () => {
208211
{}
209212
)
210213
);
214+
});
211215

212-
it(
213-
'should configure emitCss=true',
214-
testLoader(
215-
'test/fixtures/css.html',
216-
function(err, code, map) {
217-
expect(err).not.to.exist;
216+
it(
217+
'should configure emitCss=true',
218+
testLoader(
219+
'test/fixtures/css.html',
220+
function(err, code, map) {
221+
expect(err).not.to.exist;
218222

219-
expect(code).to.match(/import '.+\.css';/);
220-
},
221-
{ emitCss: true }
222-
)
223-
);
224-
});
223+
expect(code).to.match(/!=!svelte-loader\?cssPath=/);
224+
},
225+
{ emitCss: true }
226+
)
227+
);
225228

226229
describe('preprocess', () => {
227230
it('should preprocess successfully', done => {

0 commit comments

Comments
 (0)