Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit d923866

Browse files
committed
v4.3.1: Presumably fixes backward compatibility issue with processCss
This issue: css-modules#115 The version v4.2.2 introduced a breaking change for people relying on `processCss` option. The actual reason was that since that version "postcss-modules-resolve-imports" combined separate CSS into single CSS prior to triggering processCss callback, if any was provided. Up to 4.2.1 the behavior was different, and processCss was called on each individual file. This patch, if `processCss` callback is provided, with a help of additional simple plugin, records separate compiled CSS files, and then passes them to `processCss` callback individually, with the same order as before. As of now, I am not sure it solves the issue in all corner cases, but at least with this patch `babel-plugin-css-modules-transform` passes all its functional tests when upgraded to the v4.3.1 of `css-modules-require-hook`.
1 parent 3cd489e commit d923866

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dr.pogodin/css-modules-require-hook",
3-
"version": "4.3.0",
3+
"version": "4.3.1",
44
"description": "A require hook to compile CSS Modules on the fly",
55
"main": "lib/index.js",
66
"engines": {

src/index.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ module.exports = function setupHook(options) {
6363
debugSetup(options);
6464
validate(options);
6565

66+
/* An attempt to fix
67+
* https://github.com/css-modules/css-modules-require-hook/issues/115
68+
*/
69+
const processCssQueue = [];
70+
const processCssPlugin = processCss && postcss.plugin('process-css-plugin', () => (
71+
(root) => {
72+
const tree = root.clone();
73+
tree.walkRules((rule) => {
74+
if (rule.selector && (rule.selector.match(/^:(export|import)/))) {
75+
rule.remove();
76+
}
77+
});
78+
processCssQueue.push({
79+
css: tree.toString(),
80+
file: tree.source.input.file,
81+
});
82+
}
83+
));
84+
6685
const exts = toArray(extensions);
6786
const tokensByFile = {};
6887

@@ -85,19 +104,25 @@ module.exports = function setupHook(options) {
85104
);
86105
}
87106

88-
const plugins = use || [
89-
...prepend,
90-
Values,
91-
mode
92-
? new LocalByDefault({ mode })
93-
: LocalByDefault,
94-
createImportedName
95-
? new ExtractImports({ createImportedName })
96-
: ExtractImports,
97-
new Scope({ generateScopedName: scopedName }),
98-
new ResolveImports({ resolve: { extensions: exts, ...resolveOpts } }),
99-
...append,
100-
];
107+
let plugins = use;
108+
if (!plugins) {
109+
plugins = [
110+
...prepend,
111+
Values,
112+
mode
113+
? new LocalByDefault({ mode })
114+
: LocalByDefault,
115+
createImportedName
116+
? new ExtractImports({ createImportedName })
117+
: ExtractImports,
118+
new Scope({ generateScopedName: scopedName }),
119+
];
120+
if (processCssPlugin) plugins.push(processCssPlugin);
121+
plugins.push(
122+
new ResolveImports({ resolve: { extensions: exts, ...resolveOpts } }),
123+
...append,
124+
);
125+
}
101126

102127
// https://github.com/postcss/postcss#options
103128
const runner = postcss(plugins);
@@ -139,7 +164,10 @@ module.exports = function setupHook(options) {
139164
delete require.cache[filename];
140165
}
141166

142-
if (processCss) processCss(lazyResult.css, filename);
167+
if (processCss) {
168+
processCssQueue.reverse();
169+
processCssQueue.forEach(({ css, file }) => processCss(css, file));
170+
}
143171

144172
debugFetch(`${filename} → fs`);
145173
debugFetch(tokens);

0 commit comments

Comments
 (0)