@dimina/compiler 1.0.17 → 1.1.0

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,23 +1,24 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_env = require("../env-BWsUjuZE.cjs");
2
+ const require_rolldown_runtime = require("../rolldown-runtime-D6vf50IK.cjs");
3
+ const require_env = require("../env-B4U3zZUm.cjs");
3
4
  let node_path = require("node:path");
4
- node_path = require_env.__toESM(node_path, 1);
5
+ node_path = require_rolldown_runtime.__toESM(node_path, 1);
5
6
  let node_worker_threads = require("node:worker_threads");
6
7
  let node_fs = require("node:fs");
7
- node_fs = require_env.__toESM(node_fs, 1);
8
+ node_fs = require_rolldown_runtime.__toESM(node_fs, 1);
8
9
  let _vue_compiler_sfc = require("@vue/compiler-sfc");
9
10
  let autoprefixer = require("autoprefixer");
10
- autoprefixer = require_env.__toESM(autoprefixer, 1);
11
+ autoprefixer = require_rolldown_runtime.__toESM(autoprefixer, 1);
11
12
  let cssnano = require("cssnano");
12
- cssnano = require_env.__toESM(cssnano, 1);
13
+ cssnano = require_rolldown_runtime.__toESM(cssnano, 1);
13
14
  let less = require("less");
14
- less = require_env.__toESM(less, 1);
15
+ less = require_rolldown_runtime.__toESM(less, 1);
15
16
  let postcss = require("postcss");
16
- postcss = require_env.__toESM(postcss, 1);
17
+ postcss = require_rolldown_runtime.__toESM(postcss, 1);
17
18
  let postcss_selector_parser = require("postcss-selector-parser");
18
- postcss_selector_parser = require_env.__toESM(postcss_selector_parser, 1);
19
+ postcss_selector_parser = require_rolldown_runtime.__toESM(postcss_selector_parser, 1);
19
20
  let sass = require("sass");
20
- sass = require_env.__toESM(sass, 1);
21
+ sass = require_rolldown_runtime.__toESM(sass, 1);
21
22
  //#region src/core/style-compiler.js
22
23
  var compileRes = /* @__PURE__ */ new Map();
23
24
  if (!node_worker_threads.isMainThread) node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
