Skip to content

Commit 07f62da

Browse files
refactor: code
1 parent 53f041e commit 07f62da

File tree

1 file changed

+100
-96
lines changed

1 file changed

+100
-96
lines changed

src/index.js

Lines changed: 100 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ module.exports = (options = {}) => {
6363

6464
return {
6565
postcssPlugin: "postcss-modules-extract-imports",
66-
OnceExit(root, postcss) {
66+
prepare(result) {
6767
const graph = {};
6868
const visited = {};
69-
7069
const existingImports = {};
7170
const importDecls = {};
7271
const imports = {};
@@ -79,114 +78,119 @@ module.exports = (options = {}) => {
7978
`i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}`
8079
: options.createImportedName;
8180

82-
// Check the existing imports order and save refs
83-
root.walkRules((rule) => {
84-
const matches = icssImport.exec(rule.selector);
85-
86-
if (matches) {
87-
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
88-
const importPath = doubleQuotePath || singleQuotePath;
89-
90-
addImportToGraph(importPath, "root", graph, visited);
91-
92-
existingImports[importPath] = rule;
93-
}
94-
});
95-
96-
// Find any declaration that supports imports
97-
root.walkDecls(declFilter, (decl) => {
98-
let matches = decl.value.match(matchImports);
99-
let tmpSymbols;
100-
101-
if (matches) {
102-
let [
103-
,
104-
/*match*/ symbols,
105-
doubleQuotePath,
106-
singleQuotePath,
107-
global,
108-
] = matches;
109-
110-
if (global) {
111-
// Composing globals simply means changing these classes to wrap them in global(name)
112-
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
113-
} else {
81+
return {
82+
// Check the existing imports order and save refs
83+
Rule(rule) {
84+
const matches = icssImport.exec(rule.selector);
85+
86+
if (matches) {
87+
const [, /*match*/ doubleQuotePath, singleQuotePath] = matches;
11488
const importPath = doubleQuotePath || singleQuotePath;
115-
const parentRule = createParentName(decl.parent, root);
11689

117-
addImportToGraph(importPath, parentRule, graph, visited);
90+
addImportToGraph(importPath, "root", graph, visited);
91+
92+
existingImports[importPath] = rule;
93+
}
94+
},
95+
Declaration(decl) {
96+
if (!declFilter.test(decl.prop)) {
97+
return;
98+
}
11899

119-
importDecls[importPath] = decl;
120-
imports[importPath] = imports[importPath] || {};
100+
let matches = decl.value.match(matchImports);
101+
let tmpSymbols;
121102

122-
tmpSymbols = symbols.split(/\s+/).map((s) => {
123-
if (!imports[importPath][s]) {
124-
imports[importPath][s] = createImportedName(s, importPath);
125-
}
103+
if (matches) {
104+
let [
105+
,
106+
/*match*/ symbols,
107+
doubleQuotePath,
108+
singleQuotePath,
109+
global,
110+
] = matches;
126111

127-
return imports[importPath][s];
128-
});
129-
}
112+
if (global) {
113+
// Composing globals simply means changing these classes to wrap them in global(name)
114+
tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`);
115+
} else {
116+
const importPath = doubleQuotePath || singleQuotePath;
117+
const parentRule = createParentName(decl.parent, result.root);
130118

131-
decl.value = tmpSymbols.join(" ");
132-
}
133-
});
119+
addImportToGraph(importPath, parentRule, graph, visited);
134120

135-
const importsOrder = topologicalSort(graph, failOnWrongOrder);
121+
importDecls[importPath] = decl;
122+
imports[importPath] = imports[importPath] || {};
136123

137-
if (importsOrder instanceof Error) {
138-
const importPath = importsOrder.nodes.find((importPath) =>
139-
// eslint-disable-next-line no-prototype-builtins
140-
importDecls.hasOwnProperty(importPath)
141-
);
142-
const decl = importDecls[importPath];
124+
tmpSymbols = symbols.split(/\s+/).map((s) => {
125+
if (!imports[importPath][s]) {
126+
imports[importPath][s] = createImportedName(s, importPath);
127+
}
143128

144-
const errMsg =
145-
"Failed to resolve order of composed modules " +
146-
serializeImports(importsOrder.nodes) +
147-
".";
129+
return imports[importPath][s];
130+
});
131+
}
148132

149-
throw decl.error(errMsg, {
150-
plugin: "postcss-modules-extract-imports",
151-
word: "composes",
152-
});
153-
}
133+
decl.value = tmpSymbols.join(" ");
134+
}
135+
},
136+
OnceExit(root, postcss) {
137+
const importsOrder = topologicalSort(graph, failOnWrongOrder);
138+
139+
if (importsOrder instanceof Error) {
140+
const importPath = importsOrder.nodes.find((importPath) =>
141+
// eslint-disable-next-line no-prototype-builtins
142+
importDecls.hasOwnProperty(importPath)
143+
);
144+
const decl = importDecls[importPath];
145+
146+
const errMsg =
147+
"Failed to resolve order of composed modules " +
148+
serializeImports(importsOrder.nodes) +
149+
".";
150+
151+
throw decl.error(errMsg, {
152+
plugin: "postcss-modules-extract-imports",
153+
word: "composes",
154+
});
155+
}
154156

155-
let lastImportRule;
157+
let lastImportRule;
156158

157-
importsOrder.forEach((path) => {
158-
const importedSymbols = imports[path];
159-
let rule = existingImports[path];
159+
importsOrder.forEach((path) => {
160+
const importedSymbols = imports[path];
161+
let rule = existingImports[path];
160162

161-
if (!rule && importedSymbols) {
162-
rule = postcss.rule({
163-
selector: `:import("${path}")`,
164-
raws: { after: "\n" },
165-
});
163+
if (!rule && importedSymbols) {
164+
rule = postcss.rule({
165+
selector: `:import("${path}")`,
166+
raws: { after: "\n" },
167+
});
166168

167-
if (lastImportRule) {
168-
root.insertAfter(lastImportRule, rule);
169-
} else {
170-
root.prepend(rule);
171-
}
172-
}
173-
174-
lastImportRule = rule;
175-
176-
if (!importedSymbols) {
177-
return;
178-
}
179-
180-
Object.keys(importedSymbols).forEach((importedSymbol) => {
181-
rule.append(
182-
postcss.decl({
183-
value: importedSymbol,
184-
prop: importedSymbols[importedSymbol],
185-
raws: { before: "\n " },
186-
})
187-
);
188-
});
189-
});
169+
if (lastImportRule) {
170+
root.insertAfter(lastImportRule, rule);
171+
} else {
172+
root.prepend(rule);
173+
}
174+
}
175+
176+
lastImportRule = rule;
177+
178+
if (!importedSymbols) {
179+
return;
180+
}
181+
182+
Object.keys(importedSymbols).forEach((importedSymbol) => {
183+
rule.append(
184+
postcss.decl({
185+
value: importedSymbol,
186+
prop: importedSymbols[importedSymbol],
187+
raws: { before: "\n " },
188+
})
189+
);
190+
});
191+
});
192+
},
193+
};
190194
},
191195
};
192196
};

0 commit comments

Comments
 (0)