vite-plugin-taro 0.6.18 → 0.6.19

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 (133) hide show
  1. package/client.d.ts +2 -3
  2. package/dist/index.js +213574 -0
  3. package/dist/node/plugins/wx/dev/react-refresh.d.ts +14 -13
  4. package/dist/node/utils/packages.d.ts +2 -0
  5. package/package.json +26 -26
  6. package/dist/node/plugins/client/client-taro.js +0 -59
  7. package/dist/node/plugins/client/constant.js +0 -4
  8. package/dist/node/plugins/client/inject-taro-framework-apis.js +0 -9
  9. package/dist/node/plugins/conditional/conditional-directives.js +0 -76
  10. package/dist/node/plugins/h5/constant.js +0 -3
  11. package/dist/node/plugins/h5/create-stencil-client-adapter.js +0 -94
  12. package/dist/node/plugins/h5/plugins.js +0 -179
  13. package/dist/node/plugins/h5/resolver/module-resolver.js +0 -20
  14. package/dist/node/plugins/h5/transform-app.js +0 -48
  15. package/dist/node/plugins/tailwind/tailwind-css.js +0 -3
  16. package/dist/node/plugins/wx/dev/create-hmr-results-stream.js +0 -57
  17. package/dist/node/plugins/wx/dev/dev-host.js +0 -361
  18. package/dist/node/plugins/wx/dev/hmr-files.js +0 -56
  19. package/dist/node/plugins/wx/dev/host-actions.js +0 -58
  20. package/dist/node/plugins/wx/dev/patch-publisher.js +0 -77
  21. package/dist/node/plugins/wx/dev/plugins.js +0 -164
  22. package/dist/node/plugins/wx/dev/react-refresh.js +0 -231
  23. package/dist/node/plugins/wx/dev/runtime-reports.js +0 -45
  24. package/dist/node/plugins/wx/dev/wx-dev-options.js +0 -178
  25. package/dist/node/plugins/wx/module/chunk-path.js +0 -18
  26. package/dist/node/plugins/wx/module/module.js +0 -79
  27. package/dist/node/plugins/wx/native/compile-native-component-interface.js +0 -30
  28. package/dist/node/plugins/wx/native/create-native-component-output.js +0 -39
  29. package/dist/node/plugins/wx/native/native-component-assets.js +0 -76
  30. package/dist/node/plugins/wx/native/native-component-interface.js +0 -203
  31. package/dist/node/plugins/wx/output/files.js +0 -21
  32. package/dist/node/plugins/wx/output/relative-root.js +0 -6
  33. package/dist/node/plugins/wx/output/templates.js +0 -358
  34. package/dist/node/plugins/wx/placer/placement.js +0 -245
  35. package/dist/node/plugins/wx/placer/placer.js +0 -147
  36. package/dist/node/plugins/wx/plugins.js +0 -152
  37. package/dist/node/plugins/wx/render/capsule.js +0 -54
  38. package/dist/node/plugins/wx/render/native.js +0 -543
  39. package/dist/node/plugins/wx/render/system-js/string-editor.js +0 -94
  40. package/dist/node/plugins/wx/render/system-js/system-js.js +0 -588
  41. package/dist/node/plugins/wx/render/transport.js +0 -103
  42. package/dist/node/plugins/wx/resolve/resolver.js +0 -102
  43. package/dist/node/plugins/wx/resolve/specialize-app-capsule.js +0 -9
  44. package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +0 -11
  45. package/dist/node/plugins/wx/styles/plugins.js +0 -459
  46. package/dist/node/utils/constant.js +0 -1
  47. package/dist/node/utils/memoize.js +0 -86
  48. package/dist/node/utils/modules.js +0 -22
  49. package/dist/node/utils/once.js +0 -20
  50. package/dist/node/utils/oxc-transform.js +0 -57
  51. package/dist/node/utils/packages.js +0 -9
  52. package/dist/node/utils/project-config.js +0 -8
  53. package/dist/node/utils/transform.js +0 -32
  54. package/dist/node/utils/vite.js +0 -10
  55. package/dist/node/vpt.js +0 -15
  56. package/dist/options.js +0 -1
  57. package/dist/vite.js +0 -1
  58. package/src/node/plugins/client/client-taro.ts +0 -71
  59. package/src/node/plugins/client/constant.ts +0 -5
  60. package/src/node/plugins/client/inject-taro-framework-apis.ts +0 -11
  61. package/src/node/plugins/conditional/conditional-directives.ts +0 -94
  62. package/src/node/plugins/h5/constant.ts +0 -4
  63. package/src/node/plugins/h5/create-stencil-client-adapter.ts +0 -111
  64. package/src/node/plugins/h5/plugins.ts +0 -194
  65. package/src/node/plugins/h5/resolver/module-resolver.ts +0 -33
  66. package/src/node/plugins/h5/transform-app.ts +0 -90
  67. package/src/node/plugins/tailwind/tailwind-css.ts +0 -4
  68. package/src/node/plugins/wx/dev/create-hmr-results-stream.ts +0 -71
  69. package/src/node/plugins/wx/dev/dev-host.ts +0 -453
  70. package/src/node/plugins/wx/dev/hmr-files.ts +0 -80
  71. package/src/node/plugins/wx/dev/host-actions.ts +0 -88
  72. package/src/node/plugins/wx/dev/patch-publisher.ts +0 -92
  73. package/src/node/plugins/wx/dev/plugins.ts +0 -176
  74. package/src/node/plugins/wx/dev/react-refresh.ts +0 -253
  75. package/src/node/plugins/wx/dev/runtime-reports.ts +0 -70
  76. package/src/node/plugins/wx/dev/wx-dev-options.ts +0 -226
  77. package/src/node/plugins/wx/module/chunk-path.ts +0 -22
  78. package/src/node/plugins/wx/module/module.ts +0 -110
  79. package/src/node/plugins/wx/native/compile-native-component-interface.ts +0 -48
  80. package/src/node/plugins/wx/native/create-native-component-output.ts +0 -60
  81. package/src/node/plugins/wx/native/native-component-assets.ts +0 -113
  82. package/src/node/plugins/wx/native/native-component-interface.ts +0 -267
  83. package/src/node/plugins/wx/output/files.ts +0 -40
  84. package/src/node/plugins/wx/output/relative-root.ts +0 -7
  85. package/src/node/plugins/wx/output/templates.ts +0 -448
  86. package/src/node/plugins/wx/placer/placement.ts +0 -364
  87. package/src/node/plugins/wx/placer/placer.ts +0 -180
  88. package/src/node/plugins/wx/plugins.ts +0 -183
  89. package/src/node/plugins/wx/render/capsule.ts +0 -57
  90. package/src/node/plugins/wx/render/native.ts +0 -694
  91. package/src/node/plugins/wx/render/system-js/string-editor.ts +0 -116
  92. package/src/node/plugins/wx/render/system-js/system-js.ts +0 -821
  93. package/src/node/plugins/wx/render/transport.ts +0 -173
  94. package/src/node/plugins/wx/resolve/resolver.ts +0 -145
  95. package/src/node/plugins/wx/resolve/specialize-app-capsule.ts +0 -27
  96. package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +0 -29
  97. package/src/node/plugins/wx/styles/plugins.ts +0 -595
  98. package/src/node/utils/constant.ts +0 -1
  99. package/src/node/utils/memoize.ts +0 -149
  100. package/src/node/utils/modules.ts +0 -37
  101. package/src/node/utils/once.ts +0 -21
  102. package/src/node/utils/oxc-transform.ts +0 -76
  103. package/src/node/utils/packages.ts +0 -11
  104. package/src/node/utils/project-config.ts +0 -10
  105. package/src/node/utils/transform.ts +0 -48
  106. package/src/node/utils/vite.ts +0 -22
  107. package/src/node/vpt.ts +0 -18
  108. package/src/options.ts +0 -82
  109. package/src/runtime/client/taro/api.ts +0 -11
  110. package/src/runtime/client/taro/component.ts +0 -1
  111. package/src/runtime/client/taro/define-native-component.ts +0 -15
  112. package/src/runtime/h5/app.ts +0 -25
  113. package/src/runtime/h5/taro-css.d.ts +0 -3
  114. package/src/runtime/h5/taro-runtime.ts +0 -7
  115. package/src/runtime/wx/amphibious/bootstrap.ts +0 -17
  116. package/src/runtime/wx/amphibious/transport.ts +0 -8
  117. package/src/runtime/wx/capsule/app.ts +0 -12
  118. package/src/runtime/wx/capsule/component.ts +0 -7
  119. package/src/runtime/wx/capsule/page.ts +0 -29
  120. package/src/runtime/wx/capsule/taro-runtime.ts +0 -8
  121. package/src/runtime/wx/dev/dev-runtime.ts +0 -464
  122. package/src/runtime/wx/native/app.ts +0 -4
  123. package/src/runtime/wx/native/component.ts +0 -4
  124. package/src/runtime/wx/native/custom-wrapper.ts +0 -4
  125. package/src/runtime/wx/native/page.ts +0 -6
  126. package/src/runtime/wx/systemjs/LICENSE +0 -10
  127. package/src/runtime/wx/systemjs/system-core.js +0 -370
  128. package/src/runtime/wx/systemjs/system.d.ts +0 -55
  129. package/src/runtime/wx/wechat.d.ts +0 -56
  130. package/src/vite.ts +0 -2
  131. /package/dist/{vite.d.ts → index.d.ts} +0 -0
  132. /package/dist/runtime/client/taro/{define-native-component.d.ts → native.d.ts} +0 -0
  133. /package/dist/runtime/client/taro/{define-native-component.js → native.js} +0 -0
