Skip to content

Commit 88a5c2b

Browse files
fix: hot module replacement logic for lazy type (#468)
1 parent 5bb4f34 commit 88a5c2b

File tree

7 files changed

+62
-25
lines changed

7 files changed

+62
-25
lines changed

.github/workflows/nodejs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jobs:
6161
runs-on: ${{ matrix.os }}
6262

6363
steps:
64+
- name: Setup Git
65+
if: matrix.os == 'windows-latest'
66+
run: git config --global core.autocrlf input
67+
6468
- uses: actions/checkout@v2
6569

6670
- name: Use Node.js ${{ matrix.node-version }}

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5858
"babel-jest": "^25.4.0",
5959
"cross-env": "^7.0.2",
60-
"css-loader": "^3.5.2",
60+
"css-loader": "^3.5.3",
6161
"del": "^5.1.0",
6262
"del-cli": "^3.0.0",
6363
"es-check": "^5.1.0",
@@ -72,7 +72,7 @@
7272
"memfs": "^3.1.2",
7373
"npm-run-all": "^4.1.5",
7474
"prettier": "^2.0.5",
75-
"sass": "^1.26.3",
75+
"sass": "^1.26.5",
7676
"sass-loader": "^8.0.2",
7777
"semver": "^7.3.2",
7878
"standard-version": "^7.1.0",

src/index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ if (module.hot) {
3939
function() {
4040
${
4141
esModule
42-
? `update(content);`
43-
: `var newContent = require(${loaderUtils.stringifyRequest(
42+
? 'update(content);'
43+
: `content = require(${loaderUtils.stringifyRequest(
4444
this,
4545
`!!${request}`
4646
)});
4747
48-
newContent = newContent.__esModule ? newContent.default : newContent;
48+
content = content.__esModule ? content.default : content;
4949
50-
update(newContent);`
50+
update(content);`
5151
}
5252
}
5353
);
@@ -118,23 +118,23 @@ if (module.hot) {
118118
if (update && refs > 0) {
119119
update(content);
120120
}`
121-
: `var newContent = require(${loaderUtils.stringifyRequest(
121+
: `content = require(${loaderUtils.stringifyRequest(
122122
this,
123123
`!!${request}`
124124
)});
125125
126-
newContent = newContent.__esModule ? newContent.default : newContent;
126+
content = content.__esModule ? content.default : content;
127127
128-
if (!isEqualLocals(oldLocals, newContent.locals)) {
128+
if (!isEqualLocals(oldLocals, content.locals)) {
129129
module.hot.invalidate();
130130
131131
return;
132132
}
133133
134-
oldLocals = newContent.locals;
134+
oldLocals = content.locals;
135135
136136
if (update && refs > 0) {
137-
update(newContent);
137+
update(content);
138138
}`
139139
}
140140
}
@@ -230,26 +230,26 @@ if (module.hot) {
230230
oldLocals = content.locals;
231231
232232
update(content);`
233-
: `var newContent = require(${loaderUtils.stringifyRequest(
233+
: `content = require(${loaderUtils.stringifyRequest(
234234
this,
235235
`!!${request}`
236236
)});
237237
238-
newContent = newContent.__esModule ? newContent.default : newContent;
238+
content = content.__esModule ? content.default : content;
239239
240-
if (typeof newContent === 'string') {
241-
newContent = [[module.id, newContent, '']];
240+
if (typeof content === 'string') {
241+
content = [[module.id, content, '']];
242242
}
243243
244-
if (!isEqualLocals(oldLocals, newContent.locals)) {
244+
if (!isEqualLocals(oldLocals, content.locals)) {
245245
module.hot.invalidate();
246246
247247
return;
248248
}
249249
250-
oldLocals = newContent.locals;
250+
oldLocals = content.locals;
251251
252-
update(newContent);`
252+
update(content);`
253253
}
254254
}
255255
)

test/manual/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ <h2>Modules</h2>
8787
<div class="page-btn">BACKGROUND SHOULD BE CRIMSON</div>
8888
</section>
8989

90+
<section id="toggle-section">
91+
<h2>Toggle</h2>
92+
</section>
93+
9094
<section>
9195
<h2>Custom element</h2>
9296
<custom-square l="100" c="red"></custom-square>

test/manual/src/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import one from './modules/one.module.css';
1616
import two from './modules/two.module.css';
1717
import toolbar from './modules/toolbar.module.css';
1818
import page from './modules/page.module.css';
19+
import toogle from './toogle.lazy.css';
1920

2021
console.log('___LOCALS___');
2122
console.log(component);
@@ -114,3 +115,28 @@ const common1 = document.querySelector('.common');
114115
common1.className = toolbar.common;
115116
const pageBtn = document.querySelector('.page-btn');
116117
pageBtn.className = page['page-btn'];
118+
119+
const button = document.createElement('button');
120+
121+
button.innerText = 'Toggle CSS';
122+
123+
let used = false;
124+
125+
button.addEventListener('click', () => {
126+
if (!used) {
127+
console.log('toggle on');
128+
toogle.use();
129+
130+
used = true;
131+
} else {
132+
console.log('toggle off');
133+
134+
toogle.unuse();
135+
136+
used = false;
137+
}
138+
});
139+
140+
const toggleSection = document.getElementById('toggle-section');
141+
142+
toggleSection.appendChild(button);

test/manual/src/toogle.lazy.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#toggle-section {
2+
background-color: red;
3+
}

0 commit comments

Comments
 (0)