Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hmr, compare old locals to invalidate if needed #692

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/hmr/isEqualLocals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function isEqualLocals(a, b) {
if ((!a && b) || (a && !b)) {
return false;
}

let p;

for (p in a) {
if (a[p] !== b[p]) {
return false;
}
}

for (p in b) {
if (!a[p]) {
return false;
}
}

return true;
}

module.exports = isEqualLocals;
31 changes: 26 additions & 5 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ import schema from './loader-options.json';
const pluginName = 'mini-css-extract-plugin';

function hotLoader(content, context) {
const accept = context.locals
? ''
: 'module.hot.accept(undefined, cssReload);';
const accept = `
if (!_locals || module.hot.invalidate) {
if (module.hot.invalidate &&
module.hot.data &&
module.hot.data.oldLocals &&
!isEqualLocals(module.hot.data.oldLocals, _locals)) {
module.hot.invalidate();
} else {
module.hot.accept();
}
}`;

return `${content}
if(module.hot) {
Expand All @@ -30,7 +38,15 @@ function hotLoader(content, context) {
...context.options,
locals: !!context.locals,
})});
module.hot.dispose(cssReload);
var _locals = ${JSON.stringify(context.locals)};
var isEqualLocals = require(${loaderUtils.stringifyRequest(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When esModule is true it should be import,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but previously it has

var cssReload = require(${loaderUtils.stringifyRequest(
        context.context,
        path.join(__dirname, 'hmr/hotModuleReplacement.js')

This should be import too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

context.context,
path.join(__dirname, 'hmr/isEqualLocals.js')
)});
module.hot.dispose(function(data) {
cssReload();
data.oldLocals = _locals;
});
${accept}
}
`;
Expand Down Expand Up @@ -269,7 +285,12 @@ export function pitch(request) {
let resultSource = `// extracted by ${pluginName}`;

resultSource += this.hot
? hotLoader(result, { context: this.context, options, locals })
? hotLoader(result, {
context: this.context,
options,
locals,
namedExport,
})
: result;

return callback(null, resultSource);
Expand Down