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,3 @@
1
- import { transformSync } from '@babel/core';
2
1
  import { generate } from '@babel/generator';
3
2
  import { transformWithOxc } from 'vite';
4
3
  import { esTarget } from './constant.js';
@@ -34,25 +33,3 @@ function requireOnePlaceholder(code, placeholder) {
34
33
  throw new Error(`Expected one placeholder ${placeholder}, found ${replacementCount}`);
35
34
  }
36
35
  }
37
- /** Transforms one module with Babel's shared parser and source-map configuration. */
38
- export function transformWithBabel(code, filename, plugins, sourcemap = true) {
39
- const transformed = transformSync(code, {
40
- babelrc: false,
41
- compact: true,
42
- minified: true,
43
- comments: false,
44
- configFile: false,
45
- filename,
46
- plugins,
47
- sourceFileName: filename,
48
- sourceMaps: sourcemap,
49
- sourceType: 'module'
50
- });
51
- if (!transformed?.code || (sourcemap && !transformed.map)) {
52
- throw new Error(`Failed to transform ${filename} with Babel`);
53
- }
54
- return {
55
- code: transformed.code,
56
- map: sourcemap ? transformed.map : null
57
- };
58
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "author": "sep2",
5
5
  "description": "Vite 8 plugin for building one React/Taro codebase for WeChat Mini Program and H5 targets.",
6
6
  "type": "module",
@@ -53,9 +53,8 @@
53
53
  "dependencies": {
54
54
  "@babel/core": "^7.29.7",
55
55
  "@babel/generator": "^7.29.8",
56
- "@babel/plugin-transform-dynamic-import": "^7.29.7",
57
- "@babel/plugin-transform-modules-commonjs": "^7.29.7",
58
- "@babel/plugin-transform-modules-systemjs": "^7.29.8",
56
+ "@babel/types": "^7.29.8",
57
+ "@oxc-project/types": "0.143.0",
59
58
  "@rolldown/plugin-babel": "^0.2.3",
60
59
  "@tailwindcss-mangle/engine": "0.2.0",
61
60
  "@tarojs/components": "4.2.0",
@@ -76,8 +75,8 @@
76
75
  "rxjs": "^7.8.2",
77
76
  "tailwindcss": "^4.3.3",
78
77
  "weapp-tailwindcss": "^5.2.11",
79
- "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.6.1",
80
- "@tarojs/react": "npm:vite-plugin-taro-react@0.6.1"
78
+ "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.6.2",
79
+ "@tarojs/react": "npm:vite-plugin-taro-react@0.6.2"
81
80
  },
82
81
  "peerDependencies": {
83
82
  "react": "^19.0.0",
@@ -85,6 +84,9 @@
85
84
  "vite": "8.2.1"
86
85
  },
87
86
  "devDependencies": {
87
+ "@babel/plugin-transform-dynamic-import": "^7.29.7",
88
+ "@babel/plugin-transform-modules-commonjs": "^7.29.7",
89
+ "@babel/plugin-transform-modules-systemjs": "^7.29.8",
88
90
  "@types/babel__core": "^7.20.5",
89
91
  "@types/babel__generator": "^7.27.0",
90
92
  "@types/node": "^26.1.1",
@@ -1,4 +1,4 @@
1
- import { types } from '@babel/core'
1
+ import * as types from '@babel/types'
2
2
  import type { VptOptions, VptPageOption } from '../../../options.ts'
3
3
  import { createPageComponentImportPath } from '../../utils/modules.ts'
4
4
  import { createAppConfig } from '../../utils/project-config.ts'
@@ -3,7 +3,7 @@ import type { VptOptions } from '../../../../options.ts'
3
3
  import { esTarget } from '../../../utils/constant.ts'
4
4
  import { memoize } from '../../../utils/memoize.ts'
5
5
  import { normalizeModuleId } from '../../../utils/modules.ts'
6
- import { appCapsulePath, pageCapsulePath, rolldownRuntimeId, taroRuntimePath } from '../module.ts'
6
+ import { appCapsulePath, pageCapsulePath, rolldownRuntimeId, taroRuntimePath } from '../module/module.ts'
7
7
  import { createWxDevHost, type WxDevHost } from './dev-host.ts'
8
8
  import { developmentAppWxssFileName } from './hmr-files.ts'
9
9
  import { createWxReactRefreshTransforms } from './react-refresh.ts'
@@ -6,7 +6,7 @@ import type { ViteDevServer } from 'vite'
6
6
  import type { VptOptions } from '../../../../options.ts'
7
7
  import { once } from '../../../utils/once.ts'
8
8
  import { resolvePackageFile } from '../../../utils/packages.ts'
9
- import { appShellFileName } from '../module.ts'
9
+ import { appShellFileName } from '../module/module.ts'
10
10
 
11
11
  type BundledDevRolldownOptions = InputOptions & {
12
12
  experimental?: {
@@ -1,7 +1,7 @@
1
1
  import path from 'node:path'
2
2
  import { normalizePath, type Rolldown } from 'vite'
3
- import { normalizeModuleId } from '../../utils/modules.ts'
4
- import { packageRequire, resolvePackageFile } from '../../utils/packages.ts'
3
+ import { normalizeModuleId } from '../../../utils/modules.ts'
4
+ import { packageRequire, resolvePackageFile } from '../../../utils/packages.ts'
5
5
 
6
6
  /** Identifies Rolldown's generated helper module independently of its unstable output filename. */
7
7
  export const rolldownRuntimeId = '\0rolldown/runtime.js'
@@ -2,7 +2,7 @@ import { createHash } from 'node:crypto'
2
2
  import type { Rolldown } from 'vite'
3
3
 
4
4
  // Leave headroom below WeChat's 2M subpackage limit for rendered wrappers and native assets.
5
- const subpackagePlanningBudget = 1_900_000
5
+ export const subpackagePlanningBudget = 1_900_000
6
6
  const generatedSubpackageRootPrefix = 'sub/p_'
7
7
 
8
8
  /** Identifies one generated code-only subpackage by its physical output root. */
@@ -1,10 +1,19 @@
1
- import type { Plugin, Rolldown } from 'vite'
2
- import { getWxExecutionKind, isTransportModule } from '../module.ts'
1
+ import { normalizePath, type Plugin, type Rolldown } from 'vite'
2
+ import { getWxExecutionKind, isTransportModule } from '../module/module.ts'
3
3
  import { getNativeComponentAssetBytes } from '../native/native-component-assets.ts'
4
4
  import { createPlacement, type GeneratedSubpackage, type PackageLocation, type Placement } from './placement.ts'
5
5
 
6
6
  export type { GeneratedSubpackage, Placement } from './placement.ts'
7
7
 
8
+ const pnpmFrameworkPackagePattern = /\/node_modules\/\.pnpm\/(?:@tarojs\+|react(?:-dom|-reconciler)?@|scheduler@)/
9
+ const workspaceFrameworkPackagePattern = /\/packages\/(?:taro-react|taro-plugin-framework-react)\//
10
+
11
+ /** Selects the explicit React/Taro roots whose complete dependency closure forms the framework vendor chunk. */
12
+ export function isWxFrameworkVendorModule(moduleId: string): boolean {
13
+ const normalizedId = normalizePath(moduleId)
14
+ return pnpmFrameworkPackagePattern.test(normalizedId) || workspaceFrameworkPackagePattern.test(normalizedId)
15
+ }
16
+
8
17
  type PlacementState =
9
18
  | { phase: 'idle' }
10
19
  | { phase: 'awaiting-chunks' }
@@ -30,6 +39,22 @@ export const placementRolldownOptions = {
30
39
  * participation only; LTHP mutates the resulting OutputChunk filenames later without replacing the chunks.
31
40
  */
32
41
  output: {
42
+ /**
43
+ * React and Taro form one stable framework boundary shared by the App and every Page capsule. Keeping their complete
44
+ * dependency closure together prevents application edits from invalidating framework chunk identity and makes later
45
+ * development generations eligible to reuse the unchanged vendor. All remaining modules use Rolldown's automatic
46
+ * chunking; physical WX package placement operates on those final chunks without imposing another split strategy.
47
+ */
48
+ codeSplitting: {
49
+ groups: [
50
+ {
51
+ name: 'vendor',
52
+ test: isWxFrameworkVendorModule,
53
+ priority: 100,
54
+ includeDependenciesRecursively: true
55
+ }
56
+ ]
57
+ },
33
58
  /**
34
59
  * Native App/Page/Component shells are files addressed directly by WeChat and must retain the exact names configured
35
60
  * in `input`, such as `app.js` and `pages/home/index.js`. Transport is excluded even though it is CommonJS:
@@ -4,7 +4,7 @@ import { esTarget } from '../../utils/constant.ts'
4
4
  import { packageRequire } from '../../utils/packages.ts'
5
5
  import { clientTaroNativeId } from '../client/constant.ts'
6
6
  import { createWxDevelopmentPlugin } from './dev/plugins.ts'
7
- import { getWxExecutionKind, isTransportModule } from './module.ts'
7
+ import { getWxExecutionKind, isTransportModule } from './module/module.ts'
8
8
  import { compileNativeComponentInterface } from './native/compile-native-component-interface.ts'
9
9
  import { createOutputFiles } from './output/files.ts'
10
10
  import { createWxPlacementPlugin, type WxPlacementPlugin } from './placer/placer.ts'
@@ -1,21 +1,57 @@
1
- import type { PluginTarget } from '@babel/core'
2
- import transformDynamicImport from '@babel/plugin-transform-dynamic-import'
3
- import transformModulesSystemjs from '@babel/plugin-transform-modules-systemjs'
4
1
  import type { Rolldown } from 'vite'
5
- import { type AstTransformResult, transformWithBabel } from '../../../utils/transform.ts'
6
- import { wrapCapsulePlugin } from './capsule-wrapper.ts'
2
+ import type { AstTransformResult } from '../../../utils/transform.ts'
3
+ import { resolveLogicalChunkReference } from '../module/chunk-path.ts'
4
+ import { type SystemJsReferenceKind, transformSystemJs } from './system-js/system-js.ts'
7
5
 
8
- /** Renders one ESM chunk as an inert SystemJS capsule. */
9
- export function renderCapsule(code: string, chunk: Rolldown.RenderedChunk, sourcemap = true): AstTransformResult {
10
- return transformWithBabel(
6
+ /**
7
+ * Converts one final Rolldown ESM chunk into the inert registration consumed by the WX module runtime.
8
+ *
9
+ * Why this boundary exists:
10
+ *
11
+ * - Rolldown produces an ESM chunk graph, but a Mini Program loads emitted JavaScript through native `require` and
12
+ * `require.async`; it does not provide the browser ESM loader that normally links Vite chunks.
13
+ * - Native App, Page, and Component shells must start through WeChat's synchronous lifecycle APIs, while their application
14
+ * graph still needs ESM linking semantics such as cycles, live exports, dynamic imports, `import.meta`, and top-level await.
15
+ * - The plugin therefore keeps native lifecycle shells as CommonJS and executes application "capsules" through the bundled
16
+ * SystemJS runtime. Native shells enter that graph with `global.System.importSync`, and genuine asynchronous boundaries use
17
+ * the same runtime through `System.import`.
18
+ *
19
+ * What this renderer emits:
20
+ *
21
+ * The result is CommonJS source whose `module.exports` is a SystemJS registration tuple:
22
+ * `[logicalDependencyIds, declaration]`. Requiring a capsule only returns this inert tuple; it does not execute application
23
+ * code or call a global `System.register`. The generated transport can consequently load the physical file from either the
24
+ * main package or a subpackage, then hand its registration to the loader. The loader owns linking, setter invocation,
25
+ * execution order, cycle handling, and export publication.
26
+ *
27
+ * Why references are rewritten:
28
+ *
29
+ * Rolldown imports name physical output files whose relative paths include placement directories and content hashes. The
30
+ * SystemJS registry instead uses package-neutral logical chunk IDs. Canonicalizing both static and literal dynamic references
31
+ * here lets transport independently map a stable module identity to the physical `require` path selected by WX placement.
32
+ * Runtime-computed dynamic IDs cannot be canonicalized and are intentionally preserved.
33
+ *
34
+ * This function runs on final Rolldown output rather than arbitrary source modules. `transformSystemJs` accepts only the
35
+ * normalized final-chunk grammar required by this pipeline and fails the build on unsupported forms instead of risking a
36
+ * partial or semantically incorrect capsule. Source maps remain optional because WX development output deliberately disables
37
+ * them on this startup-critical path.
38
+ */
39
+ export function renderCapsule(code: string, chunk: Rolldown.RenderedChunk, sourcemap: boolean): AstTransformResult {
40
+ return transformSystemJs({
11
41
  code,
12
- chunk.fileName,
13
- [
14
- transformDynamicImport,
15
- // Erase Babel's internal plugin pass type.
16
- transformModulesSystemjs as PluginTarget,
17
- wrapCapsulePlugin(chunk.fileName) as PluginTarget
18
- ],
19
- sourcemap
20
- )
42
+ filename: chunk.fileName,
43
+ // Native require must return registration data without executing or globally registering the capsule.
44
+ format: 'commonjs-registration',
45
+ sourcemap,
46
+ // Bind every compile-time chunk edge to the same logical identity understood by transport and SystemJS.
47
+ resolveReference(reference, kind) {
48
+ return resolveCapsuleReference(chunk.fileName, reference, kind)
49
+ }
50
+ })
51
+ }
52
+
53
+ /** Canonicalizes physical chunk paths while preserving runtime-computed and package-like dynamic IDs. */
54
+ function resolveCapsuleReference(fileName: string, reference: string, kind: SystemJsReferenceKind): string {
55
+ if (kind === 'dynamic' && !reference.startsWith('./') && !reference.startsWith('../')) return reference
56
+ return resolveLogicalChunkReference(fileName, reference)
21
57
  }