@dimina/compiler 1.0.12-beta.9 → 1.0.13

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,6 +1,7 @@
1
- import fs from "node:fs";
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-COaZNCzL.js";
2
2
  import path from "node:path";
3
3
  import { isMainThread, parentPort } from "node:worker_threads";
4
+ import fs from "node:fs";
4
5
  import { compileStyle } from "@vue/compiler-sfc";
5
6
  import autoprefixer from "autoprefixer";
6
7
  import cssnano from "cssnano";
@@ -8,199 +9,214 @@ import less from "less";
8
9
  import postcss from "postcss";
9
10
  import selectorParser from "postcss-selector-parser";
10
11
  import * as sass from "sass";
11
- import { r as resetStoreInfo, g as getTargetPath, a as getComponent, b as getContentByPath, h as tagWhiteList, e as collectAssets, f as getAppId, d as getWorkPath, t as transformRpx } from "../env-Csj3AHY4.js";
12
- const fileType = [".wxss", ".ddss", ".less", ".scss", ".sass"];
13
- const compileRes = /* @__PURE__ */ new Map();
14
- if (!isMainThread) {
15
- parentPort.on("message", async ({ pages, storeInfo }) => {
16
- try {
17
- resetStoreInfo(storeInfo);
18
- const progress = {
19
- _completedTasks: 0,
20
- get completedTasks() {
21
- return this._completedTasks;
22
- },
23
- set completedTasks(value) {
24
- this._completedTasks = value;
25
- parentPort.postMessage({ completedTasks: this._completedTasks });
26
- }
27
- };
28
- await compileSS(pages.mainPages, null, progress);
29
- for (const [root, subPages] of Object.entries(pages.subPages)) {
30
- await compileSS(subPages.info, root, progress);
31
- }
32
- compileRes.clear();
33
- parentPort.postMessage({ success: true });
34
- } catch (error) {
35
- compileRes.clear();
36
- parentPort.postMessage({
37
- success: false,
38
- error: {
39
- message: error.message,
40
- stack: error.stack,
41
- name: error.name
42
- }
43
- });
44
- }
45
- });
46
- }
12
+ //#region src/core/style-compiler.js
13
+ var fileType = [
14
+ ".wxss",
15
+ ".ddss",
16
+ ".less",
17
+ ".scss",
18
+ ".sass"
19
+ ];
20
+ var compileRes = /* @__PURE__ */ new Map();
21
+ if (!isMainThread) parentPort.on("message", async ({ pages, storeInfo }) => {
22
+ try {
23
+ resetStoreInfo(storeInfo);
24
+ const progress = {
25
+ _completedTasks: 0,
26
+ get completedTasks() {
27
+ return this._completedTasks;
28
+ },
29
+ set completedTasks(value) {
30
+ this._completedTasks = value;
31
+ parentPort.postMessage({ completedTasks: this._completedTasks });
32
+ }
33
+ };
34
+ await compileSS(pages.mainPages, null, progress);
35
+ for (const [root, subPages] of Object.entries(pages.subPages)) await compileSS(subPages.info, root, progress);
36
+ compileRes.clear();
37
+ parentPort.postMessage({ success: true });
38
+ } catch (error) {
39
+ compileRes.clear();
40
+ parentPort.postMessage({
41
+ success: false,
42
+ error: {
43
+ message: error.message,
44
+ stack: error.stack,
45
+ name: error.name
46
+ }
47
+ });
48
+ }
49
+ });
50
+ /**
51
+ * 编译样式文件
52
+ */
47
53
  async function compileSS(pages, root, progress) {
48
- for (const page of pages) {
49
- const code = await buildCompileCss(page, []) || "";
50
- const filename = `${page.path.replace(/\//g, "_")}`;
51
- if (root) {
52
- const subDir = `${getTargetPath()}/${root}`;
53
- if (!fs.existsSync(subDir)) {
54
- fs.mkdirSync(subDir, { recursive: true });
55
- }
56
- fs.writeFileSync(`${subDir}/${filename}.css`, code);
57
- } else {
58
- const mainDir = `${getTargetPath()}/main`;
59
- if (!fs.existsSync(mainDir)) {
60
- fs.mkdirSync(mainDir, { recursive: true });
61
- }
62
- fs.writeFileSync(`${mainDir}/${filename}.css`, code);
63
- }
64
- progress.completedTasks++;
65
- }
54
+ for (const page of pages) {
55
+ const code = await buildCompileCss(page, [], /* @__PURE__ */ new Set()) || "";
56
+ const filename = `${page.path.replace(/\//g, "_")}`;
57
+ if (root) {
58
+ const subDir = `${getTargetPath()}/${root}`;
59
+ if (!fs.existsSync(subDir)) fs.mkdirSync(subDir, { recursive: true });
60
+ fs.writeFileSync(`${subDir}/${filename}.css`, code);
61
+ } else {
62
+ const mainDir = `${getTargetPath()}/main`;
63
+ if (!fs.existsSync(mainDir)) fs.mkdirSync(mainDir, { recursive: true });
64
+ fs.writeFileSync(`${mainDir}/${filename}.css`, code);
65
+ }
66
+ progress.completedTasks++;
67
+ }
66
68
  }
