Skip to content

Commit

Permalink
feat: updating hmr css module system (#160)
Browse files Browse the repository at this point in the history
* fix: removing modules option

* fix: removing modules option and using css-loader passthru

* fix: tests
  • Loading branch information
ScriptedAlchemy committed Mar 27, 2019
1 parent 0c6ca8e commit e79875e
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 503 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ module.exports = {
loader:ExtractCssChunks.loader,
options: {
hot: true, // if you want HMR - we try to automatically inject hot reloading but if it's not working, add it to the config
modules: true, // if you use cssModules, this can help.
reloadAll: true, // when desperation kicks in - this is a brute force HMR flag

}
Expand Down Expand Up @@ -168,7 +167,6 @@ module.exports = {
loader:ExtractCssChunks.loader,
options: {
hot: true,
modules: true,
reloadAll: true
},
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
},
"dependencies": {
"loader-utils": "^1.1.0",
"normalize-url": "^3.0.0",
"lodash": "^4.17.11",
"normalize-url": "^2.0.1",
"schema-utils": "^1.0.0",
"webpack-sources": "^1.1.0"
},
Expand All @@ -89,6 +89,7 @@
"eslint": "^4.17.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-prettier": "^2.6.0",
"execa": "^1.0.0",
"file-loader": "^1.1.11",
"husky": "^0.14.3",
"jest": "^22.2.2",
Expand Down
1 change: 1 addition & 0 deletions src/hmr/hotModuleReplacement.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ function reloadAll() {

module.exports = function(moduleId, options) {
if (noDocument) {
console.log('no window.document found, will not HMR CSS');
return noop;
}

Expand Down
15 changes: 0 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,6 @@ class ExtractCssChunksPlugin {
});
}

traverseDepthFirst(root, visit) {
let nodesToVisit = [root];

while (nodesToVisit.length > 0) {
const currentNode = nodesToVisit.shift();

if (currentNode !== null && typeof currentNode === 'object') {
const children = Object.values(currentNode);
nodesToVisit = [...children, ...nodesToVisit];
}

visit(currentNode);
}
}

getCssChunkObject(mainChunk) {
const obj = {};
for (const chunk of mainChunk.getAllAsyncChunks()) {
Expand Down
4 changes: 2 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MODULE_TYPE = 'css/extract-css-chunks';
const pluginName = 'extract-css-chunks-webpack-plugin';

function hotLoader(content, context) {
const accept = context.modules
const accept = context.locals
? ''
: 'module.hot.accept(undefined, cssReload);';
const result = `${content}
Expand Down Expand Up @@ -155,7 +155,7 @@ export function pitch(request) {
: '';

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

return callback(null, resultSource);
Expand Down
15 changes: 7 additions & 8 deletions test/TestCases.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import path from 'path';
import { exec } from 'child_process';

import webpack from 'webpack';
import execa from 'execa';

describe('TestCases', () => {
const casesDirectory = path.resolve(__dirname, 'cases');
Expand Down Expand Up @@ -89,13 +89,12 @@ describe('HMR', () => {
});

it('is es5 only', () => {
exec(
'./node_modules/.bin/es-check es5 src/hmr/hotModuleReplacement.js',
(error, stdout, stderr) => {
expect(
stderr.indexOf('there were no ES version matching errors!') > -1
).toBe(true);
}
const { stderr } = execa.shellSync(
'npx es-check es5 src/hmr/hotModuleReplacement.js'
);

expect(
stderr.indexOf('there were no ES version matching errors') > -1
).toBe(true);
});
});
1 change: 1 addition & 0 deletions test/__snapshots__/TestCases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function reloadAll() {
module.exports = function(moduleId, options) {
if (noDocument) {
console.log('no window.document found, will not HMR CSS');
return noop;
}
Expand Down
2 changes: 2 additions & 0 deletions test/cases/hmr/expected/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
/***/ (function(module, exports, __webpack_require__) {

// extracted by extract-css-chunks-webpack-plugin
if(false) { var cssReload; }


/***/ })
/******/ ]);

0 comments on commit e79875e

Please sign in to comment.