@@ -1,79 +0,0 @@
1
- import path from 'node:path';
2
- import { normalizePath } from 'vite';
3
- import { normalizeModuleId } from '../../../utils/modules.js';
4
- import { packageRequire, resolvePackageFile } from '../../../utils/packages.js';
5
- /** Identifies Rolldown's generated helper module independently of its unstable output filename. */
6
- export const rolldownRuntimeId = '\0rolldown/runtime.js';
7
- /** Resolves the direct React Reconciler dependency without assuming pnpm's layout. */
8
- export const reactReconcilerRoot = normalizePath(path.dirname(packageRequire.resolve('react-reconciler/package.json')));
9
- /** Identifies the amphibious bootstrap that initializes SystemJS and serves every native shell. */
10
- export const bootstrapPath = resolvePackageFile('dist/runtime/wx/amphibious/bootstrap.js');
11
- /** Identifies the native transport source materialized before Rolldown finalizes content hashes. */
12
- export const transportPath = resolvePackageFile('dist/runtime/wx/amphibious/transport.js');
13
- /** Redirects Vite's injected browser preload helper to the bootstrap identity loader. */
14
- export const vitePreloadId = '\0vite/preload-helper.js';
15
- /** Forces the native App shell entry to emit at WeChat's required root path. */
16
- export const appShellFileName = 'app.js';
17
- /** Identifies the synchronous native App shell source. */
18
- export const appShellPath = resolvePackageFile('dist/runtime/wx/native/app.js');
19
- /** Identifies the App capsule kept behind the native shell boundary. */
20
- export const appCapsulePath = resolvePackageFile('dist/runtime/wx/capsule/app.js');
21
- /** Forces Taro's recursive native Component entry to emit at its configured root path. */
22
- export const componentShellFileName = 'comp.js';
23
- /** Identifies the synchronous recursive Component shell source. */
24
- export const componentShellPath = resolvePackageFile('dist/runtime/wx/native/component.js');
25
- /** Identifies the recursive Component capsule shared by generated Taro component shells. */
26
- export const componentCapsulePath = resolvePackageFile('dist/runtime/wx/capsule/component.js');
27
- /** Forces Taro's CustomWrapper native shell to emit at its configured root path. */
28
- export const customWrapperShellFileName = 'custom-wrapper.js';
29
- /** Identifies the synchronous CustomWrapper shell source. */
30
- export const customWrapperShellPath = resolvePackageFile('dist/runtime/wx/native/custom-wrapper.js');
31
- /** Resolves the configured Page component from its route-qualified capsule importer. */
32
- export const pageComponentId = '\0vpt:page-component';
33
- /** Gives every Page shell one private capsule target that can be resolved using its route. */
34
- export const pageCapsuleId = '\0vpt:page-capsule';
35
- /** Provides the Page capsule source specialized through a stable route query. */
36
- export const pageCapsulePath = resolvePackageFile('dist/runtime/wx/capsule/page.js');
37
- /** Identifies the Taro facade shared by the App, Page, and generated component capsules. */
38
- export const taroRuntimePath = resolvePackageFile('dist/runtime/wx/capsule/taro-runtime.js');
39
- /** Identifies the reusable synchronous native Page shell source. */
40
- export const pageShellPath = resolvePackageFile('dist/runtime/wx/native/page.js');
41
- // These fixed source identities describe entry roles independently from the final execution kind. A capsule entry may,
42
- // for example, become amphibious when Rolldown coalesces its generated runtime into the same output chunk.
43
- const shellModuleIds = new Set([
44
- appShellPath,
45
- componentShellPath,
46
- customWrapperShellPath,
47
- pageShellPath
48
- ]);
49
- const capsuleModuleIds = new Set([appCapsulePath, componentCapsulePath, pageCapsulePath]);
50
- const amphibiousModuleIds = new Set([bootstrapPath, rolldownRuntimeId]);
51
- const transportModuleIds = new Set([transportPath]);
52
- /** Returns the one explicit lifecycle role owned by a chunk. */
53
- export function getWxEntryRole(chunk) {
54
- const ownsShell = containsModule(chunk, shellModuleIds);
55
- const ownsCapsule = containsModule(chunk, capsuleModuleIds);
56
- if (ownsShell && ownsCapsule) {
57
- throw new Error(`wx chunk mixes shell and capsule entries: ${chunk.moduleIds.join(', ')}`);
58
- }
59
- return ownsShell ? 'shell' : ownsCapsule ? 'capsule' : undefined;
60
- }
61
- /** Tests whether a chunk contains the physical transport implementation. */
62
- export function isTransportModule(chunk) {
63
- return containsModule(chunk, transportModuleIds);
64
- }
65
- /**
66
- * Classifies one final output chunk by execution domain. Entry role is deliberately independent: explicit capsule entries
67
- * normally fall through to capsule rendering, but a capsule containing an amphibious runtime executes in both domains.
68
- */
69
- export function getWxExecutionKind(chunk) {
70
- const entryRole = getWxEntryRole(chunk);
71
- if (containsModule(chunk, amphibiousModuleIds)) {
72
- return 'amphibious';
73
- }
74
- return entryRole === 'shell' || isTransportModule(chunk) ? 'native' : 'capsule';
75
- }
76
- /** Tests normalized chunk module IDs against one fixed identity set. */
77
- function containsModule(chunk, moduleIds) {
78
- return chunk.moduleIds.some((moduleId) => moduleIds.has(normalizeModuleId(moduleId)));
79
- }
@@ -1,30 +0,0 @@
1
- import path from 'node:path';
2
- import { normalizePath } from 'vite';
3
- import { normalizeModuleId } from '../../../utils/modules.js';
4
- import { collectNativeComponentAssets, nativeComponentMetaKey } from './native-component-assets.js';
5
- import { transformNativeComponentInterfaces } from './native-component-interface.js';
6
- /** Compiles one JSX interface module and attaches its opaque native sources to Rolldown metadata. */
7
- export async function compileNativeComponentInterface({ code, id, sourcemap, addWatchFile }) {
8
- const transformed = transformNativeComponentInterfaces(code, id, sourcemap);
9
- const interfacePath = normalizeModuleId(id);
10
- const nativeComponents = await Promise.all(transformed.definitions.map((definition) => collectNativeComponentAssets(definition, interfacePath)));
11
- nativeComponents.forEach((source) => {
12
- addWatchFile(source.folder);
13
- source.assets.forEach((asset) => {
14
- addWatchFile(normalizePath(path.join(source.folder, asset.relativePath)));
15
- });
16
- });
17
- const assetBytes = nativeComponents.reduce((sourceTotal, source) => {
18
- return sourceTotal + source.assets.reduce((assetTotal, asset) => assetTotal + asset.byteLength, 0);
19
- }, 0);
20
- return {
21
- code: transformed.code,
22
- map: transformed.map,
23
- meta: {
24
- [nativeComponentMetaKey]: {
25
- sources: nativeComponents,
26
- assetBytes
27
- }
28
- }
29
- };
30
- }
@@ -1,39 +0,0 @@
1
- import { readFile } from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { getNativeComponentSources } from './native-component-assets.js';
4
- /** Creates opaque native files and their registrations from each surviving JSX interface module. */
5
- export async function createNativeComponentOutput({ bundle, getModuleInfo, getPackageLocation }) {
6
- // Files and registrations accumulate in final chunk order for deterministic output.
7
- const files = [];
8
- const registrations = [];
9
- for (const output of Object.values(bundle)) {
10
- if (output.type !== 'chunk') {
11
- continue;
12
- }
13
- const location = getPackageLocation(output);
14
- const packageRoot = location.kind === 'subpackage' ? location.root : undefined;
15
- for (const moduleId of output.moduleIds) {
16
- const sources = getNativeComponentSources(getModuleInfo(moduleId)?.meta);
17
- for (const source of sources) {
18
- const name = path.posix.basename(source.folder);
19
- const componentFolder = path.posix.join('components', name);
20
- const outputFolder = packageRoot ? path.posix.join(packageRoot, componentFolder) : componentFolder;
21
- registrations.push({
22
- name,
23
- componentPath: `/${path.posix.join(outputFolder, source.entry)}`,
24
- fields: source.fields
25
- });
26
- for (const asset of source.assets) {
27
- // Native folders are intentionally opaque to Rolldown, so no bundle asset contains these bytes to reuse.
28
- // Read each planned source once here to transfer it into the compiler-owned output bundle.
29
- files.push({
30
- type: 'asset',
31
- fileName: path.posix.join(outputFolder, asset.relativePath),
32
- source: await readFile(path.join(source.folder, asset.relativePath))
33
- });
34
- }
35
- }
36
- }
37
- }
38
- return { files, registrations };
39
- }
@@ -1,76 +0,0 @@
1
- import { readdir, stat } from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { normalizePath } from 'vite';
4
- export const nativeComponentMetaKey = 'vpt:native-components';
5
- /** Reads the native contribution to a transformed module's package weight. */
6
- export function getNativeComponentAssetBytes(meta) {
7
- return meta?.[nativeComponentMetaKey]?.assetBytes ?? 0;
8
- }
9
- /** Reads native sources attached to one transformed JSX interface module. */
10
- export function getNativeComponentSources(meta) {
11
- return meta?.[nativeComponentMetaKey]?.sources ?? [];
12
- }
13
- /** Collects a native component folder as opaque, recursively ordered assets without copying its interface module. */
14
- export async function collectNativeComponentAssets(definition, interfacePath) {
15
- await requireNativeComponentDirectory(definition.folder);
16
- await requireNativeComponentEntry(definition);
17
- // This mutable accumulator is local to one traversal and avoids repeatedly copying growing asset arrays.
18
- const assets = [];
19
- await collectDirectory(definition.folder, [], assets, normalizePath(interfacePath));
20
- return { ...definition, assets };
21
- }
22
- /** Rejects a missing or non-directory source before Rolldown starts its recursive traversal. */
23
- async function requireNativeComponentDirectory(folder) {
24
- try {
25
- if ((await stat(folder)).isDirectory()) {
26
- return;
27
- }
28
- }
29
- catch (error) {
30
- if (Error.isError(error) && 'code' in error && error.code === 'ENOENT') {
31
- throw new Error(`Native component folder does not exist: ${folder}`, { cause: error });
32
- }
33
- throw error;
34
- }
35
- throw new Error(`Native component source is not a directory: ${folder}`);
36
- }
37
- /** Rejects a missing or non-file JavaScript entry before native output registration. */
38
- async function requireNativeComponentEntry(definition) {
39
- const entryPath = path.join(definition.folder, `${definition.entry}.js`);
40
- try {
41
- if ((await stat(entryPath)).isFile()) {
42
- return;
43
- }
44
- }
45
- catch (error) {
46
- if (Error.isError(error) && 'code' in error && error.code === 'ENOENT') {
47
- throw new Error(`Native component entry file does not exist: ${entryPath}`, { cause: error });
48
- }
49
- throw error;
50
- }
51
- throw new Error(`Native component entry is not a file: ${entryPath}`);
52
- }
53
- /** Traverses entries lexically so output remains deterministic across filesystems. */
54
- async function collectDirectory(folder, segments, assets, interfacePath) {
55
- const directory = path.join(folder, ...segments);
56
- const entries = await readdir(directory, { withFileTypes: true });
57
- entries.sort((left, right) => left.name.localeCompare(right.name));
58
- for (const entry of entries) {
59
- const entrySegments = [...segments, entry.name];
60
- if (entry.isDirectory()) {
61
- await collectDirectory(folder, entrySegments, assets, interfacePath);
62
- continue;
63
- }
64
- if (!entry.isFile()) {
65
- continue;
66
- }
67
- const sourcePath = path.join(folder, ...entrySegments);
68
- if (normalizePath(sourcePath) === interfacePath) {
69
- continue;
70
- }
71
- assets.push({
72
- relativePath: entrySegments.join('/'),
73
- byteLength: (await stat(sourcePath)).size
74
- });
75
- }
76
- }
@@ -1,203 +0,0 @@
1
- import path from 'node:path';
2
- import { RolldownMagicString } from 'rolldown';
3
- import { parseAst } from 'rolldown/parseAst';
4
- import { normalizePath } from 'vite';
5
- import { normalizeModuleId } from '../../../utils/modules.js';
6
- import { clientTaroNativeId } from '../../client/constant.js';
7
- /** Replaces native component interface calls with source-folder names and returns their static metadata. */
8
- export function transformNativeComponentInterfaces(code, id, sourcemap) {
9
- const moduleId = normalizeModuleId(id);
10
- const program = parseAst(code, {
11
- astType: 'ts',
12
- lang: 'tsx',
13
- preserveParens: false,
14
- sourceType: 'module'
15
- }, moduleId);
16
- const declaredInterfaces = collectInterfaceDeclarations(program);
17
- const macroImports = collectMacroImports(program);
18
- const importedMacroNames = new Set(macroImports.flatMap((item) => item.names));
19
- const calls = collectTopLevelMacroCalls(program, importedMacroNames);
20
- const replacements = calls.map((call) => {
21
- const definition = parseDefinition(call, moduleId, declaredInterfaces, (message) => createTransformError(message, moduleId, code, call.start));
22
- return { call, definition };
23
- });
24
- // This transform-local mutable buffer applies non-overlapping AST edits without regenerating untouched source.
25
- const transformed = new RolldownMagicString(code, { filename: moduleId });
26
- replacements.forEach(({ call, definition }) => {
27
- transformed.overwrite(call.start, call.end, JSON.stringify(path.posix.basename(definition.folder)));
28
- });
29
- macroImports.forEach(({ declaration }) => {
30
- transformed.remove(declaration.start, declaration.end);
31
- });
32
- return {
33
- code: transformed.toString(),
34
- map: sourcemap ? createSourceMap(transformed, moduleId) : null,
35
- definitions: replacements.map(({ definition }) => definition)
36
- };
37
- }
38
- /** Normalizes Rolldown's native source-map object to the public plugin source-map shape. */
39
- function createSourceMap(transformed, moduleId) {
40
- const sourceMap = transformed.generateMap({
41
- hires: 'boundary',
42
- includeContent: true,
43
- source: moduleId
44
- });
45
- return {
46
- file: sourceMap.file,
47
- mappings: sourceMap.mappings,
48
- names: sourceMap.names,
49
- sources: sourceMap.sources,
50
- sourcesContent: sourceMap.sourcesContent,
51
- version: sourceMap.version
52
- };
53
- }
54
- /** Finds named imports of the compile-time macro and records their local aliases. */
55
- function collectMacroImports(program) {
56
- return program.body.flatMap((statement) => {
57
- if (statement.type !== 'ImportDeclaration' || statement.source.value !== clientTaroNativeId) {
58
- return [];
59
- }
60
- const names = statement.specifiers.flatMap((specifier) => {
61
- if (specifier.type !== 'ImportSpecifier' || getStaticName(specifier.imported) !== 'defineNativeComponent') {
62
- return [];
63
- }
64
- return [specifier.local.name];
65
- });
66
- return names.length === 0 ? [] : [{ declaration: statement, names }];
67
- });
68
- }
69
- /** Collects direct module-level macro calls, excluding calls inside nested scopes. */
70
- function collectTopLevelMacroCalls(program, importedMacroNames) {
71
- return program.body.flatMap((statement) => {
72
- if (statement.type === 'ExpressionStatement') {
73
- return isImportedMacroCall(statement.expression, importedMacroNames) ? [statement.expression] : [];
74
- }
75
- if (statement.type === 'ExportDefaultDeclaration') {
76
- return isImportedMacroCall(statement.declaration, importedMacroNames) ? [statement.declaration] : [];
77
- }
78
- const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
79
- if (declaration?.type !== 'VariableDeclaration') {
80
- return [];
81
- }
82
- return declaration.declarations.flatMap(({ init }) => {
83
- return isImportedMacroCall(init, importedMacroNames) ? [init] : [];
84
- });
85
- });
86
- }
87
- /** Matches a direct call to one of the macro's module-level import bindings. */
88
- function isImportedMacroCall(node, importedMacroNames) {
89
- return (node?.type === 'CallExpression' && node.callee.type === 'Identifier' && importedMacroNames.has(node.callee.name));
90
- }
91
- /** Parses one native entry loader and its optional TypeScript interface. */
92
- function parseDefinition(call, moduleId, declaredInterfaces, buildError) {
93
- if (call.arguments.length !== 1) {
94
- throw buildError('defineNativeComponent() requires exactly one entry loader');
95
- }
96
- const entryReference = readStaticEntryImport(call.arguments[0]);
97
- if (!entryReference) {
98
- throw buildError('Native component entry must use () => import(...) with a static relative .js path');
99
- }
100
- const entryPath = normalizePath(path.resolve(path.dirname(moduleId), entryReference));
101
- return {
102
- folder: path.posix.dirname(entryPath),
103
- entry: path.posix.basename(entryPath, '.js'),
104
- fields: readInterfaceFields(call.typeArguments, declaredInterfaces, buildError)
105
- };
106
- }
107
- /** Collects non-generic local object aliases and interfaces as native template field declarations. */
108
- function collectInterfaceDeclarations(program) {
109
- // This module-local map supports nearby declarations without constructing a TypeScript type-checker program.
110
- const declarations = new Map();
111
- for (const statement of program.body) {
112
- const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
113
- if (declaration?.type === 'TSTypeAliasDeclaration' &&
114
- !declaration.typeParameters &&
115
- declaration.typeAnnotation.type === 'TSTypeLiteral') {
116
- declarations.set(declaration.id.name, declaration.typeAnnotation.members);
117
- continue;
118
- }
119
- if (declaration?.type === 'TSInterfaceDeclaration' &&
120
- !declaration.typeParameters &&
121
- declaration.extends.length === 0) {
122
- declarations.set(declaration.id.name, declaration.body.body);
123
- }
124
- }
125
- return declarations;
126
- }
127
- /** Resolves an inline type or a non-generic object declaration from the current module. */
128
- function resolveInterfaceMembers(typeNode, declarations) {
129
- if (typeNode.type === 'TSTypeLiteral') {
130
- return typeNode.members;
131
- }
132
- if (typeNode.type === 'TSTypeReference' && typeNode.typeName.type === 'Identifier' && !typeNode.typeArguments) {
133
- return declarations.get(typeNode.typeName.name);
134
- }
135
- }
136
- /** Reads native template field names without resolving or validating their TypeScript value types. */
137
- function readInterfaceFields(typeArguments, declaredInterfaces, buildError) {
138
- if (!typeArguments) {
139
- return [];
140
- }
141
- if (typeArguments.params.length !== 1) {
142
- throw buildError('Native component interface must be one TypeScript object type');
143
- }
144
- // The length check above proves the one parameter exists; ESTree models the array element as optional generically.
145
- const firstType = typeArguments.params[0];
146
- const members = resolveInterfaceMembers(firstType, declaredInterfaces);
147
- if (!members) {
148
- throw buildError('Native component interface must be inline or declared in the same module');
149
- }
150
- // This local set preserves declaration order while rejecting ambiguous duplicate template fields.
151
- const names = new Set();
152
- for (const member of members) {
153
- if (member.type !== 'TSPropertySignature' && member.type !== 'TSMethodSignature') {
154
- throw buildError('Native component interface must contain only static fields');
155
- }
156
- if (member.computed) {
157
- throw buildError('Native component interface contains a computed field');
158
- }
159
- const name = getStaticName(member.key);
160
- if (!name) {
161
- throw buildError('Native component interface contains a computed field');
162
- }
163
- if (names.has(name)) {
164
- throw buildError(`Duplicate native component interface field: ${name}`);
165
- }
166
- names.add(name);
167
- }
168
- return [...names].filter((name) => name !== 'children');
169
- }
170
- /** Reads an identifier or string-literal key. */
171
- function getStaticName(node) {
172
- if (node.type === 'Identifier') {
173
- return node.name;
174
- }
175
- if (node.type === 'Literal' && typeof node.value === 'string') {
176
- return node.value;
177
- }
178
- }
179
- /** Reads a zero-argument entry loader containing one statically resolvable native `.js` import. */
180
- function readStaticEntryImport(node) {
181
- if (node?.type !== 'ArrowFunctionExpression' || node.params.length !== 0 || node.body.type !== 'ImportExpression') {
182
- return undefined;
183
- }
184
- const importExpression = node.body;
185
- if (importExpression.source.type !== 'Literal' ||
186
- typeof importExpression.source.value !== 'string' ||
187
- importExpression.options !== null ||
188
- !isLocalJavaScriptEntry(importExpression.source.value)) {
189
- return undefined;
190
- }
191
- return importExpression.source.value;
192
- }
193
- /** Limits component entries to JavaScript files reachable from their interface module. */
194
- function isLocalJavaScriptEntry(entry) {
195
- return (entry.startsWith('./') || entry.startsWith('../')) && path.posix.extname(entry) === '.js';
196
- }
197
- /** Adds a stable source location to semantic errors reported after parsing. */
198
- function createTransformError(message, moduleId, code, offset) {
199
- const prefix = code.slice(0, offset);
200
- const line = prefix.split('\n').length;
201
- const column = offset - prefix.lastIndexOf('\n');
202
- return new Error(`${message}\n${moduleId}:${line}:${column}`);
203
- }
@@ -1,21 +0,0 @@
1
- import { createNativeComponentOutput } from '../native/create-native-component-output.js';
2
- import { createTemplateAssets } from './templates.js';
3
- /** Creates every compiler-owned WX file derived from the final Rolldown bundle. */
4
- export async function createOutputFiles({ bundle, options, subpackages, isProduction, getModuleInfo, getPackageLocation }) {
5
- const nativeOutput = await createNativeComponentOutput({ bundle, getModuleInfo, getPackageLocation });
6
- return [
7
- {
8
- type: 'asset',
9
- fileName: 'app.wxss',
10
- source: '@import "./assets/global.wxss";\n'
11
- },
12
- ...createTemplateAssets({
13
- bundle: bundle,
14
- options: options,
15
- subpackages: subpackages,
16
- nativeComponents: nativeOutput.registrations,
17
- isProduction: isProduction
18
- }),
19
- ...nativeOutput.files
20
- ];
21
- }
@@ -1,6 +0,0 @@
1
- import path from 'node:path';
2
- /** Creates a Page-relative path to a file emitted at the wx output root. */
3
- export function toRootRelativePath(pagePath, rootFileName) {
4
- const relativePath = path.posix.relative(path.posix.dirname(pagePath), rootFileName);
5
- return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
6
- }