@@ -54,7 +55,7 @@ if (!node_worker_threads.isMainThread) node_worker_threads.parentPort.on("messag
54
55
  */
55
56
  async function compileSS(pages, root, progress) {
56
57
  for (const page of pages) {
57
- const code = await buildCompileCss(page, [], /* @__PURE__ */ new Set()) || "";
58
+ const code = await buildCompileCss(page, /* @__PURE__ */ new Set()) || "";
58
59
  const filename = `${page.path.replace(/\//g, "_")}`;
59
60
  if (root) {
60
61
  const subDir = `${require_env.getTargetPath()}/${root}`;
@@ -68,27 +69,51 @@ async function compileSS(pages, root, progress) {
68
69
  progress.completedTasks++;
69
70
  }
70
71
  }
71
- async function buildCompileCss(module, depthChain = [], compiledPaths = /* @__PURE__ */ new Set()) {
72
- const currentPath = module.path || module.absolutePath;
73
- if (depthChain.includes(currentPath)) {
74
- console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
75
- return "";
76
- }
77
- if (depthChain.length > 20) {
78
- console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
79
- return "";
80
- }
81
- if (compiledPaths.has(currentPath)) return "";
82
- compiledPaths.add(currentPath);
83
- depthChain = [...depthChain, currentPath];
84
- let result = await enhanceCSS(module) || "";
85
- if (module.usingComponents) for (const componentInfo of Object.values(module.usingComponents)) {
86
- const componentModule = require_env.getComponent(componentInfo);
87
- if (!componentModule) continue;
88
- result += await buildCompileCss(componentModule, depthChain, compiledPaths) || "";
72
+ async function buildCompileCss(module, compiledPaths = /* @__PURE__ */ new Set()) {
73
+ let result = "";
74
+ const pendingModules = [module];
75
+ while (pendingModules.length > 0) {
76
+ const currentModule = pendingModules.pop();
77
+ const currentPath = currentModule.path || currentModule.absolutePath;
78
+ if (compiledPaths.has(currentPath)) continue;
79
+ compiledPaths.add(currentPath);
80
+ result += await enhanceCSS(currentModule) || "";
81
+ const componentPaths = Object.values(currentModule.usingComponents || {});
82
+ for (let index = componentPaths.length - 1; index >= 0; index--) {
83
+ const componentModule = require_env.getComponent(componentPaths[index]);
84
+ if (componentModule) pendingModules.push(componentModule);
85
+ }
89
86
  }
90
87
  return result;
91
88
  }
89
+ function boostExternalClassSelectors(cssCode, moduleId) {
90
+ if (!moduleId || !cssCode) return cssCode;
91
+ const scopeAttribute = `data-v-${moduleId}`;
92
+ const externalScopeAttribute = "data-dd-external-class-scope";
93
+ const ast = postcss.default.parse(cssCode);
94
+ ast.walkRules((rule) => {
95
+ if (!rule.selector.includes(`[${scopeAttribute}]`)) return;
96
+ rule.selector = (0, postcss_selector_parser.default)((selectors) => {
97
+ for (const selector of [...selectors.nodes]) {
98
+ const boostedSelector = selector.clone();
99
+ const scopeNodes = [];
100
+ boostedSelector.walkAttributes((attribute) => {
101
+ if (attribute.attribute === scopeAttribute) scopeNodes.push(attribute);
102
+ });
103
+ const targetScope = scopeNodes.at(-1);
104
+ if (!targetScope) continue;
105
+ targetScope.parent.insertAfter(targetScope, postcss_selector_parser.default.attribute({
106
+ attribute: externalScopeAttribute,
107
+ operator: "~=",
108
+ quoteMark: "\"",
109
+ value: scopeAttribute
110
+ }));
111
+ selectors.append(boostedSelector);
112
+ }
113
+ }).processSync(rule.selector);
114
+ });
115
+ return ast.toResult().css;
116
+ }
92
117
  async function enhanceCSS(module) {
93
118
  const absolutePath = module.absolutePath ? module.absolutePath : getAbsolutePath(module.path);
94
119
  if (!absolutePath) return "";
@@ -122,12 +147,13 @@ async function enhanceCSS(module) {
122
147
  const promises = [];
123
148
  ast.walk(async (node) => {
124
149
  if (node.type === "atrule" && node.name === "import") {
125
- const importFullPath = resolveStyleImportPath(absolutePath, node.params.replace(/^['"]|['"]$/g, ""));
150
+ const str = node.params.replace(/^['"]|['"]$/g, "");
151
+ const importFullPath = resolveStyleImportPath(absolutePath, str);
126
152
  node.remove();
127
153
  promises.push(buildCompileCss({
128
154
  absolutePath: importFullPath,
129
155
  id: module.id
130
- }, [], /* @__PURE__ */ new Set()));
156
+ }, /* @__PURE__ */ new Set()));
131
157
  } else if (node.type === "rule") {
132
158
  if (node.selector.includes("::v-deep")) node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
133
159
  if (node.selector.includes(":host")) node.selector = processHostSelector(node.selector, module.id);
@@ -149,8 +175,8 @@ async function enhanceCSS(module) {
149
175
  id: moduleId,
150
176
  scoped: !!moduleId
151
177
  }).code;
152
- const cleanedCode = await removeBaseComponentScope(scopedCode, moduleId);
153
- const res = await (0, postcss.default)([(0, autoprefixer.default)({ overrideBrowserslist: ["cover 99.5%"] }), (0, cssnano.default)()]).process(cleanedCode, { from: void 0 });
178
+ const externalClassCode = boostExternalClassSelectors(scopedCode, moduleId);
179
+ const res = await (0, postcss.default)([(0, autoprefixer.default)({ overrideBrowserslist: ["cover 99.5%"] }), (0, cssnano.default)()]).process(externalClassCode, { from: void 0 });
154
180
  const result = (await Promise.all(promises)).filter(Boolean).join("") + res.css;
155
181
  compileRes.set(cacheKey, result);
156
182
  return result;
@@ -184,25 +210,6 @@ function normalizeRootStyleImports(source, workPath = require_env.getWorkPath())
184
210
  });
185
211
  }
186
212
  /**
187
- * 移除基础组件选择器的 scoped 属性
188
- * @param {string} css - 包含 scoped 属性的 CSS
189
- * @param {string} moduleId - 模块 ID
190
- * @returns {Promise<string>} - 清理后的 CSS
191
- */
192
- async function removeBaseComponentScope(css, moduleId) {
193
- if (!moduleId) return css;
194
- const ast = postcss.default.parse(css);
195
- const scopeAttrName = `data-v-${moduleId}`;
196
- ast.walkRules((rule) => {
197
- if (require_env.tagWhiteList.some((tag) => rule.selector.includes(`.dd-${tag}`)) && rule.selector.includes(scopeAttrName)) rule.selector = (0, postcss_selector_parser.default)((selectors) => {
198
- selectors.walkAttributes((attr) => {
199
- if (attr.attribute === scopeAttrName) attr.remove();
200
- });
201
- }).processSync(rule.selector);
202
- });
203
- return ast.toResult().css;
204
- }
205
- /**
206
213
  * Ensures that all @import statements in CSS end with semicolons
207
214
  * @param {string} css - The CSS content to process
208
215
  * @returns {string} - The processed CSS with semicolons added to @import statements as needed
@@ -219,13 +226,14 @@ function ensureImportSemicolons(css) {
219
226
  * @returns {string} - 转换后的选择器
220
227
  */
221
228
  function processHostSelector(selector, moduleId) {
222
- 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}]`);
229
+ const hostSelector = `[data-dd-style-host~="${moduleId}"]`;
230
+ return selector.replace(/:host\(([^)]+)\)/g, `${hostSelector}$1`).replace(/:host(?![\w-])/g, hostSelector);
223
231
  }
224
232
  //#endregion
233
+ exports.boostExternalClassSelectors = boostExternalClassSelectors;
225
234
  exports.compileSS = compileSS;
226
235
  exports.ensureImportSemicolons = ensureImportSemicolons;
227
236
  exports.normalizeCssUrlValue = normalizeCssUrlValue;
228
237
  exports.normalizeRootStyleImports = normalizeRootStyleImports;
229
238
  exports.processHostSelector = processHostSelector;
230
- exports.removeBaseComponentScope = removeBaseComponentScope;
231
239
  exports.resolveStyleImportPath = resolveStyleImportPath;
@@ -1,4 +1,4 @@
1
- import { S as transformRpx, a as getContentByPath, h as resetStoreInfo, i as getComponent, l as getStyleExts, m as getWorkPath, n as getAppId, u as getTargetPath, v as collectAssets, x as tagWhiteList } from "../env-iShlMOS1.js";
1
+ import { C as tagWhiteList, a as getComponent, d as getTargetPath, g as resetStoreInfo, h as getWorkPath, n as getAppId, o as getContentByPath, u as getStyleExts, w as transformRpx, y as collectAssets } from "../env-OH3--qg8.js";
2
2
  import path from "node:path";
3
3
  import { isMainThread, parentPort } from "node:worker_threads";
4
4
  import fs from "node:fs";
@@ -45,7 +45,7 @@ if (!isMainThread) parentPort.on("message", async ({ pages, storeInfo }) => {
45
45
  */
46
46
  async function compileSS(pages, root, progress) {
47
47
  for (const page of pages) {
48
- const code = await buildCompileCss(page, [], /* @__PURE__ */ new Set()) || "";
48
+ const code = await buildCompileCss(page, /* @__PURE__ */ new Set()) || "";
49
49
  const filename = `${page.path.replace(/\//g, "_")}`;
50
50
  if (root) {
51
51
  const subDir = `${getTargetPath()}/${root}`;
@@ -59,27 +59,51 @@ async function compileSS(pages, root, progress) {
59
59
  progress.completedTasks++;
60
60
  }
61
61
  }
62
- async function buildCompileCss(module, depthChain = [], compiledPaths = /* @__PURE__ */ new Set()) {
63
- const currentPath = module.path || module.absolutePath;
64
- if (depthChain.includes(currentPath)) {
65
- console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
66
- return "";
67
- }
68
- if (depthChain.length > 20) {
69
- console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
70
- return "";
71
- }
72
- if (compiledPaths.has(currentPath)) return "";
73
- compiledPaths.add(currentPath);
74
- depthChain = [...depthChain, currentPath];
75
- let result = await enhanceCSS(module) || "";
76
- if (module.usingComponents) for (const componentInfo of Object.values(module.usingComponents)) {
77
- const componentModule = getComponent(componentInfo);
78
- if (!componentModule) continue;
79
- result += await buildCompileCss(componentModule, depthChain, compiledPaths) || "";
62
+ async function buildCompileCss(module, compiledPaths = /* @__PURE__ */ new Set()) {
63
+ let result = "";
64
+ const pendingModules = [module];
65
+ while (pendingModules.length > 0) {
66
+ const currentModule = pendingModules.pop();
67
+ const currentPath = currentModule.path || currentModule.absolutePath;
68
+ if (compiledPaths.has(currentPath)) continue;
69
+ compiledPaths.add(currentPath);
70
+ result += await enhanceCSS(currentModule) || "";
71
+ const componentPaths = Object.values(currentModule.usingComponents || {});
72
+ for (let index = componentPaths.length - 1; index >= 0; index--) {
73
+ const componentModule = getComponent(componentPaths[index]);
74
+ if (componentModule) pendingModules.push(componentModule);
75
+ }
80
76
  }
81
77
  return result;
82
78
  }
79
+ function boostExternalClassSelectors(cssCode, moduleId) {
80
+ if (!moduleId || !cssCode) return cssCode;
81
+ const scopeAttribute = `data-v-${moduleId}`;
82
+ const externalScopeAttribute = "data-dd-external-class-scope";
83
+ const ast = postcss.parse(cssCode);
84
+ ast.walkRules((rule) => {
85
+ if (!rule.selector.includes(`[${scopeAttribute}]`)) return;
86
+ rule.selector = selectorParser((selectors) => {
87
+ for (const selector of [...selectors.nodes]) {
88
+ const boostedSelector = selector.clone();
89
+ const scopeNodes = [];
90
+ boostedSelector.walkAttributes((attribute) => {
91
+ if (attribute.attribute === scopeAttribute) scopeNodes.push(attribute);
92
+ });
93
+ const targetScope = scopeNodes.at(-1);
94
+ if (!targetScope) continue;
95
+ targetScope.parent.insertAfter(targetScope, selectorParser.attribute({
96
+ attribute: externalScopeAttribute,
97
+ operator: "~=",
98
+ quoteMark: "\"",
99
+ value: scopeAttribute
100
+ }));
101
+ selectors.append(boostedSelector);
102
+ }
103
+ }).processSync(rule.selector);
104
+ });
105
+ return ast.toResult().css;
106
+ }
83
107
  async function enhanceCSS(module) {
84
108
  const absolutePath = module.absolutePath ? module.absolutePath : getAbsolutePath(module.path);
85
109
  if (!absolutePath) return "";
@@ -113,12 +137,13 @@ async function enhanceCSS(module) {
113
137
  const promises = [];
114
138
  ast.walk(async (node) => {
115
139
  if (node.type === "atrule" && node.name === "import") {
116
- const importFullPath = resolveStyleImportPath(absolutePath, node.params.replace(/^['"]|['"]$/g, ""));
140
+ const str = node.params.replace(/^['"]|['"]$/g, "");
141
+ const importFullPath = resolveStyleImportPath(absolutePath, str);
117
142
  node.remove();
118
143
  promises.push(buildCompileCss({
119
144
  absolutePath: importFullPath,
120
145
  id: module.id
121
- }, [], /* @__PURE__ */ new Set()));
146
+ }, /* @__PURE__ */ new Set()));
122
147
  } else if (node.type === "rule") {
123
148
  if (node.selector.includes("::v-deep")) node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
124
149
  if (node.selector.includes(":host")) node.selector = processHostSelector(node.selector, module.id);
@@ -140,8 +165,8 @@ async function enhanceCSS(module) {
140
165
  id: moduleId,
141
166
  scoped: !!moduleId
142
167
  }).code;
143
- const cleanedCode = await removeBaseComponentScope(scopedCode, moduleId);
144
- const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(cleanedCode, { from: void 0 });
168
+ const externalClassCode = boostExternalClassSelectors(scopedCode, moduleId);
169
+ const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(externalClassCode, { from: void 0 });
145
170
  const result = (await Promise.all(promises)).filter(Boolean).join("") + res.css;
146
171
  compileRes.set(cacheKey, result);
147
172
  return result;
@@ -175,25 +200,6 @@ function normalizeRootStyleImports(source, workPath = getWorkPath()) {
175
200
  });
176
201
  }
177
202
  /**
178
- * 移除基础组件选择器的 scoped 属性
179
- * @param {string} css - 包含 scoped 属性的 CSS
180
- * @param {string} moduleId - 模块 ID
181
- * @returns {Promise<string>} - 清理后的 CSS
182
- */
183
- async function removeBaseComponentScope(css, moduleId) {
184
- if (!moduleId) return css;
185
- const ast = postcss.parse(css);
186
- const scopeAttrName = `data-v-${moduleId}`;
187
- ast.walkRules((rule) => {
188
- if (tagWhiteList.some((tag) => rule.selector.includes(`.dd-${tag}`)) && rule.selector.includes(scopeAttrName)) rule.selector = selectorParser((selectors) => {
189
- selectors.walkAttributes((attr) => {
190
- if (attr.attribute === scopeAttrName) attr.remove();
191
- });
192
- }).processSync(rule.selector);
193
- });
194
- return ast.toResult().css;
195
- }
196
- /**
197
203
  * Ensures that all @import statements in CSS end with semicolons
198
204
  * @param {string} css - The CSS content to process
199
205
  * @returns {string} - The processed CSS with semicolons added to @import statements as needed
@@ -210,7 +216,8 @@ function ensureImportSemicolons(css) {
210
216
  * @returns {string} - 转换后的选择器
211
217
  */
212
218
  function processHostSelector(selector, moduleId) {
213
- 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}]`);
219
+ const hostSelector = `[data-dd-style-host~="${moduleId}"]`;
220
+ return selector.replace(/:host\(([^)]+)\)/g, `${hostSelector}$1`).replace(/:host(?![\w-])/g, hostSelector);
214
221
  }
215
222
  //#endregion
216
- export { compileSS, ensureImportSemicolons, normalizeCssUrlValue, normalizeRootStyleImports, processHostSelector, removeBaseComponentScope, resolveStyleImportPath };
223
+ export { boostExternalClassSelectors, compileSS, ensureImportSemicolons, normalizeCssUrlValue, normalizeRootStyleImports, processHostSelector, resolveStyleImportPath };