@vureact/compiler-core 1.5.0 → 1.5.1

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,5 +1,5 @@
1
1
  /**
2
- * @vureact/compiler-core v1.5.0
2
+ * @vureact/compiler-core v1.5.1
3
3
  * (c) 2025-present Ruihong Zhong (Ryan John)
4
4
  * @license MIT
5
5
  */
@@ -1385,7 +1385,12 @@ function generateComponent(ir, ctx, options) {
1385
1385
  quotes: "single"
1386
1386
  // 使用单引号
1387
1387
  },
1388
- minified: true,
1388
+ minified: false,
1389
+ // 不压缩输出内容
1390
+ retainLines: false,
1391
+ // 不保留行号
1392
+ retainFunctionParens: true,
1393
+ // 保留函数括号
1389
1394
  ...options
1390
1395
  });
1391
1396
  const result = {
@@ -1461,6 +1466,7 @@ function resolveScript(descriptor, ctx, pResult) {
1461
1466
  source: scriptBlock
1462
1467
  };
1463
1468
  const options = getBabelParseOptions(scriptBlock.lang, "script", ctx.filename);
1469
+ resolveCtxData(scriptBlock, ctx);
1464
1470
  if (descriptor.script) {
1465
1471
  const { code, ast, name } = extractSetupBodyToTopLevel(scriptBlock.content, options);
1466
1472
  if (ast) {
@@ -1480,12 +1486,21 @@ function resolveScript(descriptor, ctx, pResult) {
1480
1486
  logger.warn(msg, { file: ctx.filename });
1481
1487
  });
1482
1488
  }
1483
- const source = scriptBlock.content;
1484
- ctx.scriptData.source = source;
1485
- ctx.compName = extractCompName(source);
1486
- ctx.scriptData.lang = scriptBlock.lang || "js";
1487
1489
  pResult.script = result;
1488
1490
  }
