|
1 | 1 | const topologicalSort = require("./topologicalSort");
|
2 | 2 |
|
3 |
| -const declWhitelist = ["composes"]; |
4 |
| -const declFilter = new RegExp(`^(${declWhitelist.join("|")})$`); |
5 | 3 | const matchImports = /^(.+?)\s+from\s+(?:"([^"]+)"|'([^']+)'|(global))$/;
|
6 | 4 | const icssImport = /^:import\((?:"([^"]+)"|'([^']+)')\)/;
|
7 | 5 |
|
8 | 6 | const VISITED_MARKER = 1;
|
9 | 7 |
|
10 |
| -function createParentName(rule, root) { |
11 |
| - return `__${root.index(rule.parent)}_${rule.selector}`; |
12 |
| -} |
13 |
| - |
14 |
| -function serializeImports(imports) { |
15 |
| - return imports.map((importPath) => "`" + importPath + "`").join(", "); |
16 |
| -} |
17 |
| - |
18 | 8 | /**
|
19 | 9 | * :import('G') {}
|
20 | 10 | *
|
@@ -54,139 +44,155 @@ function addImportToGraph(importId, parentId, graph, visited) {
|
54 | 44 | }
|
55 | 45 |
|
56 | 46 | visited[visitedId] = VISITED_MARKER;
|
| 47 | + |
57 | 48 | siblings.push(importId);
|
58 | 49 | }
|
59 | 50 | }
|
60 | 51 |
|
61 | 52 | module.exports = (options = {}) => {
|
| 53 | + let importIndex = 0; |
| 54 | + const createImportedName = |
| 55 | + typeof options.createImportedName !== "function" |
| 56 | + ? (importName /*, path*/) => |
| 57 | + `i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}` |
| 58 | + : options.createImportedName; |
62 | 59 | const failOnWrongOrder = options.failOnWrongOrder;
|
63 | 60 |
|
64 | 61 | return {
|
65 | 62 | postcssPlugin: "postcss-modules-extract-imports",
|
66 |
| - RootExit(root, postcss) { |
| 63 | + prepare() { |
67 | 64 | const graph = {};
|
68 | 65 | const visited = {};
|
69 |
| - |
70 | 66 | const existingImports = {};
|
71 | 67 | const importDecls = {};
|
72 | 68 | const imports = {};
|
73 | 69 |
|
74 |
| - let importIndex = 0; |
75 |
| - |
76 |
| - const createImportedName = |
77 |
| - typeof options.createImportedName !== "function" |
78 |
| - ? (importName /*, path*/) => |
79 |
| - `i__imported_${importName.replace(/\W/g, "_")}_${importIndex++}` |
80 |
| - : options.createImportedName; |
81 |
| - |
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 { |
114 |
| - const importPath = doubleQuotePath || singleQuotePath; |
115 |
| - const parentRule = createParentName(decl.parent, root); |
116 |
| - |
117 |
| - addImportToGraph(importPath, parentRule, graph, visited); |
118 |
| - |
119 |
| - importDecls[importPath] = decl; |
120 |
| - imports[importPath] = imports[importPath] || {}; |
121 |
| - |
122 |
| - tmpSymbols = symbols.split(/\s+/).map((s) => { |
123 |
| - if (!imports[importPath][s]) { |
124 |
| - imports[importPath][s] = createImportedName(s, importPath); |
125 |
| - } |
| 70 | + return { |
| 71 | + OnceExit(root, postcss) { |
| 72 | + // Check the existing imports order and save refs |
| 73 | + root.walkRules((rule) => { |
| 74 | + const matches = icssImport.exec(rule.selector); |
126 | 75 |
|
127 |
| - return imports[importPath][s]; |
128 |
| - }); |
129 |
| - } |
| 76 | + if (matches) { |
| 77 | + const [, /*match*/ doubleQuotePath, singleQuotePath] = matches; |
| 78 | + const importPath = doubleQuotePath || singleQuotePath; |
| 79 | + |
| 80 | + addImportToGraph(importPath, "root", graph, visited); |
130 | 81 |
|
131 |
| - decl.value = tmpSymbols.join(" "); |
132 |
| - } |
133 |
| - }); |
| 82 | + existingImports[importPath] = rule; |
| 83 | + } |
| 84 | + }); |
134 | 85 |
|
135 |
| - const importsOrder = topologicalSort(graph, failOnWrongOrder); |
| 86 | + root.walkDecls(/^composes$/, (declaration) => { |
| 87 | + const matches = declaration.value.match(matchImports); |
| 88 | + |
| 89 | + if (!matches) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + let tmpSymbols; |
| 94 | + let [ |
| 95 | + , |
| 96 | + /*match*/ symbols, |
| 97 | + doubleQuotePath, |
| 98 | + singleQuotePath, |
| 99 | + global, |
| 100 | + ] = matches; |
| 101 | + |
| 102 | + if (global) { |
| 103 | + // Composing globals simply means changing these classes to wrap them in global(name) |
| 104 | + tmpSymbols = symbols.split(/\s+/).map((s) => `global(${s})`); |
| 105 | + } else { |
| 106 | + const importPath = doubleQuotePath || singleQuotePath; |
| 107 | + |
| 108 | + let parent = declaration.parent; |
| 109 | + let parentIndexes = ""; |
| 110 | + |
| 111 | + while (parent.type !== "root") { |
| 112 | + parentIndexes = |
| 113 | + parent.parent.index(parent) + "_" + parentIndexes; |
| 114 | + parent = parent.parent; |
| 115 | + } |
136 | 116 |
|
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]; |
| 117 | + const { selector } = declaration.parent; |
| 118 | + const parentRule = `_${parentIndexes}${selector}`; |
143 | 119 |
|
144 |
| - const errMsg = |
145 |
| - "Failed to resolve order of composed modules " + |
146 |
| - serializeImports(importsOrder.nodes) + |
147 |
| - "."; |
| 120 | + addImportToGraph(importPath, parentRule, graph, visited); |
148 | 121 |
|
149 |
| - throw decl.error(errMsg, { |
150 |
| - plugin: "postcss-modules-extract-imports", |
151 |
| - word: "composes", |
152 |
| - }); |
153 |
| - } |
| 122 | + importDecls[importPath] = declaration; |
| 123 | + imports[importPath] = imports[importPath] || {}; |
154 | 124 |
|
155 |
| - let lastImportRule; |
| 125 | + tmpSymbols = symbols.split(/\s+/).map((s) => { |
| 126 | + if (!imports[importPath][s]) { |
| 127 | + imports[importPath][s] = createImportedName(s, importPath); |
| 128 | + } |
156 | 129 |
|
157 |
| - importsOrder.forEach((path) => { |
158 |
| - const importedSymbols = imports[path]; |
159 |
| - let rule = existingImports[path]; |
| 130 | + return imports[importPath][s]; |
| 131 | + }); |
| 132 | + } |
160 | 133 |
|
161 |
| - if (!rule && importedSymbols) { |
162 |
| - rule = postcss.rule({ |
163 |
| - selector: `:import("${path}")`, |
164 |
| - raws: { after: "\n" }, |
| 134 | + declaration.value = tmpSymbols.join(" "); |
165 | 135 | });
|
166 | 136 |
|
167 |
| - if (lastImportRule) { |
168 |
| - root.insertAfter(lastImportRule, rule); |
169 |
| - } else { |
170 |
| - root.prepend(rule); |
| 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 | + throw decl.error( |
| 147 | + "Failed to resolve order of composed modules " + |
| 148 | + importsOrder.nodes |
| 149 | + .map((importPath) => "`" + importPath + "`") |
| 150 | + .join(", ") + |
| 151 | + ".", |
| 152 | + { |
| 153 | + plugin: "postcss-modules-extract-imports", |
| 154 | + word: "composes", |
| 155 | + } |
| 156 | + ); |
171 | 157 | }
|
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 |
| - }); |
| 158 | + |
| 159 | + let lastImportRule; |
| 160 | + |
| 161 | + importsOrder.forEach((path) => { |
| 162 | + const importedSymbols = imports[path]; |
| 163 | + let rule = existingImports[path]; |
| 164 | + |
| 165 | + if (!rule && importedSymbols) { |
| 166 | + rule = postcss.rule({ |
| 167 | + selector: `:import("${path}")`, |
| 168 | + raws: { after: "\n" }, |
| 169 | + }); |
| 170 | + |
| 171 | + if (lastImportRule) { |
| 172 | + root.insertAfter(lastImportRule, rule); |
| 173 | + } else { |
| 174 | + root.prepend(rule); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + lastImportRule = rule; |
| 179 | + |
| 180 | + if (!importedSymbols) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + Object.keys(importedSymbols).forEach((importedSymbol) => { |
| 185 | + rule.append( |
| 186 | + postcss.decl({ |
| 187 | + value: importedSymbol, |
| 188 | + prop: importedSymbols[importedSymbol], |
| 189 | + raws: { before: "\n " }, |
| 190 | + }) |
| 191 | + ); |
| 192 | + }); |
| 193 | + }); |
| 194 | + }, |
| 195 | + }; |
190 | 196 | },
|
191 | 197 | };
|
192 | 198 | };
|
|
0 commit comments