67
- async function buildCompileCss(module, depthChain = []) {
68
- const currentPath = module.path;
69
- if (depthChain.includes(currentPath)) {
70
- console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
71
- return;
72
- }
73
- if (depthChain.length > 20) {
74
- console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
75
- return;
76
- }
77
- depthChain = [...depthChain, currentPath];
78
- let result = await enhanceCSS(module) || "";
79
- if (module.usingComponents) {
80
- for (const componentInfo of Object.values(module.usingComponents)) {
81
- const componentModule = getComponent(componentInfo);
82
- if (!componentModule) {
83
- continue;
84
- }
85
- result += await buildCompileCss(componentModule, depthChain);
86
- }
87
- }
88
- return result;
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);
87
+ }
88
+ return result;
89
89
  }
90
90
  async function enhanceCSS(module) {
91
- const absolutePath = module.absolutePath ? module.absolutePath : getAbsolutePath(module.path);
92
- if (!absolutePath) {
93
- return;
94
- }
95
- const inputCSS = getContentByPath(absolutePath);
96
- if (!inputCSS) {
97
- return;
98
- }
99
- if (compileRes.has(absolutePath)) {
100
- return compileRes.get(absolutePath);
101
- }
102
- let processedCSS = inputCSS;
103
- const ext = path.extname(absolutePath).toLowerCase();
104
- try {
105
- if (ext === ".less") {
106
- const result2 = await less.render(inputCSS, {
107
- filename: absolutePath,
108
- paths: [path.dirname(absolutePath)]
109
- });
110
- processedCSS = result2.css;
111
- } else if (ext === ".scss" || ext === ".sass") {
112
- const result2 = sass.compileString(inputCSS, {
113
- loadPaths: [path.dirname(absolutePath)],
114
- syntax: ext === ".sass" ? "indented" : "scss"
115
- });
116
- processedCSS = result2.css;
117
- }
118
- } catch (error) {
119
- console.error(`[style] 预处理器编译失败 ${absolutePath}:`, error.message);
120
- processedCSS = inputCSS;
121
- }
122
- const fixedCSS = ensureImportSemicolons(processedCSS);
123
- let ast;
124
- try {
125
- ast = postcss.parse(fixedCSS);
126
- } catch (error) {
127
- console.error(`[style] PostCSS 解析失败 ${absolutePath}:`, error.message);
128
- return "";
129
- }
130
- const promises = [];
131
- ast.walk(async (node) => {
132
- if (node.type === "atrule" && node.name === "import") {
133
- const str = node.params.replace(/^['"]|['"]$/g, "");
134
- const importFullPath = path.resolve(absolutePath, `../${str}`);
135
- node.remove();
136
- promises.push(buildCompileCss({ absolutePath: importFullPath, id: module.id }));
137
- } else if (node.type === "rule") {
138
- if (node.selector.includes("::v-deep")) {
139
- node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
140
- }
141
- if (node.selector.includes(":host")) {
142
- node.selector = processHostSelector(node.selector, module.id);
143
- }
144
- node.selector = selectorParser((selectors) => {
145
- selectors.walkTags((tag) => {
146
- if (tagWhiteList.includes(tag.value)) {
147
- tag.value = `.dd-${tag.value}`;
148
- }
149
- });
150
- }).processSync(node.selector);
151
- node.walkDecls((decl) => {
152
- const match = decl.value.match(/url\("([^"]*)"\)/);
153
- if (match) {
154
- const imgSrc = match[1].trim();
155
- if (imgSrc.startsWith("data:image")) {
156
- return;
157
- }
158
- const realSrc = collectAssets(getWorkPath(), absolutePath, imgSrc, getTargetPath(), getAppId());
159
- decl.value = `url(${realSrc})`;
160
- } else {
161
- decl.value = transformRpx(decl.value);
162
- }
163
- });
164
- } else if (node.type === "comment") {
165
- node.remove();
166
- }
167
- });
168
- const cssCode = ast.toResult().css;
169
- const moduleId = module.id;
170
- const code = compileStyle({
171
- source: cssCode,
172
- id: moduleId,
173
- scoped: !!moduleId
174
- }).code;
175
- const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(code, {
176
- from: void 0
177
- // 未指定输入源文件路径
178
- });
179
- const importCss = (await Promise.all(promises)).filter(Boolean).join("");
180
- const result = importCss ? importCss + res.css : res.css;
181
- compileRes.set(module.path, result);
182
- return result;
91
+ const absolutePath = module.absolutePath ? module.absolutePath : getAbsolutePath(module.path);
92
+ if (!absolutePath) return;
93
+ const inputCSS = getContentByPath(absolutePath);
94
+ if (!inputCSS) return;
95
+ if (compileRes.has(absolutePath)) return compileRes.get(absolutePath);
96
+ let processedCSS = normalizeRootStyleImports(inputCSS);
97
+ const ext = path.extname(absolutePath).toLowerCase();
98
+ try {
99
+ if (ext === ".less") processedCSS = (await less.render(processedCSS, {
100
+ filename: absolutePath,
101
+ paths: [path.dirname(absolutePath), getWorkPath()]
102
+ })).css;
103
+ else if (ext === ".scss" || ext === ".sass") processedCSS = sass.compileString(processedCSS, {
104
+ loadPaths: [path.dirname(absolutePath), getWorkPath()],
105
+ syntax: ext === ".sass" ? "indented" : "scss"
106
+ }).css;
107
+ } catch (error) {
108
+ console.error(`[style] 预处理器编译失败 ${absolutePath}:`, error.message);
109
+ processedCSS = inputCSS;
110
+ }
111
+ const fixedCSS = ensureImportSemicolons(processedCSS);
112
+ let ast;
113
+ try {
114
+ ast = postcss.parse(fixedCSS);
115
+ } catch (error) {
116
+ console.error(`[style] PostCSS 解析失败 ${absolutePath}:`, error.message);
117
+ return "";
118
+ }
119
+ const promises = [];
120
+ ast.walk(async (node) => {
121
+ if (node.type === "atrule" && node.name === "import") {
122
+ const importFullPath = resolveStyleImportPath(absolutePath, node.params.replace(/^['"]|['"]$/g, ""));
123
+ node.remove();
124
+ promises.push(buildCompileCss({
125
+ absolutePath: importFullPath,
126
+ id: module.id
127
+ }, [], /* @__PURE__ */ new Set()));
128
+ } else if (node.type === "rule") {
129
+ if (node.selector.includes("::v-deep")) node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
130
+ if (node.selector.includes(":host")) node.selector = processHostSelector(node.selector, module.id);
131
+ node.selector = selectorParser((selectors) => {
132
+ selectors.walkTags((tag) => {
133
+ if (tagWhiteList.includes(tag.value)) tag.value = `.dd-${tag.value}`;
134
+ });
135
+ }).processSync(node.selector);
136
+ } else if (node.type === "comment") node.remove();
137
+ });
138
+ ast.walkDecls((decl) => {
139
+ decl.value = normalizeCssUrlValue(decl.value, absolutePath);
140
+ decl.value = transformRpx(decl.value);
141
+ });
142
+ const cssCode = ast.toResult().css;
143
+ const moduleId = module.id;
144
+ const scopedCode = compileStyle({
145
+ source: cssCode,
146
+ id: moduleId,
147
+ scoped: !!moduleId
148
+ }).code;
149
+ const cleanedCode = await removeBaseComponentScope(scopedCode, moduleId);
150
+ const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(cleanedCode, { from: void 0 });
151
+ const result = (await Promise.all(promises)).filter(Boolean).join("") + res.css;
152
+ compileRes.set(module.path, result);
153
+ return result;
154
+ }
155
+ function normalizeCssUrlValue(value, absolutePath) {
156
+ return value.replace(/url\(([^)]+)\)/g, (fullMatch, rawUrl) => {
157
+ const cleanedUrl = rawUrl.trim().replace(/^['"]|['"]$/g, "");
158
+ if (!cleanedUrl || cleanedUrl.startsWith("data:image")) return fullMatch;
159
+ if (cleanedUrl.startsWith("//")) return `url(https:${cleanedUrl})`;
160
+ if (/^(https?:|blob:|data:)/.test(cleanedUrl)) return fullMatch;
161
+ return `url(${collectAssets(getWorkPath(), absolutePath, cleanedUrl, getTargetPath(), getAppId())})`;
162
+ });
183
163
  }
184
164
  function getAbsolutePath(modulePath) {
185
- const workPath = getWorkPath();
186
- const src = modulePath.startsWith("/") ? modulePath : `/${modulePath}`;
187
- for (const ssType of fileType) {
188
- const ssFullPath = `${workPath}${src}${ssType}`;
189
- if (fs.existsSync(ssFullPath)) {
190
- return ssFullPath;
191
- }
192
- }
165
+ const workPath = getWorkPath();
166
+ const src = modulePath.startsWith("/") ? modulePath : `/${modulePath}`;
167
+ for (const ssType of fileType) {
168
+ const ssFullPath = `${workPath}${src}${ssType}`;
169
+ if (fs.existsSync(ssFullPath)) return ssFullPath;
170
+ const indexSsFullPath = `${workPath}${src}/index${ssType}`;
171
+ if (fs.existsSync(indexSsFullPath)) return indexSsFullPath;
172
+ }
173
+ }
174
+ function resolveStyleImportPath(absolutePath, importPath, workPath = getWorkPath()) {
175
+ if (importPath.startsWith("/")) return path.join(workPath, importPath);
176
+ return path.resolve(path.dirname(absolutePath), importPath);
177
+ }
178
+ function normalizeRootStyleImports(source, workPath = getWorkPath()) {
179
+ return source.replace(/(@import\s+(?:\(.*?\)\s*)?(?:url\()?['"])(\/[^'")]+)(['"]\)?)/g, (_, prefix, importPath, suffix) => {
180
+ return `${prefix}${path.join(workPath, importPath)}${suffix}`;
181
+ });
182
+ }
183
+ /**
184
+ * 移除基础组件选择器的 scoped 属性
185
+ * @param {string} css - 包含 scoped 属性的 CSS
186
+ * @param {string} moduleId - 模块 ID
187
+ * @returns {Promise<string>} - 清理后的 CSS
188
+ */
189
+ async function removeBaseComponentScope(css, moduleId) {
190
+ if (!moduleId) return css;
191
+ const ast = postcss.parse(css);
192
+ const scopeAttrName = `data-v-${moduleId}`;
193
+ ast.walkRules((rule) => {
194
+ if (tagWhiteList.some((tag) => rule.selector.includes(`.dd-${tag}`)) && rule.selector.includes(scopeAttrName)) rule.selector = selectorParser((selectors) => {
195
+ selectors.walkAttributes((attr) => {
196
+ if (attr.attribute === scopeAttrName) attr.remove();
197
+ });
198
+ }).processSync(rule.selector);
199
+ });
200
+ return ast.toResult().css;
193
201
  }
202
+ /**
203
+ * Ensures that all @import statements in CSS end with semicolons
204
+ * @param {string} css - The CSS content to process
205
+ * @returns {string} - The processed CSS with semicolons added to @import statements as needed
206
+ */
194
207
  function ensureImportSemicolons(css) {
195
- return css.replace(/@import[^;\n]*$/gm, (match) => {
196
- return match.endsWith(";") ? match : `${match};`;
197
- });
208
+ return css.replace(/@import[^;\n]*$/gm, (match) => {
209
+ return match.endsWith(";") ? match : `${match};`;
210
+ });
198
211
  }
212
+ /**
213
+ * 处理 :host 选择器,将其转换为适合组件根节点的选择器
214
+ * @param {string} selector - 包含 :host 的选择器
215
+ * @param {string} moduleId - 组件的模块ID
216
+ * @returns {string} - 转换后的选择器
217
+ */
199
218
  function processHostSelector(selector, moduleId) {
200
- 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
+ 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}]`);
201
220
  }
202
- export {
203
- compileSS,
204
- ensureImportSemicolons,
205
- processHostSelector
206
- };
221
+ //#endregion
222
+ export { compileSS, ensureImportSemicolons, normalizeCssUrlValue, normalizeRootStyleImports, processHostSelector, removeBaseComponentScope, resolveStyleImportPath };