1491
+ function resolveCtxData(scriptBlock, ctx) {
1492
+ let { content, lang } = scriptBlock;
1493
+ const resolveVRComment = (source) => {
1494
+ const regx = /\/\/\s*@vr-name:\s*(\w+)/;
1495
+ const nameMatch = source.match(regx);
1496
+ content = content.replace(regx, "");
1497
+ return nameMatch?.[1]?.trim() || "";
1498
+ };
1499
+ ctx.compName = resolveVRComment(content);
1500
+ ctx.scriptData.source = content;
1501
+ ctx.scriptData.lang = lang || "js";
1502
+ scriptBlock.content = content;
1503
+ }
1489
1504
  function extractSetupBodyToTopLevel(content, options) {
1490
1505
  let name = "";
1491
1506
  try {
@@ -1549,10 +1564,6 @@ function extractSetupBodyToTopLevel(content, options) {
1549
1564
  return { name, code: content };
1550
1565
  }
1551
1566
  }
1552
- function extractCompName(source) {
1553
- const nameMatch = source.match(/@vr-name:\s*(\w+)/);
1554
- return nameMatch?.[1]?.trim() || "";
1555
- }
1556
1567
 
1557
1568
  // src/core/parse/sfc/process/resolve-script-meta.ts
1558
1569
  import { traverse } from "@babel/core";
@@ -1936,265 +1947,8 @@ import { parse as babelParse3 } from "@babel/parser";
1936
1947
  // src/core/transform/sfc/script/syntax-processor/index.ts
1937
1948
  import { traverse as traverse3 } from "@babel/core";
1938
1949
 
1939
- // src/core/transform/sfc/script/syntax-processor/postprocess/insert-css-import.ts
1940
- import * as t17 from "@babel/types";
1941
- function insertCSSImport(ctx) {
1942
- const { inputType } = ctx;
1943
- if (inputType !== "sfc") return;
1944
- const { filePath, moduleName } = ctx.styleData;
1945
- if (!filePath) return;
1946
- const scriptIR = getScriptIR(ctx);
1947
- const filename = normalizePath(filePath).split("/").pop();
1948
- const importPath = `./${filename}`;
1949
- const importDecl = t17.importDeclaration(
1950
- !moduleName ? [] : [t17.importDefaultSpecifier(t17.identifier(moduleName))],
1951
- t17.stringLiteral(importPath)
1952
- );
1953
- scriptIR.imports.push(importDecl);
1954
- }
1955
-
1956
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-required-imports/index.ts
1957
- import * as t18 from "@babel/types";
1958
-
1959
- // src/core/transform/shared.ts
1960
- function recordImport(ctx, pkg, name, onDemand = true) {
1961
- const { imports } = ctx;
1962
- if (isTypeOnlyImport(name)) {
1963
- name = `type ${name}`;
1964
- }
1965
- if (imports.has(pkg)) {
1966
- const list = imports.get(pkg);
1967
- const foundItem = list.find((item) => item.name === name);
1968
- if (!foundItem) {
1969
- list.push({ name, onDemand });
1970
- }
1971
- return;
1972
- }
1973
- imports.set(pkg, [{ name, onDemand }]);
1974
- }
1975
- function isTypeOnlyImport(name) {
1976
- const arr = [REACT_API_MAP.ReactNode];
1977
- return arr.includes(name);
1978
- }
1979
-
1980
- // src/core/transform/sfc/script/shared/replace-vue-suffix.ts
1981
- function replaceVueSuffix(ctx, node) {
1982
- if (!node.value.endsWith(".vue")) return;
1983
- const jsxFile = node.value.replace(/.vue$/, "");
1984
- node.value = jsxFile;
1985
- node.extra = { rawValue: jsxFile, raw: jsxFile };
1986
- }
1987
-
1988
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-required-imports/import-strategies.ts
1989
- var VueRouterStrategy = class {
1990
- matches(moduleName) {
1991
- return moduleName === "vue-router" || moduleName.startsWith("vue-router/");
1992
- }
1993
- process() {
1994
- return {
1995
- shouldReplaceSource: true,
1996
- newSource: PACKAGE_NAME.router,
1997
- shouldRemove: false,
1998
- shouldInjectRuntimeImports: false
1999
- };
2000
- }
2001
- };
2002
- var VueEcosystemStrategy = class {
2003
- matches(moduleName) {
2004
- if (moduleName.startsWith(".") || moduleName.startsWith("/") || moduleName.startsWith("file:")) {
2005
- return false;
2006
- }
2007
- if (moduleName === "vue-router" || moduleName.startsWith("vue-router/")) {
2008
- return false;
2009
- }
2010
- if (moduleName.startsWith("@vue/")) {
2011
- return true;
2012
- }
2013
- for (const pkg of VUE_PACKAGES) {
2014
- if (moduleName === pkg || moduleName.startsWith(`${pkg}/`)) {
2015
- return true;
2016
- }
2017
- }
2018
- return false;
2019
- }
2020
- process() {
2021
- return {
2022
- shouldReplaceSource: false,
2023
- shouldRemove: true,
2024
- shouldInjectRuntimeImports: true
2025
- };
2026
- }
2027
- };
2028
- var StyleFileStrategy = class {
2029
- regExp = /\.(less|sass|scss)$/i;
2030
- matches(moduleName) {
2031
- return this.regExp.test(moduleName);
2032
- }
2033
- process(path9, ctx) {
2034
- if (!ctx.preprocessStyles) {
2035
- return {};
2036
- }
2037
- const importSource = path9.node.source.value;
2038
- if (typeof importSource !== "string") {
2039
- return {};
2040
- }
2041
- return {
2042
- shouldReplaceSource: true,
2043
- newSource: importSource.replace(this.regExp, ".css"),
2044
- shouldRemove: false,
2045
- shouldInjectRuntimeImports: false
2046
- };
2047
- }
2048
- };
2049
-
2050
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-required-imports/import-strategy-manager.ts
2051
- var ImportStrategyManager = class {
2052
- strategies = [];
2053
- constructor() {
2054
- this.strategies.push(new VueRouterStrategy());
2055
- this.strategies.push(new VueEcosystemStrategy());
2056
- this.strategies.push(new StyleFileStrategy());
2057
- }
2058
- /** 添加自定义策略 */
2059
- addStrategy(strategy) {
2060
- this.strategies.push(strategy);
2061
- }
2062
- /** 查找匹配的策略 */
2063
- findStrategy(moduleName) {
2064
- for (const strategy of this.strategies) {
2065
- if (strategy.matches(moduleName)) {
2066
- return strategy;
2067
- }
2068
- }
2069
- return null;
2070
- }
2071
- };
2072
-
2073
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-required-imports/index.ts
2074
- function resolveRequiredImports(ctx) {
2075
- const processedModules = /* @__PURE__ */ new Set();
2076
- let hasProcessedImports = false;
2077
- const strategyManager = new ImportStrategyManager();
2078
- if (ctx.inputType === "sfc") {
2079
- recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.memo);
2080
- }
2081
- function resolveRequiredImport(path9) {
2082
- const { node } = path9;
2083
- const originalModuleName = node.source.value.toLowerCase();
2084
- const strategy = strategyManager.findStrategy(originalModuleName);
2085
- if (strategy) {
2086
- const result = strategy.process(path9, ctx, originalModuleName);
2087
- if (result.shouldReplaceSource && result.newSource) {
2088
- node.source.value = result.newSource;
2089
- }
2090
- }
2091
- const normalizedModuleName = node.source.value.toLowerCase();
2092
- mergeImports(node, ctx, normalizedModuleName);
2093
- if (processedModules.has(normalizedModuleName) && !path9.removed) {
2094
- path9.remove();
2095
- return;
2096
- }
2097
- processedModules.add(normalizedModuleName);
2098
- if (!hasProcessedImports) {
2099
- const required = createRequiredImports(ctx);
2100
- if (strategy) {
2101
- const result = strategy.process(path9, ctx, originalModuleName);
2102
- if (result.shouldRemove) {
2103
- path9.replaceWithMultiple(required);
2104
- } else if (normalizedModuleName === PACKAGE_NAME.react) {
2105
- path9.insertAfter(required);
2106
- } else {
2107
- path9.insertBefore(required);
2108
- }
2109
- } else {
2110
- if (normalizedModuleName === PACKAGE_NAME.react) {
2111
- path9.insertAfter(required);
2112
- } else {
2113
- path9.insertBefore(required);
2114
- }
2115
- }
2116
- hasProcessedImports = true;
2117
- }
2118
- if (strategy) {
2119
- const result = strategy.process(path9, ctx, originalModuleName);
2120
- if (result.shouldRemove && !path9.removed) {
2121
- path9.remove();
2122
- return;
2123
- }
2124
- }
2125
- replaceVueSuffix(ctx, node.source);
2126
- }
2127
- return {
2128
- // 兜底:无 ImportDeclaration 的文件也要能注入必需依赖。
2129
- Program: {
2130
- exit(path9) {
2131
- if (hasProcessedImports) return;
2132
- const required = createRequiredImports(ctx);
2133
- path9.unshiftContainer("body", required);
2134
- hasProcessedImports = true;
2135
- }
2136
- },
2137
- ImportDeclaration(path9) {
2138
- resolveRequiredImport(path9);
2139
- }
2140
- };
2141
- }
2142
- function mergeImports(currentNode, ctx, moduleName) {
2143
- const ctxImportItems = ctx.imports.get(moduleName);
2144
- if (!ctxImportItems?.length) {
2145
- return;
2146
- }
2147
- const currentImports = /* @__PURE__ */ new Set();
2148
- for (const spec of currentNode.specifiers) {
2149
- if (t18.isImportSpecifier(spec) && t18.isIdentifier(spec.imported)) {
2150
- currentImports.add(spec.imported.name);
2151
- }
2152
- if (t18.isImportDefaultSpecifier(spec) && t18.isIdentifier(spec.local)) {
2153
- currentImports.add(spec.local.name);
2154
- }
2155
- }
2156
- for (const item of ctxImportItems) {
2157
- if (currentImports.has(item.name)) {
2158
- continue;
2159
- }
2160
- const local = t18.identifier(item.name);
2161
- const newNode = !item.onDemand ? t18.importDefaultSpecifier(local) : t18.importSpecifier(local, local);
2162
- currentNode.specifiers.push(newNode);
2163
- }
2164
- ctx.imports.delete(moduleName);
2165
- }
2166
- function createRequiredImports(ctx) {
2167
- const result = [];
2168
- const importMap = {};
2169
- ctx.imports.forEach((items, moduleName) => {
2170
- const specifier = [];
2171
- for (const item of items) {
2172
- const local = t18.identifier(item.name);
2173
- if (!item.onDemand) {
2174
- specifier.push(t18.importDefaultSpecifier(local));
2175
- } else {
2176
- specifier.push(t18.importSpecifier(local, local));
2177
- }
2178
- }
2179
- importMap[moduleName] = specifier;
2180
- });
2181
- for (const name in importMap) {
2182
- const specifiers = importMap[name];
2183
- const importDecl = t18.importDeclaration(specifiers, t18.stringLiteral(name));
2184
- if (name === PACKAGE_NAME.react) {
2185
- result.unshift(importDecl);
2186
- } else {
2187
- result.push(importDecl);
2188
- }
2189
- }
2190
- return result;
2191
- }
2192
-
2193
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-static-hoisting.ts
2194
- import * as t20 from "@babel/types";
2195
-
2196
1950
  // src/core/transform/sfc/script/shared/babel-utils.ts
