vite-plugin-taro 0.3.2 → 0.4.0
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.
- package/README.en.md +44 -0
- package/README.md +22 -497
- package/client.d.ts +9 -0
- package/dist/node/plugins/client/client-taro.d.ts +4 -0
- package/dist/node/plugins/client/client-taro.js +19 -0
- package/dist/node/plugins/client/constant.d.ts +4 -0
- package/dist/node/plugins/client/constant.js +4 -0
- package/dist/node/plugins/conditional/conditional-directives.d.ts +4 -0
- package/dist/node/plugins/{conditional-directives.js → conditional/conditional-directives.js} +27 -11
- package/dist/node/plugins/css/plugins.d.ts +4 -0
- package/dist/node/plugins/css/plugins.js +84 -0
- package/dist/node/plugins/h5/constant.d.ts +2 -0
- package/dist/node/plugins/h5/constant.js +3 -0
- package/dist/node/plugins/h5/plugins.d.ts +4 -0
- package/dist/node/plugins/h5/plugins.js +161 -0
- package/dist/node/plugins/h5/resolver/module-resolver.d.ts +14 -0
- package/dist/node/plugins/h5/resolver/module-resolver.js +19 -0
- package/dist/node/plugins/h5/transform-app.d.ts +10 -0
- package/dist/node/plugins/h5/transform-app.js +48 -0
- package/dist/node/plugins/wx/dev/dev-host.d.ts +15 -0
- package/dist/node/plugins/wx/dev/dev-host.js +352 -0
- package/dist/node/plugins/wx/dev/hmr-files.d.ts +28 -0
- package/dist/node/plugins/wx/dev/hmr-files.js +34 -0
- package/dist/node/plugins/wx/dev/patch-publisher.d.ts +22 -0
- package/dist/node/plugins/wx/dev/patch-publisher.js +40 -0
- package/dist/node/plugins/wx/dev/plugin.d.ts +7 -0
- package/dist/node/plugins/wx/dev/plugin.js +73 -0
- package/dist/node/plugins/wx/dev/react-refresh.d.ts +20 -0
- package/dist/node/plugins/wx/dev/react-refresh.js +198 -0
- package/dist/node/plugins/wx/module.d.ts +45 -0
- package/dist/node/plugins/wx/module.js +66 -0
- package/dist/node/plugins/wx/native/compile-native-component-interface.d.ts +21 -0
- package/dist/node/plugins/wx/native/compile-native-component-interface.js +29 -0
- package/dist/node/plugins/wx/native/create-native-component-output.d.ts +15 -0
- package/dist/node/plugins/wx/native/create-native-component-output.js +39 -0
- package/dist/node/plugins/wx/native/native-component-assets.d.ts +26 -0
- package/dist/node/plugins/wx/native/native-component-assets.js +76 -0
- package/dist/node/plugins/wx/native/native-component-interface.d.ts +12 -0
- package/dist/node/plugins/wx/native/native-component-interface.js +201 -0
- package/dist/node/plugins/wx/output/files.d.ts +12 -0
- package/dist/node/plugins/wx/output/files.js +12 -0
- package/dist/node/plugins/wx/output/json.d.ts +14 -0
- package/dist/node/plugins/wx/output/json.js +52 -0
- package/dist/node/plugins/wx/output/relative-root.d.ts +2 -0
- package/dist/node/plugins/wx/output/relative-root.js +6 -0
- package/dist/node/plugins/wx/output/templates.d.ts +7 -0
- package/dist/node/plugins/wx/output/templates.js +100 -0
- package/dist/node/plugins/wx/placement/placer.d.ts +78 -0
- package/dist/node/plugins/wx/placement/placer.js +145 -0
- package/dist/node/plugins/wx/placement/plan.d.ts +46 -0
- package/dist/node/plugins/wx/placement/plan.js +210 -0
- package/dist/node/plugins/wx/plugins.d.ts +4 -0
- package/dist/node/plugins/wx/plugins.js +137 -0
- package/dist/node/plugins/wx/render/capsule-wrapper.d.ts +3 -0
- package/dist/node/plugins/wx/render/capsule-wrapper.js +64 -0
- package/dist/node/plugins/wx/render/capsule.d.ts +4 -0
- package/dist/node/plugins/wx/render/capsule.js +13 -0
- package/dist/node/plugins/wx/render/native.d.ts +9 -0
- package/dist/node/plugins/wx/render/native.js +45 -0
- package/dist/node/plugins/wx/render/transport.d.ts +17 -0
- package/dist/node/plugins/wx/render/transport.js +94 -0
- package/dist/node/plugins/wx/resolve/resolver.d.ts +7 -0
- package/dist/node/plugins/wx/resolve/resolver.js +86 -0
- package/dist/node/plugins/wx/resolve/specialize-bootstrap.d.ts +9 -0
- package/dist/node/plugins/wx/resolve/specialize-bootstrap.js +9 -0
- package/dist/node/plugins/wx/resolve/specialize-page-capsule.d.ts +9 -0
- package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +11 -0
- package/dist/node/utils/constant.d.ts +1 -0
- package/dist/node/utils/constant.js +1 -0
- package/dist/node/utils/memoize.d.ts +122 -0
- package/dist/node/utils/memoize.js +86 -0
- package/dist/node/utils/modules.d.ts +20 -24
- package/dist/node/utils/modules.js +21 -34
- package/dist/node/utils/once.d.ts +10 -0
- package/dist/node/utils/once.js +20 -0
- package/dist/node/utils/packages.d.ts +1 -0
- package/dist/node/utils/packages.js +1 -1
- package/dist/node/utils/project-config.d.ts +3 -0
- package/dist/node/utils/project-config.js +8 -0
- package/dist/node/utils/serialized-task-queue.d.ts +9 -0
- package/dist/node/utils/serialized-task-queue.js +17 -0
- package/dist/node/utils/transform.d.ts +11 -0
- package/dist/node/utils/transform.js +58 -0
- package/dist/node/vite-plugin.d.ts +1 -1
- package/dist/node/vite-plugin.js +11 -27
- package/dist/options.d.ts +3 -4
- package/dist/runtime/{taro → client/taro}/api.d.ts +0 -1
- package/dist/runtime/client/taro/api.js +9 -0
- package/dist/runtime/client/taro/component.d.ts +1 -0
- package/dist/runtime/client/taro/component.js +1 -0
- package/dist/runtime/client/taro/define-native-component.d.ts +7 -0
- package/dist/runtime/client/taro/define-native-component.js +4 -0
- package/dist/runtime/h5/app.d.ts +2 -0
- package/dist/runtime/h5/app.js +15 -0
- package/dist/runtime/h5/taro-runtime.d.ts +1 -1
- package/dist/runtime/h5/taro-runtime.js +1 -4
- package/dist/runtime/wx/amphibious/bootstrap.d.ts +4 -0
- package/dist/runtime/wx/amphibious/bootstrap.js +16 -0
- package/dist/runtime/wx/amphibious/transport.d.ts +2 -0
- package/dist/runtime/wx/amphibious/transport.js +4 -0
- package/dist/runtime/wx/capsule/app.d.ts +2 -0
- package/dist/runtime/wx/capsule/app.js +7 -0
- package/dist/runtime/wx/capsule/component.d.ts +3 -0
- package/dist/runtime/wx/capsule/component.js +4 -0
- package/dist/runtime/wx/capsule/page.d.ts +3 -0
- package/dist/runtime/wx/capsule/page.js +68 -0
- package/dist/runtime/wx/capsule/taro-runtime.d.ts +6 -0
- package/dist/runtime/wx/capsule/taro-runtime.js +6 -0
- package/dist/runtime/wx/dev/dev-runtime.d.ts +1 -0
- package/dist/runtime/wx/dev/dev-runtime.js +246 -0
- package/dist/runtime/wx/native/app.d.ts +1 -0
- package/dist/runtime/wx/native/app.js +3 -0
- package/dist/runtime/wx/native/component.d.ts +1 -0
- package/dist/runtime/wx/native/component.js +3 -0
- package/dist/runtime/wx/native/page.d.ts +1 -0
- package/dist/runtime/wx/native/page.js +4 -0
- package/dist/runtime/wx/systemjs/system-core.d.ts +4 -0
- package/dist/runtime/wx/systemjs/system-core.js +331 -0
- package/package.json +21 -14
- package/src/node/plugins/client/client-taro.ts +23 -0
- package/src/node/plugins/client/constant.ts +5 -0
- package/src/node/plugins/{conditional-directives.ts → conditional/conditional-directives.ts} +33 -13
- package/src/node/plugins/css/plugins.ts +96 -0
- package/src/node/plugins/h5/constant.ts +4 -0
- package/src/node/plugins/h5/plugins.ts +197 -0
- package/src/node/plugins/h5/resolver/module-resolver.ts +32 -0
- package/src/node/plugins/h5/transform-app.ts +90 -0
- package/src/node/plugins/wx/dev/dev-host.ts +448 -0
- package/src/node/plugins/wx/dev/hmr-files.ts +55 -0
- package/src/node/plugins/wx/dev/patch-publisher.ts +49 -0
- package/src/node/plugins/wx/dev/plugin.ts +82 -0
- package/src/node/plugins/wx/dev/react-refresh.ts +227 -0
- package/src/node/plugins/wx/module.ts +97 -0
- package/src/node/plugins/wx/native/compile-native-component-interface.ts +47 -0
- package/src/node/plugins/wx/native/create-native-component-output.ts +57 -0
- package/src/node/plugins/wx/native/native-component-assets.ts +114 -0
- package/src/node/plugins/wx/native/native-component-interface.ts +265 -0
- package/src/node/plugins/wx/output/files.ts +27 -0
- package/src/node/plugins/wx/output/json.ts +79 -0
- package/src/node/plugins/wx/output/relative-root.ts +7 -0
- package/src/node/plugins/wx/output/templates.ts +141 -0
- package/src/node/plugins/wx/placement/placer.ts +172 -0
- package/src/node/plugins/wx/placement/plan.ts +306 -0
- package/src/node/plugins/wx/plugins.ts +165 -0
- package/src/node/plugins/wx/render/capsule-wrapper.ts +86 -0
- package/src/node/plugins/wx/render/capsule.ts +21 -0
- package/src/node/plugins/wx/render/native.ts +84 -0
- package/src/node/plugins/wx/render/transport.ts +157 -0
- package/src/node/plugins/wx/resolve/resolver.ts +125 -0
- package/src/node/plugins/wx/resolve/specialize-bootstrap.ts +27 -0
- package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +29 -0
- package/src/node/utils/constant.ts +1 -0
- package/src/node/utils/memoize.ts +149 -0
- package/src/node/utils/modules.ts +36 -38
- package/src/node/utils/once.ts +21 -0
- package/src/node/utils/packages.ts +1 -1
- package/src/node/utils/project-config.ts +10 -0
- package/src/node/utils/serialized-task-queue.ts +20 -0
- package/src/node/utils/transform.ts +82 -0
- package/src/node/vite-plugin.ts +12 -32
- package/src/options.ts +3 -4
- package/src/runtime/client/taro/api.ts +11 -0
- package/src/runtime/client/taro/component.ts +1 -0
- package/src/runtime/client/taro/define-native-component.ts +15 -0
- package/src/runtime/h5/app.ts +25 -0
- package/src/runtime/h5/taro-runtime.ts +1 -4
- package/src/runtime/wx/amphibious/bootstrap.ts +22 -0
- package/src/runtime/wx/amphibious/transport.ts +10 -0
- package/src/runtime/wx/capsule/app.ts +9 -0
- package/src/runtime/wx/capsule/component.ts +6 -0
- package/src/runtime/wx/capsule/page.ts +96 -0
- package/src/runtime/wx/capsule/taro-runtime.ts +14 -0
- package/src/runtime/wx/dev/dev-runtime.ts +317 -0
- package/src/runtime/wx/native/app.ts +4 -0
- package/src/runtime/wx/native/component.ts +4 -0
- package/src/runtime/wx/native/page.ts +6 -0
- package/src/runtime/wx/systemjs/LICENSE +10 -0
- package/src/runtime/wx/systemjs/system-core.js +370 -0
- package/src/runtime/wx/systemjs/system.d.ts +55 -0
- package/src/runtime/wx/wechat.d.ts +52 -0
- package/README.zh.md +0 -521
- package/dist/node/build-context.d.ts +0 -25
- package/dist/node/build-context.js +0 -47
- package/dist/node/css/css-pipeline.d.ts +0 -35
- package/dist/node/css/css-pipeline.js +0 -154
- package/dist/node/plugins/conditional-directives.d.ts +0 -3
- package/dist/node/plugins/taro-runtime.d.ts +0 -5
- package/dist/node/plugins/taro-runtime.js +0 -18
- package/dist/node/targets/h5/plugin.d.ts +0 -6
- package/dist/node/targets/h5/plugin.js +0 -117
- package/dist/node/targets/h5/virtual-modules.d.ts +0 -5
- package/dist/node/targets/h5/virtual-modules.js +0 -60
- package/dist/node/targets/wx/companion-assets.d.ts +0 -19
- package/dist/node/targets/wx/companion-assets.js +0 -129
- package/dist/node/targets/wx/dev-server/development-session.d.ts +0 -32
- package/dist/node/targets/wx/dev-server/development-session.js +0 -215
- package/dist/node/targets/wx/dev-server/full-build-scheduler.d.ts +0 -14
- package/dist/node/targets/wx/dev-server/full-build-scheduler.js +0 -60
- package/dist/node/targets/wx/dev-server/js-utils.d.ts +0 -5
- package/dist/node/targets/wx/dev-server/js-utils.js +0 -28
- package/dist/node/targets/wx/dev-server/output.d.ts +0 -19
- package/dist/node/targets/wx/dev-server/output.js +0 -44
- package/dist/node/targets/wx/dev-server/rolldown-runtime-source.d.ts +0 -8
- package/dist/node/targets/wx/dev-server/rolldown-runtime-source.js +0 -95
- package/dist/node/targets/wx/dev-server/update-server-state.d.ts +0 -65
- package/dist/node/targets/wx/dev-server/update-server-state.js +0 -116
- package/dist/node/targets/wx/dev-server/update-transport.d.ts +0 -28
- package/dist/node/targets/wx/dev-server/update-transport.js +0 -212
- package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.d.ts +0 -44
- package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.js +0 -145
- package/dist/node/targets/wx/development-files.d.ts +0 -8
- package/dist/node/targets/wx/development-files.js +0 -8
- package/dist/node/targets/wx/plugin.d.ts +0 -6
- package/dist/node/targets/wx/plugin.js +0 -144
- package/dist/node/targets/wx/react-refresh.d.ts +0 -4
- package/dist/node/targets/wx/react-refresh.js +0 -38
- package/dist/node/targets/wx/virtual-modules.d.ts +0 -16
- package/dist/node/targets/wx/virtual-modules.js +0 -120
- package/dist/node/utils/async.d.ts +0 -8
- package/dist/node/utils/async.js +0 -16
- package/dist/node/utils/filesystem.d.ts +0 -13
- package/dist/node/utils/filesystem.js +0 -43
- package/dist/runtime/taro/api.js +0 -11
- package/dist/runtime/taro/components.d.ts +0 -2
- package/dist/runtime/taro/components.js +0 -2
- package/dist/runtime/wx/page-update.d.ts +0 -6
- package/dist/runtime/wx/page-update.js +0 -157
- package/dist/runtime/wx/taro-runtime.d.ts +0 -5
- package/dist/runtime/wx/taro-runtime.js +0 -6
- package/dist/runtime/wx/update-client-state.d.ts +0 -68
- package/dist/runtime/wx/update-client-state.js +0 -84
- package/dist/runtime/wx/update-client.d.ts +0 -1
- package/dist/runtime/wx/update-client.js +0 -171
- package/src/node/build-context.ts +0 -62
- package/src/node/css/css-pipeline.ts +0 -171
- package/src/node/plugins/taro-runtime.ts +0 -22
- package/src/node/targets/h5/plugin.ts +0 -135
- package/src/node/targets/h5/virtual-modules.ts +0 -71
- package/src/node/targets/wx/companion-assets.ts +0 -174
- package/src/node/targets/wx/dev-server/development-session.ts +0 -271
- package/src/node/targets/wx/dev-server/full-build-scheduler.ts +0 -62
- package/src/node/targets/wx/dev-server/js-utils.ts +0 -33
- package/src/node/targets/wx/dev-server/output.ts +0 -56
- package/src/node/targets/wx/dev-server/rolldown-runtime-source.ts +0 -95
- package/src/node/targets/wx/dev-server/update-server-state.ts +0 -183
- package/src/node/targets/wx/dev-server/update-transport.ts +0 -261
- package/src/node/targets/wx/dev-server/vite-bundled-dev-adapter.ts +0 -203
- package/src/node/targets/wx/development-files.ts +0 -11
- package/src/node/targets/wx/plugin.ts +0 -164
- package/src/node/targets/wx/react-refresh.ts +0 -51
- package/src/node/targets/wx/virtual-modules.ts +0 -152
- package/src/node/utils/async.ts +0 -19
- package/src/node/utils/filesystem.ts +0 -45
- package/src/runtime/taro/api.ts +0 -13
- package/src/runtime/taro/components.ts +0 -2
- package/src/runtime/wx/page-update.ts +0 -230
- package/src/runtime/wx/react-refresh.d.ts +0 -20
- package/src/runtime/wx/taro-runtime.ts +0 -7
- package/src/runtime/wx/update-client-state.ts +0 -138
- package/src/runtime/wx/update-client.ts +0 -223
package/dist/node/plugins/{conditional-directives.js → conditional/conditional-directives.js}
RENAMED
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
import { normalizeModuleId } from '
|
|
2
|
-
|
|
1
|
+
import { normalizeModuleId } from '../../utils/modules.js';
|
|
2
|
+
/** Creates source-level target conditional handling shared by H5 and WX. */
|
|
3
|
+
export function createConditionalDirectivePlugin(target) {
|
|
3
4
|
return {
|
|
4
|
-
name: '
|
|
5
|
+
name: 'vpt:conditional-directives',
|
|
5
6
|
enforce: 'pre',
|
|
6
7
|
transform(code, id) {
|
|
7
|
-
if (!isConditionalDirectiveSource(id) || !code.includes('#if'))
|
|
8
|
+
if (!isConditionalDirectiveSource(id) || !code.includes('#if')) {
|
|
8
9
|
return;
|
|
9
|
-
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
code: transformConditionalDirectives(code, target),
|
|
13
|
+
map: null
|
|
14
|
+
};
|
|
10
15
|
}
|
|
11
16
|
};
|
|
12
17
|
}
|
|
18
|
+
/** Tests whether an application source can contain conditional directives. */
|
|
13
19
|
function isConditionalDirectiveSource(id) {
|
|
14
20
|
const normalizedId = normalizeModuleId(id);
|
|
15
21
|
return !normalizedId.includes('/node_modules/') && /\.(?:[cm]?[jt]sx?|css|s[ac]ss|less|styl)$/.test(normalizedId);
|
|
16
22
|
}
|
|
23
|
+
/** Keeps only the source branches active for one build target. */
|
|
17
24
|
function transformConditionalDirectives(code, target) {
|
|
18
25
|
const lines = code.match(/[^\n]*(?:\n|$)/g) ?? [];
|
|
19
26
|
const frames = [];
|
|
20
27
|
let transformed = '';
|
|
21
28
|
for (const line of lines) {
|
|
22
|
-
if (!line)
|
|
29
|
+
if (!line) {
|
|
23
30
|
continue;
|
|
31
|
+
}
|
|
24
32
|
const directive = parseDirective(line);
|
|
25
33
|
const lineEnding = line.match(/\r?\n$/)?.[0] ?? '';
|
|
26
34
|
if (!directive) {
|
|
@@ -31,12 +39,17 @@ function transformConditionalDirectives(code, target) {
|
|
|
31
39
|
const parentActive = frames.every((frame) => frame.active);
|
|
32
40
|
const targetMatched = directive.target.toLowerCase() === target;
|
|
33
41
|
const matched = directive.name === 'ifdef' ? targetMatched : !targetMatched;
|
|
34
|
-
frames.push({
|
|
42
|
+
frames.push({
|
|
43
|
+
parentActive,
|
|
44
|
+
matched,
|
|
45
|
+
active: parentActive && matched
|
|
46
|
+
});
|
|
35
47
|
}
|
|
36
48
|
else if (directive.name === 'else') {
|
|
37
49
|
const frame = frames.at(-1);
|
|
38
|
-
if (frame)
|
|
50
|
+
if (frame) {
|
|
39
51
|
frame.active = frame.parentActive && !frame.matched;
|
|
52
|
+
}
|
|
40
53
|
}
|
|
41
54
|
else {
|
|
42
55
|
frames.pop();
|
|
@@ -45,16 +58,19 @@ function transformConditionalDirectives(code, target) {
|
|
|
45
58
|
}
|
|
46
59
|
return transformed;
|
|
47
60
|
}
|
|
61
|
+
/** Parses one supported Taro conditional directive. */
|
|
48
62
|
function parseDirective(line) {
|
|
49
63
|
const match = line.match(/^\s*(?:(?:\/\/)|(?:\/\*))\s*#(ifdef|ifndef|if|elif|else|endif)\b([^*\r\n]*)/);
|
|
50
|
-
if (!match)
|
|
64
|
+
if (!match) {
|
|
51
65
|
return;
|
|
66
|
+
}
|
|
52
67
|
const name = match[1];
|
|
53
68
|
if (name === 'if' || name === 'elif') {
|
|
54
|
-
throw new Error(`
|
|
69
|
+
throw new Error(`vpt no longer supports #${name}; use #ifdef, #ifndef, or #else`);
|
|
55
70
|
}
|
|
56
|
-
if (name !== 'ifdef' && name !== 'ifndef' && name !== 'else' && name !== 'endif')
|
|
71
|
+
if (name !== 'ifdef' && name !== 'ifndef' && name !== 'else' && name !== 'endif') {
|
|
57
72
|
return;
|
|
73
|
+
}
|
|
58
74
|
return {
|
|
59
75
|
name,
|
|
60
76
|
target: match[2]?.replace(/\*\/$/, '').trim() ?? ''
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { createStyleHandler } from '@weapp-tailwindcss/postcss';
|
|
3
|
+
import { WeappTailwindcss } from 'weapp-tailwindcss/vite';
|
|
4
|
+
import { packageRequire } from '../../utils/packages.js';
|
|
5
|
+
// Keep the Vite plugin and the compatibility finalizer on identical WX conversion settings. A difference here can
|
|
6
|
+
// make the second pass preserve browser units or apply a transformation that the first pass did not expect.
|
|
7
|
+
const tailwindcssBasedir = path.dirname(packageRequire.resolve('tailwindcss/package.json'));
|
|
8
|
+
const wxStyleOptions = {
|
|
9
|
+
cssCalc: false,
|
|
10
|
+
autoprefixer: false,
|
|
11
|
+
rem2rpx: true,
|
|
12
|
+
px2rpx: true
|
|
13
|
+
};
|
|
14
|
+
/** Creates the target-aware Tailwind CSS plugins. */
|
|
15
|
+
export function createCssPlugins(target) {
|
|
16
|
+
const wx = target === 'wx';
|
|
17
|
+
return [
|
|
18
|
+
...(WeappTailwindcss({
|
|
19
|
+
appType: 'weapp-vite',
|
|
20
|
+
// WX must enable this for split Tailwind imports such as `tailwindcss/theme.css`. Otherwise Vite's
|
|
21
|
+
// PostCSS resolver tries to resolve those imports from the application and fails when Tailwind is owned
|
|
22
|
+
// by vpt. The web generator consumes the imports before that resolver runs, so H5 keeps the
|
|
23
|
+
// upstream default.
|
|
24
|
+
rewriteCssImports: wx,
|
|
25
|
+
// Tailwind is a plugin dependency, not an application dependency. Give weapp-tailwindcss the owning package
|
|
26
|
+
// directory explicitly so bundled development and strict package managers resolve split CSS imports equally.
|
|
27
|
+
tailwindcssBasedir,
|
|
28
|
+
generator: {
|
|
29
|
+
target: wx ? 'weapp' : 'web'
|
|
30
|
+
// webCompat: {
|
|
31
|
+
// preset: 'legacy-web'
|
|
32
|
+
// }
|
|
33
|
+
},
|
|
34
|
+
cssOptions: {
|
|
35
|
+
...wxStyleOptions,
|
|
36
|
+
autoprefixer: !wx
|
|
37
|
+
},
|
|
38
|
+
logLevel: 'warn'
|
|
39
|
+
}) ?? []),
|
|
40
|
+
wx ? createWxssCompatibilityFinalizer() : undefined
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Completes WXSS adaptation that weapp-tailwindcss leaves pending after rewriting split Tailwind imports.
|
|
45
|
+
*
|
|
46
|
+
* `rewriteCssImports: true` makes the early Vite transform generate the Tailwind CSS, but the non-web generator
|
|
47
|
+
* also defers CSS adaptation. The generated asset is consequently browser-shaped
|
|
48
|
+
* CSS containing values and syntax such as `rem`, escaped class selectors, and `@property`. It is then recorded as a
|
|
49
|
+
* processed Vite asset, so the upstream output finalizer does not perform the missing complete WXSS adaptation.
|
|
50
|
+
*
|
|
51
|
+
* This plugin runs after the upstream finalizer, repeats only the compatibility transform, and restores the captured
|
|
52
|
+
* global asset to WeChat's required `app.wxss` path. Exact path correlation leaves Page WXSS companions untouched.
|
|
53
|
+
* Remove it when upstream both completes adaptation and preserves the bundler-selected filename.
|
|
54
|
+
*/
|
|
55
|
+
function createWxssCompatibilityFinalizer() {
|
|
56
|
+
// This compatibility pass only needs the PostCSS style pipeline. Creating a complete weapp-tailwindcss context here
|
|
57
|
+
// would initialize another Tailwind compiler and source scanner for CSS that the upstream Vite plugin already generated.
|
|
58
|
+
const transformWxss = createStyleHandler(wxStyleOptions);
|
|
59
|
+
return {
|
|
60
|
+
name: 'vpt:wxss-compatibility-finalizer',
|
|
61
|
+
enforce: 'post',
|
|
62
|
+
generateBundle: {
|
|
63
|
+
order: 'post',
|
|
64
|
+
async handler(_, bundle) {
|
|
65
|
+
// With cssCodeSplit: false the global style is the only asset with a logical
|
|
66
|
+
// .css source name, even when an earlier hook already changed its final suffix.
|
|
67
|
+
const globalStyleAsset = Object.values(bundle).find((output) => output.type === 'asset' &&
|
|
68
|
+
(output.fileName.replaceAll('\\', '/').endsWith('.css') ||
|
|
69
|
+
output.names.some((name) => name.replaceAll('\\', '/').endsWith('.css'))));
|
|
70
|
+
if (!globalStyleAsset)
|
|
71
|
+
return;
|
|
72
|
+
// Both finalizers use a post-ordered generateBundle hook. Array order places this hook after the
|
|
73
|
+
// upstream finalizer, where the single Vite global style asset has its final contents and filename.
|
|
74
|
+
const source = typeof globalStyleAsset.source === 'string'
|
|
75
|
+
? globalStyleAsset.source
|
|
76
|
+
: new TextDecoder().decode(globalStyleAsset.source);
|
|
77
|
+
if (source.length > 0) {
|
|
78
|
+
globalStyleAsset.source = (await transformWxss(source)).css;
|
|
79
|
+
}
|
|
80
|
+
globalStyleAsset.fileName = 'app.wxss';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { types } from '@babel/core';
|
|
2
|
+
import babel from '@rolldown/plugin-babel';
|
|
3
|
+
import { esTarget } from '../../utils/constant.js';
|
|
4
|
+
import { toViteFileImportPath } from '../../utils/modules.js';
|
|
5
|
+
import { packageRequire } from '../../utils/packages.js';
|
|
6
|
+
import { clientTaroApiId } from '../client/client-taro.js';
|
|
7
|
+
import { h5AppPath } from './constant.js';
|
|
8
|
+
import { createModuleResolver } from './resolver/module-resolver.js';
|
|
9
|
+
/** Creates the plugins that own the H5 target. */
|
|
10
|
+
export function createH5TargetPlugins(options) {
|
|
11
|
+
return [...createH5SupportPlugins(), createH5TargetPlugin(options)];
|
|
12
|
+
}
|
|
13
|
+
/** Configures H5 resolution and supplies the specialized physical application entry. */
|
|
14
|
+
function createH5TargetPlugin(options) {
|
|
15
|
+
const moduleResolver = createModuleResolver(options);
|
|
16
|
+
return {
|
|
17
|
+
name: 'vpt:h5',
|
|
18
|
+
config() {
|
|
19
|
+
return {
|
|
20
|
+
define: createH5Defines(),
|
|
21
|
+
resolve: {
|
|
22
|
+
mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
|
|
23
|
+
alias: [
|
|
24
|
+
{
|
|
25
|
+
find: /^@stencil\/core\/internal\/client$/,
|
|
26
|
+
replacement: packageRequire.resolve('@stencil/core/internal/client', {
|
|
27
|
+
paths: [packageRequire.resolve('@tarojs/components/package.json')]
|
|
28
|
+
})
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
find: /^@tarojs\/components$/,
|
|
32
|
+
replacement: packageRequire.resolve('@tarojs/components/lib/react')
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
find: /^@tarojs\/components\/dist\/components$/,
|
|
36
|
+
replacement: packageRequire.resolve('@tarojs/components/dist/components')
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
find: /^@tarojs\/taro$/,
|
|
40
|
+
replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
optimizeDeps: {
|
|
45
|
+
exclude: ['@stencil/core/internal/client']
|
|
46
|
+
},
|
|
47
|
+
build: {
|
|
48
|
+
target: esTarget
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
resolveId: {
|
|
53
|
+
order: 'pre',
|
|
54
|
+
handler(id) {
|
|
55
|
+
return moduleResolver.resolveId({
|
|
56
|
+
id,
|
|
57
|
+
projectRoot: this.environment.config.root
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
transform: {
|
|
62
|
+
order: 'pre',
|
|
63
|
+
handler(code, id) {
|
|
64
|
+
return moduleResolver.transform({
|
|
65
|
+
code,
|
|
66
|
+
id,
|
|
67
|
+
projectRoot: this.environment.config.root,
|
|
68
|
+
sourcemap: Boolean(this.environment.config.build.sourcemap)
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
transformIndexHtml: {
|
|
73
|
+
order: 'pre',
|
|
74
|
+
handler() {
|
|
75
|
+
return createH5IndexHtmlTags();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** Injects the physical H5 App into the application document. */
|
|
81
|
+
function createH5IndexHtmlTags() {
|
|
82
|
+
return [
|
|
83
|
+
{
|
|
84
|
+
tag: 'script',
|
|
85
|
+
attrs: {
|
|
86
|
+
type: 'module'
|
|
87
|
+
},
|
|
88
|
+
children: `import '${toViteFileImportPath(h5AppPath)}'`,
|
|
89
|
+
injectTo: 'body'
|
|
90
|
+
}
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
/** Creates H5-only Babel transforms for Stencil CSS ordering and Taro API imports. */
|
|
94
|
+
function createH5SupportPlugins() {
|
|
95
|
+
return [
|
|
96
|
+
babel({
|
|
97
|
+
include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
|
|
98
|
+
exclude: [],
|
|
99
|
+
plugins: [rewriteStencilStyleInsertion]
|
|
100
|
+
}),
|
|
101
|
+
babel({
|
|
102
|
+
plugins: [
|
|
103
|
+
[
|
|
104
|
+
packageRequire.resolve('babel-plugin-transform-taroapi'),
|
|
105
|
+
{
|
|
106
|
+
packageName: clientTaroApiId,
|
|
107
|
+
definition: packageRequire(packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
]
|
|
111
|
+
})
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
/** Keeps Stencil-injected Taro component styles before application stylesheets. */
|
|
115
|
+
function rewriteStencilStyleInsertion() {
|
|
116
|
+
return {
|
|
117
|
+
name: 'vpt:rewrite-stencil-style-insertion',
|
|
118
|
+
visitor: {
|
|
119
|
+
CallExpression(callPath) {
|
|
120
|
+
if (!isStencilStyleInsertBeforeCall(callPath)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
callPath
|
|
124
|
+
.get('arguments.1')
|
|
125
|
+
.replaceWith(types.conditionalExpression(types.callExpression(types.memberExpression(types.identifier('scopeId'), types.identifier('startsWith')), [types.stringLiteral('sc-taro-')]), createStyleQuery('style,link[rel="stylesheet"]'), createStyleQuery('link')));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Identifies Stencil's default component-style insertion call. */
|
|
131
|
+
function isStencilStyleInsertBeforeCall(callPath) {
|
|
132
|
+
const { callee, arguments: callArguments } = callPath.node;
|
|
133
|
+
return (types.isMemberExpression(callee) &&
|
|
134
|
+
types.isIdentifier(callee.object, { name: 'styleContainerNode' }) &&
|
|
135
|
+
types.isIdentifier(callee.property, { name: 'insertBefore' }) &&
|
|
136
|
+
types.isIdentifier(callArguments[0], { name: 'styleElm' }) &&
|
|
137
|
+
isStyleQuery(callArguments[1], 'link'));
|
|
138
|
+
}
|
|
139
|
+
/** Identifies one style-container querySelector call. */
|
|
140
|
+
function isStyleQuery(node, selector) {
|
|
141
|
+
return (types.isCallExpression(node) &&
|
|
142
|
+
types.isMemberExpression(node.callee) &&
|
|
143
|
+
types.isIdentifier(node.callee.object, { name: 'styleContainerNode' }) &&
|
|
144
|
+
types.isIdentifier(node.callee.property, { name: 'querySelector' }) &&
|
|
145
|
+
types.isStringLiteral(node.arguments[0], { value: selector }));
|
|
146
|
+
}
|
|
147
|
+
/** Creates one style-container querySelector call. */
|
|
148
|
+
function createStyleQuery(selector) {
|
|
149
|
+
return types.callExpression(types.memberExpression(types.identifier('styleContainerNode'), types.identifier('querySelector')), [types.stringLiteral(selector)]);
|
|
150
|
+
}
|
|
151
|
+
/** Creates H5 Taro compile-time constants. */
|
|
152
|
+
function createH5Defines() {
|
|
153
|
+
return {
|
|
154
|
+
'process.env.FRAMEWORK': JSON.stringify('react'),
|
|
155
|
+
'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
|
|
156
|
+
'process.env.TARO_ENV': JSON.stringify('h5'),
|
|
157
|
+
'process.env.TARO_PLATFORM': JSON.stringify('web'),
|
|
158
|
+
'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
|
|
159
|
+
DEPRECATED_ADAPTER_COMPONENT: 'false'
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { VitePluginTaroOptions } from '../../../../options.ts';
|
|
2
|
+
/** Creates H5 application module resolution and specialization. */
|
|
3
|
+
export declare function createModuleResolver(options: VitePluginTaroOptions): {
|
|
4
|
+
resolveId({ id, projectRoot }: {
|
|
5
|
+
id: string;
|
|
6
|
+
projectRoot: string;
|
|
7
|
+
}): string | undefined;
|
|
8
|
+
transform({ code, id, projectRoot, sourcemap }: {
|
|
9
|
+
code: string;
|
|
10
|
+
id: string;
|
|
11
|
+
projectRoot: string;
|
|
12
|
+
sourcemap?: boolean;
|
|
13
|
+
}): Promise<import("../../../utils/transform.ts").AstTransformResult> | undefined;
|
|
14
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { normalizeModuleId, resolveAppComponentPath } from '../../../utils/modules.js';
|
|
2
|
+
import { appComponentId } from '../../client/constant.js';
|
|
3
|
+
import { h5AppPath } from '../constant.js';
|
|
4
|
+
import { transformH5App } from '../transform-app.js';
|
|
5
|
+
/** Creates H5 application module resolution and specialization. */
|
|
6
|
+
export function createModuleResolver(options) {
|
|
7
|
+
return {
|
|
8
|
+
resolveId({ id, projectRoot }) {
|
|
9
|
+
if (id === appComponentId) {
|
|
10
|
+
return resolveAppComponentPath({ appPath: options.app, projectRoot });
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
transform({ code, id, projectRoot, sourcemap = true }) {
|
|
14
|
+
if (normalizeModuleId(id) === normalizeModuleId(h5AppPath)) {
|
|
15
|
+
return transformH5App({ code, id, options, projectRoot, sourcemap });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { VitePluginTaroOptions } from '../../../options.ts';
|
|
2
|
+
import { type AstTransformResult } from '../../utils/transform.ts';
|
|
3
|
+
/** Specializes the physical H5 App for one configured project. */
|
|
4
|
+
export declare function transformH5App({ code, id, options, projectRoot, sourcemap }: {
|
|
5
|
+
code: string;
|
|
6
|
+
id: string;
|
|
7
|
+
options: VitePluginTaroOptions;
|
|
8
|
+
projectRoot: string;
|
|
9
|
+
sourcemap?: boolean;
|
|
10
|
+
}): Promise<AstTransformResult>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { types } from '@babel/core';
|
|
2
|
+
import { createPageComponentImportPath } from '../../utils/modules.js';
|
|
3
|
+
import { createAppConfig } from '../../utils/project-config.js';
|
|
4
|
+
import { replaceWithAst } from '../../utils/transform.js';
|
|
5
|
+
const appConfigPlaceholder = '__VITE_PLUGIN_TARO_H5_APP_CONFIG__';
|
|
6
|
+
const routesPlaceholder = '__VITE_PLUGIN_TARO_H5_ROUTES__';
|
|
7
|
+
/** Specializes the physical H5 App for one configured project. */
|
|
8
|
+
export async function transformH5App({ code, id, options, projectRoot, sourcemap = true }) {
|
|
9
|
+
const transformed = await replaceWithAst(code, id, {
|
|
10
|
+
[appConfigPlaceholder]: types.valueToNode({
|
|
11
|
+
router: {},
|
|
12
|
+
...createAppConfig(options)
|
|
13
|
+
}),
|
|
14
|
+
[routesPlaceholder]: types.arrayExpression(options.pages.map((page) => {
|
|
15
|
+
return createRoute({ page, projectRoot });
|
|
16
|
+
}))
|
|
17
|
+
}, sourcemap);
|
|
18
|
+
return { code: transformed.code, map: transformed.map };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates one lazy H5 route shaped like:
|
|
22
|
+
* {
|
|
23
|
+
* path: 'pages/home/index',
|
|
24
|
+
* load: async function (context, params) {
|
|
25
|
+
* const page = await import('/@fs/project/src/pages/home/index.tsx')
|
|
26
|
+
* return [page, context, params]
|
|
27
|
+
* },
|
|
28
|
+
* ...pageConfig
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
function createRoute({ page, projectRoot }) {
|
|
32
|
+
const pageComponentPath = createPageComponentImportPath({ pagePath: page.path, projectRoot });
|
|
33
|
+
const load = types.functionExpression(null, [types.identifier('context'), types.identifier('params')], types.blockStatement([
|
|
34
|
+
types.variableDeclaration('const', [
|
|
35
|
+
types.variableDeclarator(types.identifier('page'), types.awaitExpression(types.importExpression(types.stringLiteral(pageComponentPath))))
|
|
36
|
+
]),
|
|
37
|
+
types.returnStatement(types.arrayExpression([
|
|
38
|
+
types.identifier('page'),
|
|
39
|
+
types.identifier('context'),
|
|
40
|
+
types.identifier('params')
|
|
41
|
+
]))
|
|
42
|
+
]), false, true);
|
|
43
|
+
return types.objectExpression([
|
|
44
|
+
types.objectProperty(types.identifier('path'), types.stringLiteral(page.path)),
|
|
45
|
+
types.objectProperty(types.identifier('load'), load),
|
|
46
|
+
types.spreadElement(types.valueToNode(page.config))
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ViteDevServer } from 'vite';
|
|
2
|
+
import type { VitePluginTaroOptions } from '../../../../options.ts';
|
|
3
|
+
export type WxDevHost = Readonly<{
|
|
4
|
+
close: () => Promise<void>;
|
|
5
|
+
}>;
|
|
6
|
+
/**
|
|
7
|
+
* Creates the wx dev host: the adapter that owns the physical Rolldown DevEngine (created
|
|
8
|
+
* with dev(...)) and the patch publisher, and replaces Vite's bundledDev.listen so the
|
|
9
|
+
* engine writes directly to the Mini Program output directory instead of serving browser
|
|
10
|
+
* HMR over HTTP.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createWxDevHost({ server, options }: {
|
|
13
|
+
server: ViteDevServer;
|
|
14
|
+
options: VitePluginTaroOptions;
|
|
15
|
+
}): Promise<WxDevHost>;
|