@wevu/compiler 6.20.3 → 6.20.5

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.
Files changed (2) hide show
  1. package/dist/index.mjs +188 -61
  2. package/package.json +4 -4
package/dist/index.mjs CHANGED
@@ -2061,9 +2061,25 @@ function isTemplateReturnPropertyReference(path) {
2061
2061
  if (t.isIdentifier(key, { name: path.node.name }) || t.isStringLiteral(key, { value: path.node.name })) return true;
2062
2062
  return false;
2063
2063
  }
2064
+ function isStaticPropertyNamed(node, name) {
2065
+ return !node.computed && (t.isIdentifier(node.key, { name }) || t.isStringLiteral(node.key, { value: name }));
2066
+ }
2067
+ function isOptionsApiComponentsObject(path) {
2068
+ const componentsProperty = path.parentPath;
2069
+ if (!componentsProperty?.isObjectProperty() || !isStaticPropertyNamed(componentsProperty.node, "components")) return false;
2070
+ const optionsObject = componentsProperty.parentPath;
2071
+ if (!optionsObject?.isObjectExpression()) return false;
2072
+ const optionsParent = optionsObject.parentPath;
2073
+ if (optionsParent?.isExportDefaultDeclaration()) return true;
2074
+ return Boolean(optionsParent?.isCallExpression() && optionsParent.node.arguments[0] === optionsObject.node && optionsParent.parentPath?.isExportDefaultDeclaration());
2075
+ }
2076
+ function isOptionsApiComponentRegistrationReference(path) {
2077
+ const property = path.parentPath;
2078
+ return Boolean(property?.isObjectProperty() && property.node.value === path.node && property.parentPath?.isObjectExpression() && isOptionsApiComponentsObject(property.parentPath));
2079
+ }
2064
2080
  function isReferencedOutsideTemplateReturn(path) {
2065
2081
  if (!path.isReferencedIdentifier()) return false;
2066
- if (isTemplateReturnGetterReference(path) || isTemplateReturnPropertyReference(path)) return false;
2082
+ if (isTemplateReturnGetterReference(path) || isTemplateReturnPropertyReference(path) || isOptionsApiComponentRegistrationReference(path)) return false;
2067
2083
  return true;
2068
2084
  }
2069
2085
  function collectReferencedNames(ast, candidateNames) {
@@ -2102,6 +2118,21 @@ function removeSetupReturnProperties(ast, removableNames) {
2102
2118
  });
2103
2119
  return changed;
2104
2120
  }
2121
+ function removeOptionsApiComponentRegistrations(ast, removableNames) {
2122
+ if (!removableNames.size) return false;
2123
+ let changed = false;
2124
+ traverse(ast, { ObjectProperty(path) {
2125
+ if (!path.parentPath?.isObjectExpression() || !isOptionsApiComponentsObject(path.parentPath) || !t.isIdentifier(path.node.value) || !removableNames.has(path.node.value.name)) return;
2126
+ path.remove();
2127
+ changed = true;
2128
+ } });
2129
+ traverse(ast, { ObjectProperty(path) {
2130
+ if (!t.isObjectExpression(path.node.value) || !isStaticPropertyNamed(path.node, "components") || path.node.value.properties.length > 0 || !path.parentPath?.isObjectExpression()) return;
2131
+ const optionsParent = path.parentPath.parentPath;
2132
+ if (optionsParent?.isExportDefaultDeclaration() || optionsParent?.isCallExpression() && optionsParent.node.arguments[0] === path.parentPath.node && optionsParent.parentPath?.isExportDefaultDeclaration()) path.remove();
2133
+ } });
2134
+ return changed;
2135
+ }
2105
2136
  function pruneTemplateComponentMeta(ast, templateComponentMeta) {
2106
2137
  if (!Object.keys(templateComponentMeta).length) return false;
2107
2138
  const candidateNames = new Set(Object.keys(templateComponentMeta));
@@ -2125,6 +2156,7 @@ function pruneTemplateComponentMeta(ast, templateComponentMeta) {
2125
2156
  }
2126
2157
  } });