2197
- import * as t19 from "@babel/types";
1951
+ import * as t17 from "@babel/types";
2198
1952
  function findRootVariablePath(path9) {
2199
1953
  const rootId = findRootIdentifier(path9.node);
2200
1954
  if (!rootId?.name) return null;
@@ -2205,10 +1959,10 @@ function findRootVariablePath(path9) {
2205
1959
  }
2206
1960
  function findRootIdentifier(node) {
2207
1961
  let current = node.object;
2208
- while (t19.isMemberExpression(current) || t19.isOptionalMemberExpression(current)) {
1962
+ while (t17.isMemberExpression(current) || t17.isOptionalMemberExpression(current)) {
2209
1963
  current = current.object;
2210
1964
  }
2211
- return t19.isIdentifier(current) ? current : null;
1965
+ return t17.isIdentifier(current) ? current : null;
2212
1966
  }
2213
1967
  function getVariableDeclaratorPath(path9) {
2214
1968
  if (path9.isVariableDeclarator()) {
@@ -2292,14 +2046,14 @@ function isPropertyName(path9) {
2292
2046
  }
2293
2047
  function replaceCallName(callExp, identifierName) {
2294
2048
  const { callee } = callExp;
2295
- if (!t19.isIdentifier(callee)) return;
2049
+ if (!t17.isIdentifier(callee)) return;
2296
2050
  callee.name = identifierName;
2297
2051
  if (callee.loc) {
2298
2052
  callee.loc.identifierName = identifierName;
2299
2053
  }
2300
2054
  }
2301
2055
  function replaceIdName(id, newName) {
2302
- if (!t19.isIdentifier(id)) return;
2056
+ if (!t17.isIdentifier(id)) return;
2303
2057
  id.name = newName;
2304
2058
  if (id.loc) {
2305
2059
  id.loc.identifierName = newName;
@@ -2309,69 +2063,110 @@ function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
2309
2063
  const { filename, scriptData } = ctx;
2310
2064
  const exp = stringToExpr(input, scriptData.lang, filename);
2311
2065
  const ts = expressionToTSType(exp);
2312
- return tsTypeAnnotation7 ? t19.tsTypeAnnotation(ts) : ts;
2066
+ return tsTypeAnnotation7 ? t17.tsTypeAnnotation(ts) : ts;
2313
2067
  }
2314
2068
  function expressionToTSType(exp) {
2315
- if (t19.isStringLiteral(exp)) return t19.tsStringKeyword();
2316
- if (t19.isNumericLiteral(exp)) return t19.tsNumberKeyword();
2317
- if (t19.isBooleanLiteral(exp)) return t19.tsBooleanKeyword();
2318
- if (t19.isArrayExpression(exp)) return t19.tsArrayType(t19.tsAnyKeyword());
2319
- if (t19.isObjectExpression(exp)) {
2069
+ if (t17.isStringLiteral(exp)) return t17.tsStringKeyword();
2070
+ if (t17.isNumericLiteral(exp)) return t17.tsNumberKeyword();
2071
+ if (t17.isBooleanLiteral(exp)) return t17.tsBooleanKeyword();
2072
+ if (t17.isArrayExpression(exp)) return t17.tsArrayType(t17.tsAnyKeyword());
2073
+ if (t17.isObjectExpression(exp)) {
2320
2074
  const members = [];
2321
2075
  for (const p of exp.properties) {
2322
- if (!t19.isObjectProperty(p)) continue;
2076
+ if (!t17.isObjectProperty(p)) continue;
2323
2077
  let key;
2324
- if (t19.isIdentifier(p.key)) key = p.key.name;
2325
- else if (t19.isStringLiteral(p.key)) key = p.key.value;
2078
+ if (t17.isIdentifier(p.key)) key = p.key.name;
2079
+ else if (t17.isStringLiteral(p.key)) key = p.key.value;
2326
2080
  if (!key) continue;
2327
- if (t19.isExpression(p.value)) {
2081
+ if (t17.isExpression(p.value)) {
2328
2082
  members.push(
2329
- t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(expressionToTSType(p.value)))
2083
+ t17.tsPropertySignature(t17.identifier(key), t17.tsTypeAnnotation(expressionToTSType(p.value)))
2330
2084
  );
2331
2085
  } else {
2332
2086
  members.push(
2333
- t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(t19.tsAnyKeyword()))
2087
+ t17.tsPropertySignature(t17.identifier(key), t17.tsTypeAnnotation(t17.tsAnyKeyword()))
2334
2088
  );
2335
2089
  }
2336
2090
  }
2337
- return t19.tsTypeLiteral(members);
2091
+ return t17.tsTypeLiteral(members);
2338
2092
  }
2339
- if (t19.isArrowFunctionExpression(exp) || t19.isFunctionExpression(exp)) {
2093
+ if (t17.isArrowFunctionExpression(exp) || t17.isFunctionExpression(exp)) {
2340
2094
  const params = exp.params.map((p, i) => {
2341
- const id = t19.isIdentifier(p) ? t19.identifier(p.name) : t19.identifier(`arg${i}`);
2342
- id.typeAnnotation = t19.tsTypeAnnotation(t19.tsAnyKeyword());
2095
+ const id = t17.isIdentifier(p) ? t17.identifier(p.name) : t17.identifier(`arg${i}`);
2096
+ id.typeAnnotation = t17.tsTypeAnnotation(t17.tsAnyKeyword());
2343
2097
  return id;
2344
2098
  });
2345
- let returnType = t19.tsAnyKeyword();
2346
- if (t19.isBlockStatement(exp.body)) {
2099
+ let returnType = t17.tsAnyKeyword();
2100
+ if (t17.isBlockStatement(exp.body)) {
2347
2101
  for (const stmt of exp.body.body) {
2348
- if (t19.isReturnStatement(stmt) && stmt.argument) {
2349
- if (t19.isExpression(stmt.argument)) {
2102
+ if (t17.isReturnStatement(stmt) && stmt.argument) {
2103
+ if (t17.isExpression(stmt.argument)) {
2350
2104
  returnType = expressionToTSType(stmt.argument);
2351
2105
  break;
2352
2106
  }
2353
2107
  }
2354
2108
  }
2355
- } else if (t19.isExpression(exp.body)) {
2109
+ } else if (t17.isExpression(exp.body)) {
2356
2110
  returnType = expressionToTSType(exp.body);
2357
2111
  }
2358
- return t19.tsFunctionType(null, params, t19.tsTypeAnnotation(returnType));
2112
+ return t17.tsFunctionType(null, params, t17.tsTypeAnnotation(returnType));
2359
2113
  }
2360
- return t19.tsAnyKeyword();
2114
+ return t17.tsAnyKeyword();
2361
2115
  }
2362
2116
  function isCalleeNamed(node, name) {
2363
- if (!t19.isIdentifier(node.callee)) {
2117
+ if (!t17.isIdentifier(node.callee)) {
2364
2118
  return false;
2365
2119
  }
2366
2120
  return node.callee.name === name;
2367
2121
  }
2368
2122
  function isSimpleLiteral(node) {
2369
2123
  if (!node) return false;
2370
- if (t19.isStringLiteral(node) || t19.isNumericLiteral(node) || t19.isBooleanLiteral(node) || t19.isNullLiteral(node) || t19.isRegExpLiteral(node) || t19.isBigIntLiteral(node) || t19.isDecimalLiteral(node)) {
2124
+ if (t17.isStringLiteral(node) || t17.isNumericLiteral(node) || t17.isBooleanLiteral(node) || t17.isNullLiteral(node) || t17.isRegExpLiteral(node) || t17.isBigIntLiteral(node) || t17.isDecimalLiteral(node)) {
2371
2125
  return true;
2372
2126
  }
2373
2127
  return false;
2374
2128
  }
2129
+ function forkNode(node, deep = true) {
2130
+ const newNode = t17.cloneNode(node, deep);
2131
+ newNode.leadingComments = node.leadingComments;
2132
+ newNode.innerComments = node.innerComments;
2133
+ newNode.trailingComments = null;
2134
+ cleanNodeLoc(node);
2135
+ cleanNodeComments(node);
2136
+ return newNode;
2137
+ }
2138
+ function cleanNodeLoc(node) {
2139
+ node.start = null;
2140
+ node.end = null;
2141
+ node.loc = null;
2142
+ }
2143
+ function cleanNodeComments(node) {
2144
+ node.leadingComments = null;
2145
+ node.innerComments = null;
2146
+ node.trailingComments = null;
2147
+ }
2148
+
2149
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-global-type-chunk.ts
2150
+ function resolveGlobalTypeChunks(path9, ir) {
2151
+ if (!path9.parentPath?.isProgram()) {
2152
+ return;
2153
+ }
2154
+ const forked = forkNode(path9.node);
2155
+ ir.tsTypes.push(forked);
2156
+ path9.remove();
2157
+ }
2158
+
2159
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-module-chunk.ts
2160
+ import * as t18 from "@babel/types";
2161
+ function resolveModuleChunks(path9, ir) {
2162
+ const forked = forkNode(path9.node);
2163
+ if (t18.isImportDeclaration(forked)) {
2164
+ ir.imports.push(forked);
2165
+ } else if (t18.isExportDeclaration(forked)) {
2166
+ ir.exports.push(forked);
2167
+ }
2168
+ path9.remove();
2169
+ }
2375
2170
 
2376
2171
  // src/core/transform/sfc/script/shared/metadata-utils.ts
2377
2172
  var META_KEY = "__vureact_metadata";
@@ -2389,43 +2184,289 @@ function setScriptNodeMeta(node, opts) {
2389
2184
  node[META_KEY] = opts;
2390
2185
  }
2391
2186
 
2392
- // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-static-hoisting.ts
2393
- function resolveStaticHoisting(ctx) {
2187
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/resolve-static-const-chunk.ts
2188
+ function resolveStaticConstChunks(path9, ir) {
2189
+ const { node, parentPath } = path9;
2190
+ const parent = parentPath.node;
2191
+ if (!isVariableDeclTopLevel(path9) || !parentPath.isVariableDeclaration() || parent.kind !== "const" || !isSimpleLiteral(node.init) || getScriptNodeMeta(node)) {
2192
+ return;
2193
+ }
2194
+ const forked = forkNode(parent);
2195
+ ir.statement.global.push(forked);
2196
+ parentPath.remove();
2197
+ }
2198
+
2199
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-ast-chunks/index.ts
2200
+ function resolveASTChunks(ctx, ast) {
2394
2201
  const scriptIR = getScriptIR(ctx);
2395
2202
  if (ctx.inputType !== "sfc") {
2396
2203
  return {};
2397
2204
  }
2205
+ scriptIR.statement.local = ast;
2398
2206
  return {
2207
+ // 提取 import/export
2399
2208
  "ImportDeclaration|ExportDeclaration"(path9) {
2400
- if (t20.isImportDeclaration(path9.node)) {
2401
- scriptIR.imports.push(path9.node);
2402
- } else if (t20.isExportDeclaration(path9.node)) {
2403
- scriptIR.exports.push(path9.node);
2404
- }
2405
- path9.remove();
2209
+ resolveModuleChunks(path9, scriptIR);
2406
2210
  },
2211
+ // 提取全局类型声明
2407
2212
  "TSInterfaceDeclaration|TSTypeAliasDeclaration|TSEnumDeclaration|TSModuleDeclaration|TSModuleDeclaration"(path9) {
2408
- if (t20.isProgram(path9.parent)) {
2409
- scriptIR.tsTypes.push(path9.node);
2410
- path9.remove();
2411
- }
2213
+ resolveGlobalTypeChunks(path9, scriptIR);
2412
2214
  },
2215
+ // 提升顶层常量声明
2413
2216
  VariableDeclarator(path9) {
2217
+ resolveStaticConstChunks(path9, scriptIR);
2218
+ }
2219
+ };
2220
+ }
2221
+
2222
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/index.ts
2223
+ import * as t19 from "@babel/types";
2224
+
2225
+ // src/core/transform/shared.ts
2226
+ function recordImport(ctx, pkg, name, onDemand = true) {
2227
+ const { imports } = ctx;
2228
+ if (isTypeOnlyImport(name)) {
2229
+ name = `type ${name}`;
2230
+ }
2231
+ if (imports.has(pkg)) {
2232
+ const list = imports.get(pkg);
2233
+ const foundItem = list.find((item) => item.name === name);
2234
+ if (!foundItem) {
2235
+ list.push({ name, onDemand });
2236
+ }
2237
+ return;
2238
+ }
2239
+ imports.set(pkg, [{ name, onDemand }]);
2240
+ }
2241
+ function isTypeOnlyImport(name) {
2242
+ const arr = [REACT_API_MAP.ReactNode];
2243
+ return arr.includes(name);
2244
+ }
2245
+
2246
+ // src/core/transform/sfc/script/shared/replace-vue-suffix.ts
2247
+ function replaceVueSuffix(node) {
2248
+ if (!node.value.endsWith(".vue")) return;
2249
+ const replaced = node.value.replace(/.vue$/, "");
2250
+ node.value = replaced;
2251
+ node.extra = {
2252
+ rawValue: replaced,
2253
+ // fix: 当 minified: true 的情况下,输出内容丢失引号
2254
+ raw: `'${replaced}'`
2255
+ };
2256
+ }
2257
+
2258
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/import-strategies.ts
2259
+ var VueRouterStrategy = class {
2260
+ matches(moduleName) {
2261
+ return moduleName === "vue-router" || moduleName.startsWith("vue-router/");
2262
+ }
2263
+ process() {
2264
+ return {
2265
+ shouldReplaceSource: true,
2266
+ newSource: PACKAGE_NAME.router,
2267
+ shouldRemove: false,
2268
+ shouldInjectRuntimeImports: false
2269
+ };
2270
+ }
2271
+ };
2272
+ var VueEcosystemStrategy = class {
2273
+ matches(moduleName) {
2274
+ if (moduleName.startsWith(".") || moduleName.startsWith("/") || moduleName.startsWith("file:")) {
2275
+ return false;
2276
+ }
2277
+ if (moduleName === "vue-router" || moduleName.startsWith("vue-router/")) {
2278
+ return false;
2279
+ }
2280
+ if (moduleName.startsWith("@vue/")) {
2281
+ return true;
2282
+ }
2283
+ for (const pkg of VUE_PACKAGES) {
2284
+ if (moduleName === pkg || moduleName.startsWith(`${pkg}/`)) {
2285
+ return true;
2286
+ }
2287
+ }
2288
+ return false;
2289
+ }
2290
+ process() {
2291
+ return {
2292
+ shouldReplaceSource: false,
2293
+ shouldRemove: true,
2294
+ shouldInjectRuntimeImports: true
2295
+ };
2296
+ }
2297
+ };
2298
+ var StyleFileStrategy = class {
2299
+ regExp = /\.(less|sass|scss)$/i;
2300
+ matches(moduleName) {
2301
+ return this.regExp.test(moduleName);
2302
+ }
2303
+ process(path9, ctx) {
2304
+ if (!ctx.preprocessStyles) {
2305
+ return {};
2306
+ }
2307
+ const importSource = path9.node.source.value;
2308
+ if (typeof importSource !== "string") {
2309
+ return {};
2310
+ }
2311
+ return {
2312
+ shouldReplaceSource: true,
2313
+ newSource: importSource.replace(this.regExp, ".css"),
2314
+ shouldRemove: false,
2315
+ shouldInjectRuntimeImports: false
2316
+ };
2317
+ }
2318
+ };
2319
+
2320
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/import-strategy-manager.ts
2321
+ var ImportStrategyManager = class {
2322
+ strategies = [];
2323
+ constructor() {
2324
+ this.strategies.push(new VueRouterStrategy());
2325
+ this.strategies.push(new VueEcosystemStrategy());
2326
+ this.strategies.push(new StyleFileStrategy());
2327
+ }
2328
+ /** 添加自定义策略 */
2329
+ addStrategy(strategy) {
2330
+ this.strategies.push(strategy);
2331
+ }
2332
+ /** 查找匹配的策略 */
2333
+ findStrategy(moduleName) {
2334
+ for (const strategy of this.strategies) {
2335
+ if (strategy.matches(moduleName)) {
2336
+ return strategy;
2337
+ }
2338
+ }
2339
+ return null;
2340
+ }
2341
+ };
2342
+
2343
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-runtime-imports/index.ts
2344
+ function resolveRuntimeImports(ctx) {
2345
+ if (ctx.inputType === "sfc") {
2346
+ recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.memo);
2347
+ }
2348
+ const strategyManager = new ImportStrategyManager();
2349
+ const processedModules = /* @__PURE__ */ new Set();
2350
+ let hasImports = false;
2351
+ return {
2352
+ ImportDeclaration(path9) {
2414
2353
  const { node } = path9;
2415
- if (!isVariableDeclTopLevel(path9) || !isSimpleLiteral(node.init) || getScriptNodeMeta(node)) {
2354
+ const originalModuleName = node.source.value.toLowerCase();
2355
+ const strategy = strategyManager.findStrategy(originalModuleName);
2356
+ let strategyResult = null;
2357
+ if (strategy) {
2358
+ strategyResult = strategy.process(path9, ctx, originalModuleName);
2359
+ if (strategyResult.shouldReplaceSource && strategyResult.newSource) {
2360
+ node.source.value = strategyResult.newSource;
2361
+ }
2362
+ if (strategyResult.shouldRemove && !path9.removed) {
2363
+ path9.remove();
2364
+ return;
2365
+ }
2366
+ }
2367
+ replaceVueSuffix(node.source);
2368
+ const finalModuleName = node.source.value.toLowerCase();
2369
+ if (processedModules.has(finalModuleName) && !path9.removed) {
2370
+ path9.remove();
2416
2371
  return;
2417
2372
  }
2418
- const declarationPath = path9.findParent((p) => p.isVariableDeclaration());
2419
- if (!declarationPath) return;
2420
- scriptIR.statement.global.push(declarationPath.node);
2421
- declarationPath.remove();
2373
+ processedModules.add(finalModuleName);
2374
+ mergeImports(node, ctx, finalModuleName);
2375
+ if (!hasImports) {
2376
+ const importNodes = createImportNodes(ctx);
2377
+ if (importNodes.length) {
2378
+ if (strategyResult?.shouldInjectRuntimeImports) {
2379
+ path9.insertAfter(importNodes);
2380
+ } else if (finalModuleName === PACKAGE_NAME.react) {
2381
+ path9.insertAfter(importNodes);
2382
+ } else {
2383
+ forkLeadingComments(importNodes[0], node);
2384
+ path9.insertBefore(importNodes);
2385
+ }
2386
+ }
2387
+ hasImports = true;
2388
+ }
2389
+ },
2390
+ // 兜底:无 ImportDeclaration 的文件也要能注入必需依赖。
2391
+ Program: {
2392
+ exit(path9) {
2393
+ if (hasImports) return;
2394
+ hasImports = true;
2395
+ const { node } = path9;
2396
+ const importNodes = createImportNodes(ctx);
2397
+ if (!importNodes.length) return;
2398
+ forkLeadingComments(importNodes[0], node);
2399
+ path9.unshiftContainer("body", importNodes);
2400
+ }
2422
2401
  }
2423
2402
  };
2424
2403
  }
2425
- function collectLocalStatements(ctx, ast) {
2404
+ function mergeImports(currentNode, ctx, moduleName) {
2405
+ const ctxImportItems = ctx.imports.get(moduleName);
2406
+ if (!ctxImportItems?.length) {
2407
+ return;
2408
+ }
2409
+ const currentImports = /* @__PURE__ */ new Set();
2410
+ for (const spec of currentNode.specifiers) {
2411
+ if (t19.isImportSpecifier(spec) && t19.isIdentifier(spec.imported)) {
2412
+ currentImports.add(spec.imported.name);
2413
+ }
2414
+ if (t19.isImportDefaultSpecifier(spec) && t19.isIdentifier(spec.local)) {
2415
+ currentImports.add(spec.local.name);
2416
+ }
2417
+ }
2418
+ for (const item of ctxImportItems) {
2419
+ if (currentImports.has(item.name)) {
2420
+ continue;
2421
+ }
2422
+ const local = t19.identifier(item.name);
2423
+ const newNode = !item.onDemand ? t19.importDefaultSpecifier(local) : t19.importSpecifier(local, local);
2424
+ currentNode.specifiers.push(newNode);
2425
+ }
2426
+ ctx.imports.delete(moduleName);
2427
+ }
2428
+ function createImportNodes(ctx) {
2429
+ const result = [];
2430
+ const importDeclarations = Array.from(ctx.imports).map(([moduleName, items]) => {
2431
+ const specifiers = items.map((item) => {
2432
+ const local = t19.identifier(item.name);
2433
+ return item.onDemand ? t19.importSpecifier(local, local) : t19.importDefaultSpecifier(local);
2434
+ });
2435
+ return t19.importDeclaration(specifiers, t19.stringLiteral(moduleName));
2436
+ });
2437
+ for (const decl of importDeclarations) {
2438
+ const name = decl.source.value;
2439
+ if (name === PACKAGE_NAME.react) {
2440
+ result.unshift(decl);
2441
+ } else {
2442
+ result.push(decl);
2443
+ }
2444
+ }
2445
+ return result;
2446
+ }
2447
+ function forkLeadingComments(target, source) {
2448
+ const { leadingComments } = source;
2449
+ if (!leadingComments?.length) {
2450
+ return;
2451
+ }
2452
+ const newComments = [...leadingComments];
2453
+ source.leadingComments = null;
2454
+ target.leadingComments = newComments;
2455
+ }
2456
+
2457
+ // src/core/transform/sfc/script/syntax-processor/postprocess/resolve-sfc-css-import.ts
2458
+ import * as t20 from "@babel/types";
2459
+ function resolveSfcCssImport(ctx) {
2426
2460
  if (ctx.inputType !== "sfc") return;
2427
2461
  const scriptIR = getScriptIR(ctx);
2428
- scriptIR.statement.local = ast;
2462
+ const { filePath, moduleName } = ctx.styleData;
2463
+ if (!filePath) return;
2464
+ const styleFilename = normalizePath(filePath).split("/").pop();
2465
+ const importDecl = t20.importDeclaration(
2466
+ !moduleName ? [] : [t20.importDefaultSpecifier(t20.identifier(moduleName))],
2467
+ t20.stringLiteral(`./${styleFilename}`)
2468
+ );
2469
+ scriptIR.imports.push(importDecl);
2429
2470
  }
2430
2471
 
2431
2472
  // src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
@@ -2485,7 +2526,7 @@ function checkIsDynamicImport(ctx, node) {
2485
2526
  if (t21.isCallExpression(node)) {
2486
2527
  warnIsNotImport(node.callee);
2487
2528
  if (t21.isStringLiteral(node.arguments[0])) {
2488
- replaceVueSuffix(ctx, node.arguments[0]);
2529
+ replaceVueSuffix(node.arguments[0]);
2489
2530
  }
2490
2531
  return;
2491
2532
  }
@@ -4266,8 +4307,8 @@ function processVueSyntax2(ast, ctx) {
4266
4307
  excludeBabel: [resolveTemplateSlotIface, resolveCompIProps]
4267
4308
  },
4268
4309
  postprocess: {
4269
- applyBabel: [resolveRequiredImports, resolveStaticHoisting],
4270
- excludeBabel: [insertCSSImport, collectLocalStatements]
4310
+ applyBabel: [resolveRuntimeImports, resolveASTChunks],
4311
+ excludeBabel: [resolveSfcCssImport]
4271
4312
  }
4272
4313
  });
4273
4314
  }
@@ -5650,7 +5691,7 @@ function transform(ast, ctx, options) {
5650
5691
  }
5651
5692
 
5652
5693
  // package.json
5653
- var version = "1.5.0";
5694
+ var version = "1.5.1";
5654
5695
  var bin = {
5655
5696
  vureact: "./bin/vureact.js"
5656
5697
  };
@@ -6398,18 +6439,10 @@ var BaseCompiler = class extends Helper {
6398
6439
  }
6399
6440
  prepareGenerateOptions(filename) {
6400
6441
  const userOptions = this.options.generate || {};
6401
- const mergedOptions = {
6402
- jsescOption: {
6403
- minimal: true,
6404
- quotes: "single"
6405
- },
6406
- minified: true,
6407
- ...userOptions
6408
- };
6409
- if (mergedOptions.sourceMaps && filename) {
6410
- mergedOptions.sourceFileName = mergedOptions.sourceFileName || filename;
6442
+ if (userOptions.sourceMaps && filename) {
6443
+ userOptions.sourceFileName = userOptions.sourceFileName || filename;
6411
6444
  }
6412
- return mergedOptions;
6445
+ return userOptions;
6413
6446
  }
6414
6447
  resolveMainResult(ir, gen, ctxData) {
6415
6448
  const { fileId, filename, scriptData, styleData, inputType } = ctxData;