auklet 0.2.6 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
+
import postcss from 'postcss';
|
|
2
3
|
import { mergeLoadResults } from '#auklet/css/vite/moduleGraph/loadResult';
|
|
3
4
|
import { parsePackageStyleId } from '#auklet/css/vite/moduleGraph/styleId';
|
|
4
5
|
import { toDevDependencyImportSpecifier } from '#auklet/css/vite/moduleGraph/devDependency';
|
|
@@ -12,7 +13,7 @@ export class StyleCodeFactory {
|
|
|
12
13
|
this.config = config;
|
|
13
14
|
}
|
|
14
15
|
async createPackageStyleCode(parsed, cache) {
|
|
15
|
-
return cache.getLoadResult(parsed, () => this.createUncachedPackageStyleCode(parsed, cache));
|
|
16
|
+
return cache.getLoadResult(parsed, async () => this.normalizeTopLevelImports(await this.createUncachedPackageStyleCode(parsed, cache)));
|
|
16
17
|
}
|
|
17
18
|
async createUncachedPackageStyleCode(parsed, cache) {
|
|
18
19
|
const context = await cache.getContext(parsed);
|
|
@@ -43,7 +44,7 @@ export class StyleCodeFactory {
|
|
|
43
44
|
else {
|
|
44
45
|
result = await this.createSourceModuleStyleCode(context, cache, parsed.stylePath);
|
|
45
46
|
}
|
|
46
|
-
return cache.writePersistentLoadResult(parsed, context, result);
|
|
47
|
+
return cache.writePersistentLoadResult(parsed, context, this.normalizeTopLevelImports(result));
|
|
47
48
|
}
|
|
48
49
|
async createStyleCode(context, cache) {
|
|
49
50
|
const results = [];
|
|
@@ -214,4 +215,65 @@ export class StyleCodeFactory {
|
|
|
214
215
|
dependencyPackages: Array.from(new Set([packageName, ...(result.dependencyPackages ?? [])])),
|
|
215
216
|
};
|
|
216
217
|
}
|
|
218
|
+
normalizeTopLevelImports(result) {
|
|
219
|
+
if (!result.code.includes('@import'))
|
|
220
|
+
return result;
|
|
221
|
+
const lateImports = [];
|
|
222
|
+
const removedImports = [];
|
|
223
|
+
const seenImportParams = new Set();
|
|
224
|
+
const root = postcss.parse(result.code);
|
|
225
|
+
let hasChanges = false;
|
|
226
|
+
let hasSeenStyleNode = false;
|
|
227
|
+
for (const node of root.nodes ?? []) {
|
|
228
|
+
if (this.isImportPreludeNode(node))
|
|
229
|
+
continue;
|
|
230
|
+
if (this.isTopLevelImportRule(node)) {
|
|
231
|
+
if (seenImportParams.has(node.params)) {
|
|
232
|
+
hasChanges = true;
|
|
233
|
+
removedImports.push(node);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
seenImportParams.add(node.params);
|
|
237
|
+
if (hasSeenStyleNode) {
|
|
238
|
+
hasChanges = true;
|
|
239
|
+
lateImports.push(node.clone());
|
|
240
|
+
removedImports.push(node);
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
hasSeenStyleNode = true;
|
|
245
|
+
}
|
|
246
|
+
if (!hasChanges)
|
|
247
|
+
return result;
|
|
248
|
+
for (const node of removedImports) {
|
|
249
|
+
node.remove();
|
|
250
|
+
}
|
|
251
|
+
const firstStyleNode = root.nodes?.find((node) => !this.isImportPreludeNode(node) && !this.isTopLevelImportRule(node));
|
|
252
|
+
if (firstStyleNode) {
|
|
253
|
+
for (const rule of lateImports) {
|
|
254
|
+
firstStyleNode.before(rule);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
for (const rule of lateImports) {
|
|
259
|
+
root.append(rule);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
...result,
|
|
264
|
+
code: root.toString(),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
isTopLevelImportRule(node) {
|
|
268
|
+
return node.type === 'atrule' && node.name === 'import';
|
|
269
|
+
}
|
|
270
|
+
isImportPreludeNode(node) {
|
|
271
|
+
if (node.type === 'comment')
|
|
272
|
+
return true;
|
|
273
|
+
if (node.type !== 'atrule')
|
|
274
|
+
return false;
|
|
275
|
+
if (node.name === 'charset')
|
|
276
|
+
return true;
|
|
277
|
+
return node.name === 'layer' && !node.nodes?.length;
|
|
278
|
+
}
|
|
217
279
|
}
|