Skip to content

Commit be0b336

Browse files
committed
Fixed compilation in dev mode and added tests.
1 parent 54ff56e commit be0b336

24 files changed

+7532
-3
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node
2+
3+
cache:
4+
directories:
5+
- ./npm
6+
7+
script:
8+
- npm ci
9+
- npm test

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Promise.all([
7070
7171
## Limitations
7272
73-
You must use the theme imports only in css modules. So for example the following is not
73+
1. You must use the theme imports only in css modules. So for example the following is not
7474
possible since the plugin cannot detect the theme import and do the switch.
7575
7676
```sass
@@ -85,3 +85,8 @@ possible since the plugin cannot detect the theme import and do the switch.
8585
@import './path/to/theme_one/theme.scss'
8686
$myOtherVar: $themeVar1 + $themeVar2
8787
```
88+
89+
2. Currently the plugin won't include plain `.css` imports from your sources or from under
90+
node modules into the separate themes. Since these are not themable anyway it is advised
91+
to separate this css imports in their own files (common chunks) and then load them for all
92+
themes. This is good for caching and theme file size overall.

index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ class MiniCssThemesWebpackPlugin {
122122
this.nonDefaultThemeKeys.forEach(theme => addThemeChunk(theme));
123123
});
124124

125+
let moduleDependencyHistory = [];
126+
let moduleChunkHistory = [];
127+
125128
// Required cleanup for functional final compilation.
126129
compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
127130
compilation.hooks.optimizeChunkModules.tap(this.pluginName, chunks => {
@@ -130,11 +133,13 @@ class MiniCssThemesWebpackPlugin {
130133
// Remove duplicate dependency links for the same css module as otherwise
131134
// webpack will concatenate IDs on the imports of these modules which will
132135
// produce an id to a module that doesn't in fact exist.
133-
if (module.dependencies) {
136+
if (module.dependencies && module.dependencies.find(dep => !dep.themed)) {
137+
moduleDependencyHistory.push({module, dependencies: module.dependencies});
134138
module.dependencies = module.dependencies.filter(dep => !dep.themed);
135139
}
136140
// Remove from built JS duplicates of css modules classes generated by themes.
137141
if (module.themed) {
142+
moduleChunkHistory.push({module, chunk});
138143
module.removeChunk(chunk);
139144
}
140145
});
@@ -143,7 +148,7 @@ class MiniCssThemesWebpackPlugin {
143148
// Remove theme chunks from the entry points so that they are not recognized in
144149
// a dependency tree and thus we can safely remove the empty JS files generated
145150
// by these chunks.
146-
filterThemeChunks(chunks).forEach(chunk => chunk._groups.forEach(entry => entry.removeChunk(chunk)))
151+
filterThemeChunks(chunks).forEach(chunk => chunk._groups.forEach(entry => entry.removeChunk(chunk)));
147152
});
148153
});
149154

@@ -158,6 +163,23 @@ class MiniCssThemesWebpackPlugin {
158163
});
159164
});
160165
});
166+
167+
// When in watch mode, we need to revert previous cleanup as otherwise dependency
168+
// links will be lost and the themes won't be included in future compilations.
169+
compiler.hooks.watchRun.tap(this.pluginName, () => {
170+
compiler.hooks.afterCompile.tap(this.pluginName, () => {
171+
if (moduleDependencyHistory.length) {
172+
moduleDependencyHistory.forEach((history) => history.module.dependencies = history.dependencies);
173+
moduleDependencyHistory = [];
174+
}
175+
176+
if (moduleChunkHistory.length) {
177+
moduleChunkHistory.forEach((history) => history.chunk.addModule(history.module));
178+
moduleChunkHistory = [];
179+
}
180+
});
181+
});
182+
161183
}
162184
}
163185

test/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

test/dist-spec/main.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/dist-spec/main.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/dist-spec/main.js

Lines changed: 160 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/dist-spec/main.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)