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