Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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;
57 changes: 44 additions & 13 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,48 @@ 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