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
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
32 changes: 21 additions & 11 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import schema from './loader-options.json';
const pluginName = 'mini-css-extract-plugin';

function hotLoader(content, context) {
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 &&
Expand All @@ -29,20 +37,21 @@ function hotLoader(content, context) {
}`;

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,
})});
var cssReload = cssReloadModule(module.id, ${JSON.stringify({
...context.options,
locals: !!context.locals,
})});
var _locals = ${JSON.stringify(context.locals)};
var isEqualLocals = require(${loaderUtils.stringifyRequest(
context.context,
path.join(__dirname, 'hmr/isEqualLocals.js')
)});
module.hot.dispose(function(data) {
cssReload();
data.oldLocals = _locals;
Expand Down Expand Up @@ -290,6 +299,7 @@ export function pitch(request) {
options,
locals,
namedExport,
esModule,
})
: result;

Expand Down