Skip to content

Commit 0a3362d

Browse files
committed
chore(webpack-5): update for webpack 5
update manual test cases update CssModule for webpack 5 (to avoid deprecation) clean up inconsistency in loader
1 parent bb09d75 commit 0a3362d

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

src/CssModule.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import webpack from 'webpack';
22

33
import { MODULE_TYPE } from './utils';
44

5+
const TYPES = new Set([MODULE_TYPE]);
6+
const CODE_GENERATION_RESULT = {
7+
sources: new Map(),
8+
runtimeRequirements: new Set(),
9+
};
10+
511
class CssModule extends webpack.Module {
612
constructor({
713
context,
@@ -20,9 +26,11 @@ class CssModule extends webpack.Module {
2026
this.content = content;
2127
this.media = media;
2228
this.sourceMap = sourceMap;
29+
this.buildInfo = {};
30+
this.buildMeta = {};
2331
}
2432

25-
// no source() so webpack doesn't do add stuff to the bundle
33+
// no source() so webpack 4 doesn't do add stuff to the bundle
2634

2735
size() {
2836
return this.content.length;
@@ -38,6 +46,16 @@ class CssModule extends webpack.Module {
3846
}`;
3947
}
4048

49+
// eslint-disable-next-line class-methods-use-this
50+
getSourceTypes() {
51+
return TYPES;
52+
}
53+
54+
// eslint-disable-next-line class-methods-use-this
55+
codeGeneration() {
56+
return CODE_GENERATION_RESULT;
57+
}
58+
4159
nameForCondition() {
4260
const resource = this._identifier.split('!').pop();
4361
const idx = resource.indexOf('?');
@@ -60,6 +78,11 @@ class CssModule extends webpack.Module {
6078
return true;
6179
}
6280

81+
// eslint-disable-next-line class-methods-use-this
82+
needBuild(context, callback) {
83+
callback(null, false);
84+
}
85+
6386
build(options, compilation, resolver, fileSystem, callback) {
6487
this.buildInfo = {};
6588
this.buildMeta = {};

src/loader.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ export function pitch(request) {
192192
: originalExports;
193193

194194
if (namedExport) {
195-
locals = '';
196-
197195
Object.keys(originalExports).forEach((key) => {
198196
if (key !== 'default') {
199-
locals += `\nexport const ${key} = "${originalExports[key]}";`;
197+
if (!locals) locals = {};
198+
locals[key] = originalExports[key];
200199
}
201200
});
202201
} else {
@@ -228,7 +227,11 @@ export function pitch(request) {
228227

229228
const result = locals
230229
? namedExport
231-
? locals
230+
? Object.keys(locals)
231+
.map(
232+
(key) => `\nexport const ${key} = ${JSON.stringify(locals[key])};`
233+
)
234+
.join('')
232235
: `\n${
233236
esModule ? 'export default' : 'module.exports ='
234237
} ${JSON.stringify(locals)};`

test/manual/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<title>mini-css-extract-plugin testcase</title>
77
<meta name="viewport" content="width=device-width, initial-scale=1">
8-
<link rel="stylesheet" type="text/css" href="/dist/preloaded1.css" />
8+
<link rel="stylesheet" type="text/css" href="/dist/preloaded1.chunk.css" />
99
<style>
1010
.test {
1111
background: lightcoral;
@@ -18,7 +18,7 @@
1818
background: lightgreen;
1919
}
2020
</style>
21-
<style data-href="preloaded2.css">
21+
<style data-href="preloaded2.chunk.css">
2222
.preloaded-css2 {
2323
background: lightgreen;
2424
}
@@ -46,18 +46,21 @@
4646
<p>But turn orange, when <button class="lazy-button2">pressing this button</button>. Additional clicks have no effect.</p>
4747
<p>Refresh and press buttons in reverse order: This should turn green instead.</p>
4848
</div>
49+
<div class="test lazy-css-module">
50+
<p>Lazy CSS Module: Must be red, but turn green when <button class="lazy-module-button">pressing this button</button>.</p>
51+
</div>
4952
<div class="test lazy-failure-css">
5053
<p>Lazy CSS: Turn off the network and <button class="lazy-failure-button">press this button</button>.</p>
5154
<p>An error should have appeared.</p>
5255
<p>Now if you turn the network back on and click it again, it should turn aqua.</p>
5356
</div>
5457
<div class="test preloaded-css1">
5558
<p>Preloaded CSS: Must be green.</p>
56-
<p><button class="preloaded-button1">Pressing this button</button> displays an alert and should turn red.</p>
59+
<p><button class="preloaded-button1">Pressing this button</button> displays an alert but has no styling effect.</p>
5760
</div>
5861
<div class="test preloaded-css2">
5962
<p>Preloaded inlined CSS: Must be green.</p>
60-
<p><button class="preloaded-button2">Pressing this button</button> displays an alert and should turn red.</p>
63+
<p><button class="preloaded-button2">Pressing this button</button> displays an alert but has no styling effect.</p>
6164
</div>
6265
<div class="test crossorigin">
6366
<p>CrossOriginLoading Option: Must be red.</p>

test/manual/src/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ Object.keys(classes).forEach((localClass) => {
2323
replaceClass(localClass, classes[localClass]);
2424
});
2525

26+
let oldClasses = classes;
27+
28+
if (module.hot) {
29+
module.hot.accept('./simple.module.css', () => {
30+
Object.keys(oldClasses).forEach((localClass) => {
31+
replaceClass(oldClasses[localClass], localClass);
32+
});
33+
Object.keys(classes).forEach((localClass) => {
34+
replaceClass(localClass, classes[localClass]);
35+
});
36+
oldClasses = classes;
37+
alert('HMR updated CSS module');
38+
});
39+
}
40+
2641
const handleError = (err) => {
2742
document.querySelector('.errors').textContent += `\n${err.toString()}`;
2843
console.error(err);
@@ -44,6 +59,12 @@ const makeButton = (className, fn, shouldDisable = true) => {
4459

4560
makeButton('.lazy-button', () => import('./lazy.js'));
4661
makeButton('.lazy-button2', () => import('./lazy2.css'));
62+
makeButton('.lazy-module-button', () =>
63+
import('./lazy.module.css').then((module) => {
64+
console.log(module);
65+
document.querySelector('.lazy-css-module').classList.add(module.style);
66+
})
67+
);
4768

4869
makeButton('.preloaded-button1', () =>
4970
import(/* webpackChunkName: "preloaded1" */ './preloaded1')
@@ -56,7 +77,7 @@ makeButton('.lazy-failure-button', () => import('./lazy-failure.js'), false);
5677

5778
makeButton('.crossorigin', () => {
5879
const originalPublicPath = __webpack_public_path__;
59-
__webpack_public_path__ = 'http://0.0.0.0:8080/dist/';
80+
__webpack_public_path__ = 'http://127.0.0.1:8080/dist/';
6081
const promise = import('./crossorigin').then(() => {
6182
const lastTwoElements = Array.from(document.head.children).slice(-2);
6283
const hasCrossorigin = lastTwoElements.every(

test/manual/src/lazy.module.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.style {
2+
background: lightgreen;
3+
}

test/manual/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ENABLE_ES_MODULE =
1313
module.exports = {
1414
mode: 'development',
1515
output: {
16-
chunkFilename: '[contenthash].js',
16+
chunkFilename: '[name].chunk.js',
1717
publicPath: '/dist/',
1818
crossOriginLoading: 'anonymous',
1919
},
@@ -60,7 +60,7 @@ module.exports = {
6060
plugins: [
6161
new Self({
6262
filename: '[name].css',
63-
chunkFilename: '[contenthash].css',
63+
chunkFilename: '[name].chunk.css',
6464
}),
6565
],
6666
devServer: {

0 commit comments

Comments
 (0)