2127
2158
  changed = removeSetupReturnProperties(ast, removableNames) || changed;
2159
+ changed = removeOptionsApiComponentRegistrations(ast, removableNames) || changed;
2128
2160
  return changed;
2129
2161
  }
2130
2162
  //#endregion
@@ -10104,6 +10136,49 @@ function collectTemplateComponentTagInfo(template, filename, warn) {
10104
10136
  tagsByComponentName
10105
10137
  };
10106
10138
  }
10139
+ async function resolveScriptComponentRegistrations(options) {
10140
+ const { pending, filename, compileOptions, autoUsingComponents, result } = options;
10141
+ const resolvedComponents = await Promise.all(pending.map(async ({ localName, importSource, importedName, kind, templateTags }) => {
10142
+ let resolved = autoUsingComponents?.resolveUsingComponentPath ? normalizeResolvedUsingComponent(await autoUsingComponents.resolveUsingComponentPath(importSource, filename, {
10143
+ localName,
10144
+ importedName,
10145
+ kind
10146
+ })) : void 0;
10147
+ if (!resolved?.from && importSource.startsWith("/")) resolved = {
10148
+ from: removeExtensionDeep(importSource),
10149
+ resolvedId: importSource
10150
+ };
10151
+ if (!resolved?.resolvedId && isVueSfcSource(importSource) && importSource.startsWith(".")) resolved = {
10152
+ ...resolved ?? {},
10153
+ resolvedId: path.resolve(path.dirname(filename), importSource)
10154
+ };
10155
+ const componentMeta = await resolveVueSfcStaticComponentMeta(resolved?.resolvedId, {
10156
+ cache: compileOptions?.componentMetaCache,
10157
+ warn: autoUsingComponents?.warn ?? compileOptions?.warn
10158
+ });
10159
+ return {
10160
+ localName,
10161
+ importSource,
10162
+ resolved,
10163
+ componentMeta,
10164
+ templateTags
10165
+ };
10166
+ }));
10167
+ for (const { localName, importSource, resolved, componentMeta, templateTags } of resolvedComponents) {
10168
+ if (resolved?.from) {
10169
+ for (const tag of templateTags) result.autoUsingComponentsMap[tag] = resolved.from;
10170
+ result.autoComponentMeta[localName] = resolved.from;
10171
+ }
10172
+ if (isVueSfcSource(importSource) || isWevuSfcComponent(resolved)) {
10173
+ result.wevuComponentTags.add(localName);
10174
+ result.wevuComponentTags.add(normalizeTemplateTagName(localName));
10175
+ }
10176
+ registerComponentName(result, localName, componentMeta.componentName);
10177
+ for (const tag of templateTags) registerComponentName(result, tag, componentMeta.componentName);
10178
+ registerMiniProgramComponentTag(result, localName, componentMeta.isMiniProgramComponent);
10179
+ for (const tag of templateTags) registerMiniProgramComponentTag(result, tag, componentMeta.isMiniProgramComponent);
10180
+ }
10181
+ }
10107
10182
  async function collectScriptSetupUsingComponents(options) {
10108
10183
  const { descriptor, descriptorForCompile, filename, compileOptions, autoUsingComponents, templateComponentNames, templateTagsByComponentName, result } = options;
10109
10184
  if (!descriptor.scriptSetup || !descriptor.template) return;
@@ -10140,49 +10215,89 @@ async function collectScriptSetupUsingComponents(options) {
10140
10215
  }
10141
10216
  }
10142
10217
  } });
