vite-plugin-taro 0.6.1 → 0.6.2

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 (47) hide show
  1. package/dist/node/plugins/h5/transform-app.js +1 -1
  2. package/dist/node/plugins/wx/dev/plugins.js +1 -1
  3. package/dist/node/plugins/wx/dev/wx-dev-options.js +1 -1
  4. package/dist/node/plugins/wx/{module.js → module/module.js} +2 -2
  5. package/dist/node/plugins/wx/placer/placement.d.ts +1 -0
  6. package/dist/node/plugins/wx/placer/placement.js +1 -1
  7. package/dist/node/plugins/wx/placer/placer.d.ts +17 -1
  8. package/dist/node/plugins/wx/placer/placer.js +25 -1
  9. package/dist/node/plugins/wx/plugins.js +1 -1
  10. package/dist/node/plugins/wx/render/capsule.d.ts +35 -3
  11. package/dist/node/plugins/wx/render/capsule.js +53 -12
  12. package/dist/node/plugins/wx/render/native.d.ts +20 -2
  13. package/dist/node/plugins/wx/render/native.js +539 -35
  14. package/dist/node/plugins/wx/render/system-js/string-editor.d.ts +24 -0
  15. package/dist/node/plugins/wx/render/system-js/string-editor.js +93 -0
  16. package/dist/node/plugins/wx/render/system-js/system-js.d.ts +23 -0
  17. package/dist/node/plugins/wx/render/system-js/system-js.js +602 -0
  18. package/dist/node/plugins/wx/render/transport.js +3 -3
  19. package/dist/node/plugins/wx/resolve/resolver.js +1 -1
  20. package/dist/node/plugins/wx/resolve/specialize-bootstrap.js +1 -1
  21. package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +1 -1
  22. package/dist/node/utils/transform.d.ts +0 -3
  23. package/dist/node/utils/transform.js +0 -23
  24. package/package.json +8 -6
  25. package/src/node/plugins/h5/transform-app.ts +1 -1
  26. package/src/node/plugins/wx/dev/plugins.ts +1 -1
  27. package/src/node/plugins/wx/dev/wx-dev-options.ts +1 -1
  28. package/src/node/plugins/wx/{module.ts → module/module.ts} +2 -2
  29. package/src/node/plugins/wx/placer/placement.ts +1 -1
  30. package/src/node/plugins/wx/placer/placer.ts +27 -2
  31. package/src/node/plugins/wx/plugins.ts +1 -1
  32. package/src/node/plugins/wx/render/capsule.ts +53 -17
  33. package/src/node/plugins/wx/render/native.ts +670 -55
  34. package/src/node/plugins/wx/render/system-js/string-editor.ts +115 -0
  35. package/src/node/plugins/wx/render/system-js/system-js.ts +831 -0
  36. package/src/node/plugins/wx/render/transport.ts +3 -3
  37. package/src/node/plugins/wx/resolve/resolver.ts +1 -1
  38. package/src/node/plugins/wx/resolve/specialize-bootstrap.ts +1 -1
  39. package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +1 -1
  40. package/src/node/utils/transform.ts +0 -30
  41. package/dist/node/plugins/wx/render/capsule-wrapper.d.ts +0 -3
  42. package/dist/node/plugins/wx/render/capsule-wrapper.js +0 -64
  43. package/src/node/plugins/wx/render/capsule-wrapper.ts +0 -86
  44. /package/dist/node/plugins/wx/{chunk-path.d.ts → module/chunk-path.d.ts} +0 -0
  45. /package/dist/node/plugins/wx/{chunk-path.js → module/chunk-path.js} +0 -0
  46. /package/dist/node/plugins/wx/{module.d.ts → module/module.d.ts} +0 -0
  47. /package/src/node/plugins/wx/{chunk-path.ts → module/chunk-path.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import { types } from '@babel/core';
1
+ import * as types from '@babel/types';
2
2
  import { createPageComponentImportPath } from '../../utils/modules.js';
3
3
  import { createAppConfig } from '../../utils/project-config.js';
4
4
  import { replaceWithAst } from '../../utils/transform.js';
@@ -2,7 +2,7 @@ import { normalizePath, transformWithOxc } from 'vite';
2
2
  import { esTarget } from '../../../utils/constant.js';
3
3
  import { memoize } from '../../../utils/memoize.js';
4
4
  import { normalizeModuleId } from '../../../utils/modules.js';
5
- import { appCapsulePath, pageCapsulePath, rolldownRuntimeId, taroRuntimePath } from '../module.js';
5
+ import { appCapsulePath, pageCapsulePath, rolldownRuntimeId, taroRuntimePath } from '../module/module.js';
6
6
  import { createWxDevHost } from './dev-host.js';
7
7
  import { developmentAppWxssFileName } from './hmr-files.js';
8
8
  import { createWxReactRefreshTransforms } from './react-refresh.js';
@@ -3,7 +3,7 @@ import { build } from 'rolldown';
3
3
  import { viteReporterPlugin } from 'rolldown/experimental';
4
4
  import { once } from '../../../utils/once.js';
5
5
  import { resolvePackageFile } from '../../../utils/packages.js';
6
- import { appShellFileName } from '../module.js';
6
+ import { appShellFileName } from '../module/module.js';
7
7
  /**
8
8
  * Installs physical WX output and runtime conventions over Vite's browser-oriented bundled-development options.
9
9
  * Build completion deliberately remains outside this options adapter: DevEngine onOutput is the single lifecycle authority.
@@ -1,7 +1,7 @@
1
1
  import path from 'node:path';
2
2
  import { normalizePath } from 'vite';
3
- import { normalizeModuleId } from '../../utils/modules.js';
4
- import { packageRequire, resolvePackageFile } from '../../utils/packages.js';
3
+ import { normalizeModuleId } from '../../../utils/modules.js';
4
+ import { packageRequire, resolvePackageFile } from '../../../utils/packages.js';
5
5
  /** Identifies Rolldown's generated helper module independently of its unstable output filename. */
6
6
  export const rolldownRuntimeId = '\0rolldown/runtime.js';
7
7
  /** Resolves the direct React Reconciler dependency without assuming pnpm's layout. */
@@ -1,4 +1,5 @@
1
1
  import type { Rolldown } from 'vite';
2
+ export declare const subpackagePlanningBudget = 1900000;
2
3
  /** Identifies one generated code-only subpackage by its physical output root. */
3
4
  export type SubpackageLocation = {
4
5
  /** Discriminates generated subpackages from main. */
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  // Leave headroom below WeChat's 2M subpackage limit for rendered wrappers and native assets.
3
- const subpackagePlanningBudget = 1_900_000;
3
+ export const subpackagePlanningBudget = 1_900_000;
4
4
  const generatedSubpackageRootPrefix = 'sub/p_';
5
5
  /** Shared main-package value used for every synchronously reachable chunk. */
6
6
  const mainPackage = { kind: 'main' };
@@ -1,6 +1,8 @@
1
- import type { Plugin, Rolldown } from 'vite';
1
+ import { type Plugin, type Rolldown } from 'vite';
2
2
  import { type GeneratedSubpackage, type PackageLocation } from './placement.ts';
3
3
  export type { GeneratedSubpackage, Placement } from './placement.ts';
4
+ /** Selects the explicit React/Taro roots whose complete dependency closure forms the framework vendor chunk. */
5
+ export declare function isWxFrameworkVendorModule(moduleId: string): boolean;
4
6
  /** Placement services consumed by the later `vpt:wx` rendering and output hooks. */
5
7
  export type WxPlacementPlugin = Plugin & Readonly<{
6
8
  getPackageLocation(chunk: Rolldown.RenderedChunk | Rolldown.OutputChunk): PackageLocation;
@@ -18,6 +20,20 @@ export declare const placementRolldownOptions: {
18
20
  * participation only; LTHP mutates the resulting OutputChunk filenames later without replacing the chunks.
19
21
  */
20
22
  output: {
23
+ /**
24
+ * React and Taro form one stable framework boundary shared by the App and every Page capsule. Keeping their complete
25
+ * dependency closure together prevents application edits from invalidating framework chunk identity and makes later
26
+ * development generations eligible to reuse the unchanged vendor. All remaining modules use Rolldown's automatic
27
+ * chunking; physical WX package placement operates on those final chunks without imposing another split strategy.
28
+ */
29
+ codeSplitting: {
30
+ groups: {
31
+ name: string;
32
+ test: typeof isWxFrameworkVendorModule;
33
+ priority: number;
34
+ includeDependenciesRecursively: boolean;
35
+ }[];
36
+ };
21
37
  /**
22
38
  * Native App/Page/Component shells are files addressed directly by WeChat and must retain the exact names configured
23
39
  * in `input`, such as `app.js` and `pages/home/index.js`. Transport is excluded even though it is CommonJS:
@@ -1,6 +1,14 @@
1
- import { getWxExecutionKind, isTransportModule } from '../module.js';
1
+ import { normalizePath } from 'vite';
2
+ import { getWxExecutionKind, isTransportModule } from '../module/module.js';
2
3
  import { getNativeComponentAssetBytes } from '../native/native-component-assets.js';
3
4
  import { createPlacement } from './placement.js';
5
+ const pnpmFrameworkPackagePattern = /\/node_modules\/\.pnpm\/(?:@tarojs\+|react(?:-dom|-reconciler)?@|scheduler@)/;
6
+ const workspaceFrameworkPackagePattern = /\/packages\/(?:taro-react|taro-plugin-framework-react)\//;
7
+ /** Selects the explicit React/Taro roots whose complete dependency closure forms the framework vendor chunk. */
8
+ export function isWxFrameworkVendorModule(moduleId) {
9
+ const normalizedId = normalizePath(moduleId);
10
+ return pnpmFrameworkPackagePattern.test(normalizedId) || workspaceFrameworkPackagePattern.test(normalizedId);
11
+ }
4
12
  /**
5
13
  * Rolldown options owned by WX placement. Every field enforces a distinct output invariant. The plugin returns this object
6
14
  * from its config hook, while direct Rolldown integration tests reuse the same value to exercise the identical lifecycle.
@@ -11,6 +19,22 @@ export const placementRolldownOptions = {
11
19
  * participation only; LTHP mutates the resulting OutputChunk filenames later without replacing the chunks.
12
20
  */
13
21
  output: {
22
+ /**
23
+ * React and Taro form one stable framework boundary shared by the App and every Page capsule. Keeping their complete
24
+ * dependency closure together prevents application edits from invalidating framework chunk identity and makes later
25
+ * development generations eligible to reuse the unchanged vendor. All remaining modules use Rolldown's automatic
26
+ * chunking; physical WX package placement operates on those final chunks without imposing another split strategy.
27
+ */
28
+ codeSplitting: {
29
+ groups: [
30
+ {
31
+ name: 'vendor',
32
+ test: isWxFrameworkVendorModule,
33
+ priority: 100,
34
+ includeDependenciesRecursively: true
35
+ }
36
+ ]
37
+ },
14
38
  /**
15
39
  * Native App/Page/Component shells are files addressed directly by WeChat and must retain the exact names configured
16
40
  * in `input`, such as `app.js` and `pages/home/index.js`. Transport is excluded even though it is CommonJS:
@@ -2,7 +2,7 @@ import { esTarget } from '../../utils/constant.js';
2
2
  import { packageRequire } from '../../utils/packages.js';
3
3
  import { clientTaroNativeId } from '../client/constant.js';
4
4
  import { createWxDevelopmentPlugin } from './dev/plugins.js';
5
- import { getWxExecutionKind, isTransportModule } from './module.js';
5
+ import { getWxExecutionKind, isTransportModule } from './module/module.js';
6
6
  import { compileNativeComponentInterface } from './native/compile-native-component-interface.js';
7
7
  import { createOutputFiles } from './output/files.js';
8
8
  import { createWxPlacementPlugin } from './placer/placer.js';
@@ -1,4 +1,36 @@
1
1
  import type { Rolldown } from 'vite';
2
- import { type AstTransformResult } from '../../../utils/transform.ts';
3
- /** Renders one ESM chunk as an inert SystemJS capsule. */
4
- export declare function renderCapsule(code: string, chunk: Rolldown.RenderedChunk, sourcemap?: boolean): AstTransformResult;
2
+ import type { AstTransformResult } from '../../../utils/transform.ts';
3
+ /**
4
+ * Converts one final Rolldown ESM chunk into the inert registration consumed by the WX module runtime.
5
+ *
6
+ * Why this boundary exists:
7
+ *
8
+ * - Rolldown produces an ESM chunk graph, but a Mini Program loads emitted JavaScript through native `require` and
9
+ * `require.async`; it does not provide the browser ESM loader that normally links Vite chunks.
10
+ * - Native App, Page, and Component shells must start through WeChat's synchronous lifecycle APIs, while their application
11
+ * graph still needs ESM linking semantics such as cycles, live exports, dynamic imports, `import.meta`, and top-level await.
12
+ * - The plugin therefore keeps native lifecycle shells as CommonJS and executes application "capsules" through the bundled
13
+ * SystemJS runtime. Native shells enter that graph with `global.System.importSync`, and genuine asynchronous boundaries use
14
+ * the same runtime through `System.import`.
15
+ *
16
+ * What this renderer emits:
17
+ *
18
+ * The result is CommonJS source whose `module.exports` is a SystemJS registration tuple:
19
+ * `[logicalDependencyIds, declaration]`. Requiring a capsule only returns this inert tuple; it does not execute application
20
+ * code or call a global `System.register`. The generated transport can consequently load the physical file from either the
21
+ * main package or a subpackage, then hand its registration to the loader. The loader owns linking, setter invocation,
22
+ * execution order, cycle handling, and export publication.
23
+ *
24
+ * Why references are rewritten:
25
+ *
26
+ * Rolldown imports name physical output files whose relative paths include placement directories and content hashes. The
27
+ * SystemJS registry instead uses package-neutral logical chunk IDs. Canonicalizing both static and literal dynamic references
28
+ * here lets transport independently map a stable module identity to the physical `require` path selected by WX placement.
29
+ * Runtime-computed dynamic IDs cannot be canonicalized and are intentionally preserved.
30
+ *
31
+ * This function runs on final Rolldown output rather than arbitrary source modules. `transformSystemJs` accepts only the
32
+ * normalized final-chunk grammar required by this pipeline and fails the build on unsupported forms instead of risking a
33
+ * partial or semantically incorrect capsule. Source maps remain optional because WX development output deliberately disables
34
+ * them on this startup-critical path.
35
+ */
36
+ export declare function renderCapsule(code: string, chunk: Rolldown.RenderedChunk, sourcemap: boolean): AstTransformResult;
@@ -1,13 +1,54 @@
1
- import transformDynamicImport from '@babel/plugin-transform-dynamic-import';
2
- import transformModulesSystemjs from '@babel/plugin-transform-modules-systemjs';
3
- import { transformWithBabel } from '../../../utils/transform.js';
4
- import { wrapCapsulePlugin } from './capsule-wrapper.js';
5
- /** Renders one ESM chunk as an inert SystemJS capsule. */
6
- export function renderCapsule(code, chunk, sourcemap = true) {
7
- return transformWithBabel(code, chunk.fileName, [
8
- transformDynamicImport,
9
- // Erase Babel's internal plugin pass type.
10
- transformModulesSystemjs,
11
- wrapCapsulePlugin(chunk.fileName)
12
- ], sourcemap);
1
+ import { resolveLogicalChunkReference } from '../module/chunk-path.js';
2
+ import { transformSystemJs } from './system-js/system-js.js';
3
+ /**
4
+ * Converts one final Rolldown ESM chunk into the inert registration consumed by the WX module runtime.
5
+ *
6
+ * Why this boundary exists:
7
+ *
8
+ * - Rolldown produces an ESM chunk graph, but a Mini Program loads emitted JavaScript through native `require` and
9
+ * `require.async`; it does not provide the browser ESM loader that normally links Vite chunks.
10
+ * - Native App, Page, and Component shells must start through WeChat's synchronous lifecycle APIs, while their application
11
+ * graph still needs ESM linking semantics such as cycles, live exports, dynamic imports, `import.meta`, and top-level await.
12
+ * - The plugin therefore keeps native lifecycle shells as CommonJS and executes application "capsules" through the bundled
13
+ * SystemJS runtime. Native shells enter that graph with `global.System.importSync`, and genuine asynchronous boundaries use
14
+ * the same runtime through `System.import`.
15
+ *
16
+ * What this renderer emits:
17
+ *
18
+ * The result is CommonJS source whose `module.exports` is a SystemJS registration tuple:
19
+ * `[logicalDependencyIds, declaration]`. Requiring a capsule only returns this inert tuple; it does not execute application
20
+ * code or call a global `System.register`. The generated transport can consequently load the physical file from either the
21
+ * main package or a subpackage, then hand its registration to the loader. The loader owns linking, setter invocation,
22
+ * execution order, cycle handling, and export publication.
23
+ *
24
+ * Why references are rewritten:
25
+ *
26
+ * Rolldown imports name physical output files whose relative paths include placement directories and content hashes. The
27
+ * SystemJS registry instead uses package-neutral logical chunk IDs. Canonicalizing both static and literal dynamic references
28
+ * here lets transport independently map a stable module identity to the physical `require` path selected by WX placement.
29
+ * Runtime-computed dynamic IDs cannot be canonicalized and are intentionally preserved.
30
+ *
31
+ * This function runs on final Rolldown output rather than arbitrary source modules. `transformSystemJs` accepts only the
32
+ * normalized final-chunk grammar required by this pipeline and fails the build on unsupported forms instead of risking a
33
+ * partial or semantically incorrect capsule. Source maps remain optional because WX development output deliberately disables
34
+ * them on this startup-critical path.
35
+ */
36
+ export function renderCapsule(code, chunk, sourcemap) {
37
+ return transformSystemJs({
38
+ code,
39
+ filename: chunk.fileName,
40
+ // Native require must return registration data without executing or globally registering the capsule.
41
+ format: 'commonjs-registration',
42
+ sourcemap,
43
+ // Bind every compile-time chunk edge to the same logical identity understood by transport and SystemJS.
44
+ resolveReference(reference, kind) {
45
+ return resolveCapsuleReference(chunk.fileName, reference, kind);
46
+ }
47
+ });
48
+ }
49
+ /** Canonicalizes physical chunk paths while preserving runtime-computed and package-like dynamic IDs. */
50
+ function resolveCapsuleReference(fileName, reference, kind) {
51
+ if (kind === 'dynamic' && !reference.startsWith('./') && !reference.startsWith('../'))
52
+ return reference;
53
+ return resolveLogicalChunkReference(fileName, reference);
13
54
  }
@@ -1,6 +1,24 @@
1
1
  import type { Rolldown } from 'vite';
2
- import { type AstTransformResult } from '../../../utils/transform.ts';
3
- /** Renders a native module while activating its statically imported capsules through SystemJS. */
2
+ import type { AstTransformResult } from '../../../utils/transform.ts';
3
+ /**
4
+ * Materializes one final native/amphibious Rolldown chunk as executable WX CommonJS.
5
+ *
6
+ * The transform performs four semantic operations:
7
+ *
8
+ * 1. Static ESM imports are hoisted into source-order `require` calls. Named imports remain property reads from the required
9
+ * namespace so they observe current values. Default and namespace imports receive Babel-compatible CommonJS interop.
10
+ * 2. An import whose target owns a capsule entry is not passed to native `require`. It becomes
11
+ * `global.System.importSync(logicalChunkId)`, synchronously linking the capsule before the native lifecycle call.
12
+ * 3. Local exports are published at declaration and mutation points. Imported re-exports use getters, while assignments and
13
+ * updates notify every alias without changing expression completion values or accidentally matching shadowed bindings.
14
+ * 4. ESM top-level `this` becomes `undefined`. Direct imported calls and tags are explicitly unbound so converting an import
15
+ * into a namespace property does not introduce a false CommonJS receiver.
16
+ *
17
+ * Static dependency loading is emitted before the untouched module body because ESM evaluates dependencies before body
18
+ * statements regardless of where import declarations appear textually. Generated helper names are allocated against every
19
+ * source identifier. Source maps use the same range edits when requested; WX development disables them and keeps this path
20
+ * focused on startup latency.
21
+ */
4
22
  export declare function renderNative({ code, chunk, chunks, sourcemap }: {
5
23
  code: string;
6
24
  chunk: Rolldown.RenderedChunk;