@dimina/compiler 1.0.6 → 1.0.8

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,16 +1,35 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
3
  const fs = require("node:fs");
3
4
  const path = require("node:path");
4
5
  const node_worker_threads = require("node:worker_threads");
5
- const postcss = require("postcss");
6
+ const compilerSfc = require("@vue/compiler-sfc");
6
7
  const autoprefixer = require("autoprefixer");
7
- const selectorParser = require("postcss-selector-parser");
8
8
  const cssnano = require("cssnano");
9
- const compilerSfc = require("@vue/compiler-sfc");
10
- const env = require("../env-Chow6VXH.cjs");
11
- const fileType = [".wxss", ".ddss"];
9
+ const less = require("less");
10
+ const postcss = require("postcss");
11
+ const selectorParser = require("postcss-selector-parser");
12
+ const sass = require("sass");
13
+ const env = require("../env-Cmen1qwy.cjs");
14
+ function _interopNamespaceDefault(e) {
15
+ const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
16
+ if (e) {
17
+ for (const k in e) {
18
+ if (k !== "default") {
19
+ const d = Object.getOwnPropertyDescriptor(e, k);
20
+ Object.defineProperty(n, k, d.get ? d : {
21
+ enumerable: true,
22
+ get: () => e[k]
23
+ });
24
+ }
25
+ }
26
+ }
27
+ n.default = e;
28
+ return Object.freeze(n);
29
+ }
30
+ const sass__namespace = /* @__PURE__ */ _interopNamespaceDefault(sass);
31
+ const fileType = [".wxss", ".ddss", ".less", ".scss", ".sass"];
12
32
  const compileRes = /* @__PURE__ */ new Map();