10143
- const resolvedComponents = await Promise.all(pending.map(async ({ localName, importSource, importedName, kind, templateTags }) => {
10144
- let resolved = autoUsingComponents?.resolveUsingComponentPath ? normalizeResolvedUsingComponent(await autoUsingComponents.resolveUsingComponentPath(importSource, filename, {
10145
- localName,
10146
- importedName,
10147
- kind
10148
- })) : void 0;
10149
- if (!resolved?.from && importSource.startsWith("/")) resolved = {
10150
- from: removeExtensionDeep(importSource),
10151
- resolvedId: importSource
10152
- };
10153
- if (!resolved?.resolvedId && isVueSfcSource(importSource) && importSource.startsWith(".")) resolved = {
10154
- ...resolved ?? {},
10155
- resolvedId: path.resolve(path.dirname(filename), importSource)
10156
- };
10157
- const componentMeta = await resolveVueSfcStaticComponentMeta(resolved?.resolvedId, {
10158
- cache: compileOptions?.componentMetaCache,
10159
- warn: autoUsingComponents?.warn ?? compileOptions?.warn
10160
- });
10161
- return {
10162
- localName,
10163
- importSource,
10164
- resolved,
10165
- componentMeta,
10166
- templateTags
10167
- };
10168
- }));
10169
- for (const { localName, importSource, resolved, componentMeta, templateTags } of resolvedComponents) {
10170
- if (resolved?.from) {
10171
- for (const tag of templateTags) result.autoUsingComponentsMap[tag] = resolved.from;
10172
- result.autoComponentMeta[localName] = resolved.from;
10173
- }
10174
- if (isVueSfcSource(importSource) || isWevuSfcComponent(resolved)) {
10175
- result.wevuComponentTags.add(localName);
10176
- result.wevuComponentTags.add(normalizeTemplateTagName(localName));
10218
+ await resolveScriptComponentRegistrations({
10219
+ pending,
10220
+ filename,
10221
+ compileOptions,
10222
+ autoUsingComponents,
10223
+ result
10224
+ });
10225
+ } catch (error) {
10226
+ const message = error instanceof Error ? error.message : String(error);
10227
+ autoUsingComponents?.warn?.(`[Vue 编译] 解析 ${filename} 的 <script setup> 导入失败:${message}`);
10228
+ }
10229
+ }
10230
+ function getExportDefaultObject(scriptAst) {
10231
+ let options;
10232
+ traverse(scriptAst, { ExportDefaultDeclaration(path) {
10233
+ const declaration = path.node.declaration;
10234
+ if (t.isObjectExpression(declaration)) options = declaration;
10235
+ else if (t.isCallExpression(declaration) && t.isObjectExpression(declaration.arguments[0])) options = declaration.arguments[0];
10236
+ path.stop();
10237
+ } });
10238
+ return options;
10239
+ }
10240
+ function getObjectProperty(node, keyName) {
10241
+ for (const property of node.properties) {
10242
+ if (!t.isObjectProperty(property) || property.computed) continue;
10243
+ if (t.isIdentifier(property.key) ? property.key.name === keyName : t.isStringLiteral(property.key) && property.key.value === keyName) return property;
10244
+ }
10245
+ }
10246
+ function getComponentRegistrationName(property) {
10247
+ if (t.isIdentifier(property.key)) return property.key.name;
10248
+ return t.isStringLiteral(property.key) ? property.key.value : void 0;
10249
+ }
10250
+ async function collectOptionsApiUsingComponents(options) {
10251
+ const { descriptor, filename, compileOptions, autoUsingComponents, templateComponentNames, templateTagsByComponentName, result } = options;
10252
+ if (!descriptor.script || !descriptor.template || !templateComponentNames?.size) return;
10253
+ try {
10254
+ const ast = parse$3(descriptor.script.content, BABEL_TS_MODULE_PARSER_OPTIONS);
10255
+ const imports = /* @__PURE__ */ new Map();
10256
+ traverse(ast, { ImportDeclaration(path) {
10257
+ if (path.node.importKind === "type" || !t.isStringLiteral(path.node.source)) return;
10258
+ for (const specifier of path.node.specifiers) {
10259
+ if ("importKind" in specifier && specifier.importKind === "type") continue;
10260
+ if (!t.isIdentifier(specifier.local)) continue;
10261
+ if (t.isImportDefaultSpecifier(specifier)) imports.set(specifier.local.name, {
10262
+ importSource: path.node.source.value,
10263
+ importedName: "default",
10264
+ kind: "default"
10265
+ });
10266
+ else if (t.isImportSpecifier(specifier)) {
10267
+ const importedName = t.isIdentifier(specifier.imported) ? specifier.imported.name : t.isStringLiteral(specifier.imported) ? specifier.imported.value : void 0;
10268
+ imports.set(specifier.local.name, {
10269
+ importSource: path.node.source.value,
10270
+ importedName,
10271
+ kind: "named"
10272
+ });
10273
+ }
10177
10274
  }
10178
- registerComponentName(result, localName, componentMeta.componentName);
10179
- for (const tag of templateTags) registerComponentName(result, tag, componentMeta.componentName);
10180
- registerMiniProgramComponentTag(result, localName, componentMeta.isMiniProgramComponent);
10181
- for (const tag of templateTags) registerMiniProgramComponentTag(result, tag, componentMeta.isMiniProgramComponent);
10275
+ } });
10276
+ const componentOptions = getExportDefaultObject(ast);
10277
+ const componentsProperty = componentOptions && getObjectProperty(componentOptions, "components");
10278
+ if (!componentsProperty || !t.isObjectExpression(componentsProperty.value)) return;
10279
+ const pending = [];
10280
+ for (const property of componentsProperty.value.properties) {
10281
+ if (!t.isObjectProperty(property) || !t.isIdentifier(property.value)) continue;
10282
+ const registrationName = getComponentRegistrationName(property);
10283
+ const imported = imports.get(property.value.name);
10284
+ if (!registrationName || !imported || !templateComponentNames.has(registrationName)) continue;
10285
+ pending.push({
10286
+ localName: property.value.name,
10287
+ ...imported,
10288
+ templateTags: [...templateTagsByComponentName?.get(registrationName) ?? [registrationName]]
10289
+ });
10182
10290
  }
10291
+ await resolveScriptComponentRegistrations({
10292
+ pending,
10293
+ filename,
10294
+ compileOptions,
10295
+ autoUsingComponents,
10296
+ result
10297
+ });
10183
10298
  } catch (error) {
10184
10299
  const message = error instanceof Error ? error.message : String(error);
10185
- autoUsingComponents?.warn?.(`[Vue 编译] 解析 ${filename} 的 <script setup> 导入失败:${message}`);
10300
+ autoUsingComponents?.warn?.(`[Vue 编译] 解析 ${filename} 的 Options API 局部组件失败:${message}`);
10186
10301
  }
10187
10302
  }
