@dimina-kit/compiler 0.0.2-dev.20260717120050 → 0.0.2-dev.20260718095333

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.
@@ -14,7 +14,7 @@ import {
14
14
  resetStoreInfo,
15
15
  tagWhiteList,
16
16
  transformRpx
17
- } from "./chunk-O5CWW2ZX.js";
17
+ } from "./chunk-QVEZ34FP.js";
18
18
 
19
19
  // ../../dimina/fe/packages/compiler/src/core/style-compiler.js
20
20
  import path from "node:path";
@@ -61,7 +61,7 @@ if (!isMainThread) {
61
61
  }
62
62
  async function compileSS(pages, root, progress) {
63
63
  for (const page of pages) {
64
- const code = await buildCompileCss(page, [], /* @__PURE__ */ new Set()) || "";
64
+ const code = await buildCompileCss(page, /* @__PURE__ */ new Set()) || "";
65
65
  const filename = `${page.path.replace(/\//g, "_")}`;
66
66
  if (root) {
67
67
  const subDir = `${getTargetPath()}/${root}`;
@@ -79,33 +79,63 @@ async function compileSS(pages, root, progress) {
79
79
  progress.completedTasks++;
80
80
  }
81
81
  }
82
- async function buildCompileCss(module, depthChain = [], compiledPaths = /* @__PURE__ */ new Set()) {
83
- const currentPath = module.path || module.absolutePath;
84
- if (depthChain.includes(currentPath)) {
85
- console.warn("[style]", `\u68C0\u6D4B\u5230\u5FAA\u73AF\u4F9D\u8D56: ${[...depthChain, currentPath].join(" -> ")}`);
86
- return "";
87
- }
88
- if (depthChain.length > 20) {
89
- console.warn("[style]", `\u68C0\u6D4B\u5230\u6DF1\u5EA6\u4F9D\u8D56: ${[...depthChain, currentPath].join(" -> ")}`);
90
- return "";
91
- }
92
- if (compiledPaths.has(currentPath)) {
93
- return "";
94
- }
95
- compiledPaths.add(currentPath);
96
- depthChain = [...depthChain, currentPath];
97
- let result = await enhanceCSS(module) || "";
98
- if (module.usingComponents) {
99
- for (const componentInfo of Object.values(module.usingComponents)) {
100
- const componentModule = getComponent(componentInfo);
101
- if (!componentModule) {
102
- continue;
82
+ async function buildCompileCss(module, compiledPaths = /* @__PURE__ */ new Set()) {
83
+ let result = "";
84
+ const pendingModules = [module];
85
+ while (pendingModules.length > 0) {
86
+ const currentModule = pendingModules.pop();
87
+ const currentPath = currentModule.path || currentModule.absolutePath;
88
+ if (compiledPaths.has(currentPath)) {
89
+ continue;
90
+ }
91
+ compiledPaths.add(currentPath);
92
+ result += await enhanceCSS(currentModule) || "";
93
+ const componentPaths = Object.values(currentModule.usingComponents || {});
94
+ for (let index = componentPaths.length - 1; index >= 0; index--) {
95
+ const componentModule = getComponent(componentPaths[index]);
96
+ if (componentModule) {
97
+ pendingModules.push(componentModule);
103
98
  }
104
- result += await buildCompileCss(componentModule, depthChain, compiledPaths) || "";
105
99
  }
106
100
  }
107
101
  return result;
108
102
  }
103
+ function boostExternalClassSelectors(cssCode, moduleId) {
104
+ if (!moduleId || !cssCode) {
105
+ return cssCode;
106
+ }
107
+ const scopeAttribute = `data-v-${moduleId}`;
108
+ const externalScopeAttribute = "data-dd-external-class-scope";
109
+ const ast = postcss.parse(cssCode);
110
+ ast.walkRules((rule) => {
111
+ if (!rule.selector.includes(`[${scopeAttribute}]`)) {
112
+ return;
113
+ }
114
+ rule.selector = selectorParser((selectors) => {
115
+ for (const selector of [...selectors.nodes]) {
116
+ const boostedSelector = selector.clone();
117
+ const scopeNodes = [];
118
+ boostedSelector.walkAttributes((attribute) => {
119
+ if (attribute.attribute === scopeAttribute) {
120
+ scopeNodes.push(attribute);
121
+ }
122
+ });
123
+ const targetScope = scopeNodes.at(-1);
124
+ if (!targetScope) {
125
+ continue;
126
+ }
127
+ targetScope.parent.insertAfter(targetScope, selectorParser.attribute({
128
+ attribute: externalScopeAttribute,
129
+ operator: "~=",
130
+ quoteMark: '"',
131
+ value: scopeAttribute
132
+ }));
133
+ selectors.append(boostedSelector);
134
+ }
135
+ }).processSync(rule.selector);
136
+ });
137
+ return ast.toResult().css;
138
+ }
109
139
  async function enhanceCSS(module) {
110
140
  const absolutePath = module.absolutePath ? module.absolutePath : getAbsolutePath(module.path);
111
141
  if (!absolutePath) {
@@ -153,7 +183,7 @@ async function enhanceCSS(module) {
153
183
  const str = node.params.replace(/^['"]|['"]$/g, "");
154
184
  const importFullPath = resolveStyleImportPath(absolutePath, str);
155
185
  node.remove();
156
- promises.push(buildCompileCss({ absolutePath: importFullPath, id: module.id }, [], /* @__PURE__ */ new Set()));
186
+ promises.push(buildCompileCss({ absolutePath: importFullPath, id: module.id }, /* @__PURE__ */ new Set()));
157
187
  } else if (node.type === "rule") {
158
188
  if (node.selector.includes("::v-deep")) {
159
189
  node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
@@ -183,11 +213,11 @@ async function enhanceCSS(module) {
183
213
  id: moduleId,
184
214
  scoped: !!moduleId
185
215
  }).code;
186
- const cleanedCode = await removeBaseComponentScope(scopedCode, moduleId);
216
+ const externalClassCode = boostExternalClassSelectors(scopedCode, moduleId);
187
217
  const res = await postcss([
188
218
  autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }),
189
219
  cssnano()
190
- ]).process(cleanedCode, { from: void 0 });
220
+ ]).process(externalClassCode, { from: void 0 });
191
221
  const importCss = (await Promise.all(promises)).filter(Boolean).join("");
192
222
  const result = importCss + res.css;
193
223
  compileRes.set(cacheKey, result);
@@ -234,44 +264,25 @@ function normalizeRootStyleImports(source, workPath = getWorkPath()) {
234
264
  return `${prefix}${path.join(workPath, importPath)}${suffix}`;
235
265
  });
236
266
  }
237
- async function removeBaseComponentScope(css, moduleId) {
238
- if (!moduleId) return css;
239
- const ast = postcss.parse(css);
240
- const scopeAttrName = `data-v-${moduleId}`;
241
- ast.walkRules((rule) => {
242
- const hasBaseComponent = tagWhiteList.some(
243
- (tag) => rule.selector.includes(`.dd-${tag}`)
244
- );
245
- if (hasBaseComponent && rule.selector.includes(scopeAttrName)) {
246
- rule.selector = selectorParser((selectors) => {
247
- selectors.walkAttributes((attr) => {
248
- if (attr.attribute === scopeAttrName) {
249
- attr.remove();
250
- }
251
- });
252
- }).processSync(rule.selector);
253
- }
254
- });
255
- return ast.toResult().css;
256
- }
257
267
  function ensureImportSemicolons(css) {
258
268
  return css.replace(/@import[^;\n]*$/gm, (match) => {
259
269
  return match.endsWith(";") ? match : `${match};`;
260
270
  });
261
271
  }
262
272
  function processHostSelector(selector, moduleId) {
263
- return selector.replace(/^:host$/, `[data-v-${moduleId}]`).replace(/:host\(([^)]+)\)/g, `[data-v-${moduleId}]$1`).replace(/:host\s+/g, `[data-v-${moduleId}] `).replace(/:host(?=\.|#|:)/g, `[data-v-${moduleId}]`);
273
+ const hostSelector = `[data-dd-style-host~="${moduleId}"]`;
274
+ return selector.replace(/:host\(([^)]+)\)/g, `${hostSelector}$1`).replace(/:host(?![\w-])/g, hostSelector);
264
275
  }
265
276
  function __resetStyleState() {
266
277
  compileRes.clear();
267
278
  }
268
279
  export {
269
280
  __resetStyleState,
281
+ boostExternalClassSelectors,
270
282
  compileSS,
271
283
  ensureImportSemicolons,
272
284
  normalizeCssUrlValue,
273
285
  normalizeRootStyleImports,
274
286
  processHostSelector,
275
- removeBaseComponentScope,
276
287
  resolveStyleImportPath
277
288
  };