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 2 commits
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;
55 changes: 43 additions & 12 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,46 @@ 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 renderImport = (name, modulePath) => {
const request = loaderUtils.stringifyRequest(context.context, modulePath);

if (context.esModule) {
return `import ${name} from ${request};`;
Copy link
Author

Choose a reason for hiding this comment

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

@alexander-akait not sure if webpack module interop will work here, mb need to change to * as $name

}
return `var ${name} = require(${request});`;
};
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}
${renderImport(
'cssReloadModule',
path.join(__dirname, 'hmr/hotModuleReplacement.js')
)}
${renderImport(
'isEqualLocals',
path.join(__dirname, 'hmr/isEqualLocals.js')
)}
if(module.hot) {
// ${Date.now()}
var cssReload = require(${loaderUtils.stringifyRequest(
context.context,
path.join(__dirname, 'hmr/hotModuleReplacement.js')
)})(module.id, ${JSON.stringify({
...context.options,
locals: !!context.locals,
})});
module.hot.dispose(cssReload);
var cssReload = cssReloadModule(module.id, ${JSON.stringify({
...context.options,
locals: !!context.locals,
})});
var _locals = ${JSON.stringify(context.locals)};
module.hot.dispose(function(data) {
cssReload();
data.oldLocals = _locals;
});
${accept}
}
`;
Expand Down Expand Up @@ -269,7 +294,13 @@ 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,
esModule,
})
: result;

return callback(null, resultSource);
Expand Down