10188
10303
  async function collectAutoImportWevuComponents(options) {
@@ -10224,27 +10339,39 @@ async function collectAutoImportWevuComponents(options) {
10224
10339
  async function collectComponentSourceInfo(options) {
10225
10340
  const result = createComponentSourceInfo();
10226
10341
  const templateComponentTagInfo = options.descriptor.template ? collectTemplateComponentTagInfo(options.descriptor.template.content, options.filename, options.autoUsingComponents?.warn ?? options.autoImportTags?.warn ?? options.compileOptions?.warn) : void 0;
10227
- const scriptSetupResult = createComponentSourceInfo();
10342
+ const scriptImportResult = createComponentSourceInfo();
10228
10343
  const autoImportResult = createComponentSourceInfo();
10229
- await Promise.all([collectScriptSetupUsingComponents({
10230
- descriptor: options.descriptor,
10231
- descriptorForCompile: options.descriptorForCompile,
10232
- filename: options.filename,
10233
- compileOptions: options.compileOptions,
10234
- autoUsingComponents: options.autoUsingComponents,
10235
- templateComponentNames: templateComponentTagInfo?.componentNames,
10236
- templateTagsByComponentName: templateComponentTagInfo?.tagsByComponentName,
10237
- result: scriptSetupResult
10238
- }), collectAutoImportWevuComponents({
10239
- descriptor: options.descriptor,
10240
- filename: options.filename,
10241
- compileOptions: options.compileOptions,
10242
- autoImportTags: options.autoImportTags,
10243
- templateAutoImportTags: templateComponentTagInfo?.autoImportTags,
10244
- warn: options.compileOptions?.warn,
10245
- result: autoImportResult
10246
- })]);
10247
- mergeComponentSourceInfo(result, scriptSetupResult);
10344
+ await Promise.all([
10345
+ collectScriptSetupUsingComponents({
10346
+ descriptor: options.descriptor,
10347
+ descriptorForCompile: options.descriptorForCompile,
10348
+ filename: options.filename,
10349
+ compileOptions: options.compileOptions,
10350
+ autoUsingComponents: options.autoUsingComponents,
10351
+ templateComponentNames: templateComponentTagInfo?.componentNames,
10352
+ templateTagsByComponentName: templateComponentTagInfo?.tagsByComponentName,
10353
+ result: scriptImportResult
10354
+ }),
10355
+ collectOptionsApiUsingComponents({
10356
+ descriptor: options.descriptor,
10357
+ filename: options.filename,
10358
+ compileOptions: options.compileOptions,
10359
+ autoUsingComponents: options.autoUsingComponents,
10360
+ templateComponentNames: templateComponentTagInfo?.componentNames,
10361
+ templateTagsByComponentName: templateComponentTagInfo?.tagsByComponentName,
10362
+ result: scriptImportResult
10363
+ }),
10364
+ collectAutoImportWevuComponents({
10365
+ descriptor: options.descriptor,
10366
+ filename: options.filename,
10367
+ compileOptions: options.compileOptions,
10368
+ autoImportTags: options.autoImportTags,
10369
+ templateAutoImportTags: templateComponentTagInfo?.autoImportTags,
10370
+ warn: options.compileOptions?.warn,
10371
+ result: autoImportResult
10372
+ })
10373
+ ]);
10374
+ mergeComponentSourceInfo(result, scriptImportResult);
10248
10375
  mergeComponentSourceInfo(result, autoImportResult);
10249
10376
  return result;
10250
10377
  }
@@ -11348,7 +11475,7 @@ async function compileVueFile(source, filename, options) {
11348
11475
  ...parsed.meta,
11349
11476
  styleBlocks: parsed.descriptor.styles
11350
11477
  } };
11351
- const autoUsingComponents = options?.autoUsingComponents?.enabled && parsed.descriptor.scriptSetup && parsed.descriptor.template && options.autoUsingComponents.resolveUsingComponentPath ? options.autoUsingComponents : void 0;
11478
+ const autoUsingComponents = options?.autoUsingComponents?.enabled && (parsed.descriptor.scriptSetup || parsed.descriptor.script) && parsed.descriptor.template && options.autoUsingComponents.resolveUsingComponentPath ? options.autoUsingComponents : void 0;
11352
11479
  const autoImportTags = options?.autoImportTags?.enabled && parsed.descriptor.template && options.autoImportTags.resolveUsingComponent ? options.autoImportTags : void 0;
11353
11480
  const componentSourceInfo = await collectComponentSourceInfo({
11354
11481
  descriptor: parsed.descriptor,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/compiler",
3
3
  "type": "module",
4
- "version": "6.20.3",
4
+ "version": "6.20.5",
5
5
  "description": "wevu 编译器基础包,面向小程序模板的编译与转换",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -47,7 +47,7 @@
47
47
  "@vue/compiler-dom": "^3.5.41",
48
48
  "comment-json": "^5.0.0",
49
49
  "lru-cache": "^11.5.2",
50
- "magic-string": "^1.2.0",
50
+ "magic-string": "^1.2.2",
51
51
  "merge": "^2.1.1",
52
52
  "pathe": "^2.0.3",
53
53
  "postcss": "^8.5.26",
@@ -55,8 +55,8 @@
55
55
  "vue": "^3.5.41",
56
56
  "@weapp-core/constants": "0.1.17",
57
57
  "@weapp-core/shared": "3.1.1",
58
- "@weapp-vite/ast": "6.20.3",
59
- "rolldown-require": "2.0.25"
58
+ "@weapp-vite/ast": "6.20.5",
59
+ "rolldown-require": "2.0.26"
60
60
  },
61
61
  "publishConfig": {
62
62
  "access": "public",