13
- const processedModules = /* @__PURE__ */ new Set();
14
33
  if (!node_worker_threads.isMainThread) {
15
34
  node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
16
35
  try {
@@ -42,13 +61,13 @@ async function compileSS(pages, root, progress) {
42
61
  if (root) {
43
62
  const subDir = `${env.getTargetPath()}/${root}`;
44
63
  if (!fs.existsSync(subDir)) {
45
- fs.mkdirSync(subDir);
64
+ fs.mkdirSync(subDir, { recursive: true });
46
65
  }
47
66
  fs.writeFileSync(`${subDir}/${filename}.css`, code);
48
67
  } else {
49
68
  const mainDir = `${env.getTargetPath()}/main`;
50
69
  if (!fs.existsSync(mainDir)) {
51
- fs.mkdirSync(mainDir);
70
+ fs.mkdirSync(mainDir, { recursive: true });
52
71
  }
53
72
  fs.writeFileSync(`${mainDir}/${filename}.css`, code);
54
73
  }
@@ -58,10 +77,12 @@ async function compileSS(pages, root, progress) {
58
77
  async function buildCompileCss(module2, depthChain = []) {
59
78
  const currentPath = module2.path;
60
79
  if (depthChain.includes(currentPath)) {
61
- console.warn(`检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
80
+ console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
81
+ return;
62
82
  }
63
- if (depthChain.length > 100) {
64
- console.warn(`检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
83
+ if (depthChain.length > 20) {
84
+ console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
85
+ return;
65
86
  }
66
87
  depthChain = [...depthChain, currentPath];
67
88
  let result = await enhanceCSS(module2) || "";
@@ -74,7 +95,6 @@ async function buildCompileCss(module2, depthChain = []) {
74
95
  result += await buildCompileCss(componentModule, depthChain);
75
96
  }
76
97
  }
77
- processedModules.add(currentPath);
78
98
  return result;
79
99
  }
80
100
  async function enhanceCSS(module2) {
@@ -89,10 +109,34 @@ async function enhanceCSS(module2) {
89
109
  if (compileRes.has(absolutePath)) {
90
110
  return compileRes.get(absolutePath);
91
111
  }
92
- const fixedCSS = inputCSS.replace(/@import[^;\n]*$/gm, (match) => {
93
- return match.endsWith(";") ? match : `${match};`;
94
- });
95
- const ast = postcss.parse(fixedCSS);
112
+ let processedCSS = inputCSS;
113
+ const ext = path.extname(absolutePath).toLowerCase();
114
+ try {
115
+ if (ext === ".less") {
116
+ const result2 = await less.render(inputCSS, {
117
+ filename: absolutePath,
118
+ paths: [path.dirname(absolutePath)]
119
+ });
120
+ processedCSS = result2.css;
121
+ } else if (ext === ".scss" || ext === ".sass") {
122
+ const result2 = sass__namespace.compileString(inputCSS, {
123
+ loadPaths: [path.dirname(absolutePath)],
124
+ syntax: ext === ".sass" ? "indented" : "scss"
125
+ });
126
+ processedCSS = result2.css;
127
+ }
128
+ } catch (error) {
129
+ console.error(`[style] 预处理器编译失败 ${absolutePath}:`, error.message);
130
+ processedCSS = inputCSS;
131
+ }
132
+ const fixedCSS = ensureImportSemicolons(processedCSS);
133
+ let ast;
134
+ try {
135
+ ast = postcss.parse(fixedCSS);
136
+ } catch (error) {
137
+ console.error(`[style] PostCSS 解析失败 ${absolutePath}:`, error.message);
138
+ return "";
139
+ }
96
140
  const promises = [];
97
141
  ast.walk(async (node) => {
98
142
  if (node.type === "atrule" && node.name === "import") {
@@ -104,6 +148,9 @@ async function enhanceCSS(module2) {
104
148
  if (node.selector.includes("::v-deep")) {
105
149
  node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
106
150
  }
151
+ if (node.selector.includes(":host")) {
152
+ node.selector = processHostSelector(node.selector, module2.id);
153
+ }
107
154
  node.selector = selectorParser((selectors) => {
108
155
  selectors.walkTags((tag) => {
109
156
  if (env.tagWhiteList.includes(tag.value)) {
@@ -129,10 +176,11 @@ async function enhanceCSS(module2) {
129
176
  }
130
177
  });
131
178
  const cssCode = ast.toResult().css;
179
+ const moduleId = module2.id;
132
180
  const code = compilerSfc.compileStyle({
133
181
  source: cssCode,
134
- id: module2.id,
135
- scoped: !!module2.id
182
+ id: moduleId,
183
+ scoped: !!moduleId
136
184
  }).code;
137
185
  const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(code, {
138
186
  from: void 0
@@ -153,4 +201,14 @@ function getAbsolutePath(modulePath) {
153
201
  }
154
202
  }
155
203
  }
156
- module.exports = compileSS;
204
+ function ensureImportSemicolons(css) {
205
+ return css.replace(/@import[^;\n]*$/gm, (match) => {
206
+ return match.endsWith(";") ? match : `${match};`;
207
+ });
208
+ }
209
+ function processHostSelector(selector, moduleId) {
210
+ 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}]`);
211
+ }
212
+ exports.compileSS = compileSS;
213
+ exports.ensureImportSemicolons = ensureImportSemicolons;
214
+ exports.processHostSelector = processHostSelector;
@@ -1,15 +1,16 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { isMainThread, parentPort } from "node:worker_threads";
4
- import postcss from "postcss";
4
+ import { compileStyle } from "@vue/compiler-sfc";
5
5
  import autoprefixer from "autoprefixer";
6
- import selectorParser from "postcss-selector-parser";
7
6
  import cssnano from "cssnano";
8
- import { compileStyle } from "@vue/compiler-sfc";
9
- import { r as resetStoreInfo, g as getTargetPath, a as getComponent, b as getContentByPath, h as tagWhiteList, d as collectAssets, e as getAppId, f as getWorkPath, t as transformRpx } from "../env-CezfCSQz.js";
10
- const fileType = [".wxss", ".ddss"];
7
+ import less from "less";
8
+ import postcss from "postcss";
9
+ import selectorParser from "postcss-selector-parser";
10
+ 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"];
11
13
  const compileRes = /* @__PURE__ */ new Map();
12
- const processedModules = /* @__PURE__ */ new Set();
13
14
  if (!isMainThread) {
14
15
  parentPort.on("message", async ({ pages, storeInfo }) => {
15
16
  try {
@@ -41,13 +42,13 @@ async function compileSS(pages, root, progress) {
41
42
  if (root) {
42
43
  const subDir = `${getTargetPath()}/${root}`;
43
44
  if (!fs.existsSync(subDir)) {
44
- fs.mkdirSync(subDir);
45
+ fs.mkdirSync(subDir, { recursive: true });
45
46
  }
46
47
  fs.writeFileSync(`${subDir}/${filename}.css`, code);
47
48
  } else {
48
49
  const mainDir = `${getTargetPath()}/main`;
49
50
  if (!fs.existsSync(mainDir)) {
50
- fs.mkdirSync(mainDir);
51
+ fs.mkdirSync(mainDir, { recursive: true });
51
52
  }
52
53
  fs.writeFileSync(`${mainDir}/${filename}.css`, code);
53
54
  }
@@ -57,10 +58,12 @@ async function compileSS(pages, root, progress) {
57
58
  async function buildCompileCss(module, depthChain = []) {
58
59
  const currentPath = module.path;
59
60
  if (depthChain.includes(currentPath)) {
60
- console.warn(`检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
61
+ console.warn("[style]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
62
+ return;
61
63
  }
62
- if (depthChain.length > 100) {
63
- console.warn(`检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
64
+ if (depthChain.length > 20) {
65
+ console.warn("[style]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
66
+ return;
64
67
  }
65
68
  depthChain = [...depthChain, currentPath];
66
69
  let result = await enhanceCSS(module) || "";
@@ -73,7 +76,6 @@ async function buildCompileCss(module, depthChain = []) {
73
76
  result += await buildCompileCss(componentModule, depthChain);
74
77
  }
75
78
  }
76
- processedModules.add(currentPath);
77
79
  return result;
78
80
  }
79
81
  async function enhanceCSS(module) {
@@ -88,10 +90,34 @@ async function enhanceCSS(module) {
88
90
  if (compileRes.has(absolutePath)) {
89
91
  return compileRes.get(absolutePath);
90
92
  }
91
- const fixedCSS = inputCSS.replace(/@import[^;\n]*$/gm, (match) => {
92
- return match.endsWith(";") ? match : `${match};`;
93
- });
94
- const ast = postcss.parse(fixedCSS);
93
+ let processedCSS = inputCSS;
94
+ const ext = path.extname(absolutePath).toLowerCase();
95
+ try {
96
+ if (ext === ".less") {
97
+ const result2 = await less.render(inputCSS, {
98
+ filename: absolutePath,
99
+ paths: [path.dirname(absolutePath)]
100
+ });
101
+ processedCSS = result2.css;
102
+ } else if (ext === ".scss" || ext === ".sass") {
103
+ const result2 = sass.compileString(inputCSS, {
104
+ loadPaths: [path.dirname(absolutePath)],
105
+ syntax: ext === ".sass" ? "indented" : "scss"
106
+ });
107
+ processedCSS = result2.css;
108
+ }
109
+ } catch (error) {
110
+ console.error(`[style] 预处理器编译失败 ${absolutePath}:`, error.message);
111
+ processedCSS = inputCSS;
112
+ }
113
+ const fixedCSS = ensureImportSemicolons(processedCSS);
114
+ let ast;
115
+ try {
116
+ ast = postcss.parse(fixedCSS);
117
+ } catch (error) {
118
+ console.error(`[style] PostCSS 解析失败 ${absolutePath}:`, error.message);
119
+ return "";
120
+ }
95
121
  const promises = [];
96
122
  ast.walk(async (node) => {
97
123
  if (node.type === "atrule" && node.name === "import") {
@@ -103,6 +129,9 @@ async function enhanceCSS(module) {
103
129
  if (node.selector.includes("::v-deep")) {
104
130
  node.selector = node.selector.replace(/::v-deep\s+(\S[^{]*)/g, ":deep($1)");
105
131
  }
132
+ if (node.selector.includes(":host")) {
133
+ node.selector = processHostSelector(node.selector, module.id);
134
+ }
106
135
  node.selector = selectorParser((selectors) => {
107
136
  selectors.walkTags((tag) => {
108
137
  if (tagWhiteList.includes(tag.value)) {
@@ -128,10 +157,11 @@ async function enhanceCSS(module) {
128
157
  }
129
158
  });
130
159
  const cssCode = ast.toResult().css;
160
+ const moduleId = module.id;
131
161
  const code = compileStyle({
132
162
  source: cssCode,
133
- id: module.id,
134
- scoped: !!module.id
163
+ id: moduleId,
164
+ scoped: !!moduleId
135
165
  }).code;
136
166
  const res = await postcss([autoprefixer({ overrideBrowserslist: ["cover 99.5%"] }), cssnano()]).process(code, {
137
167
  from: void 0
@@ -152,6 +182,16 @@ function getAbsolutePath(modulePath) {
152
182
  }
153
183
  }
154
184
  }
185
+ function ensureImportSemicolons(css) {
186
+ return css.replace(/@import[^;\n]*$/gm, (match) => {
187
+ return match.endsWith(";") ? match : `${match};`;
188
+ });
189
+ }
190
+ function processHostSelector(selector, moduleId) {
191
+ 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}]`);
192
+ }
155
193
  export {
156
- compileSS as default
194
+ compileSS,
195
+ ensureImportSemicolons,
196
+ processHostSelector
157
197
  };