Skip to content

Commit 0172bb4

Browse files
Single-Dancerchenjiajun79
andauthored
fix(taro-plugin-generator): vite runner 配置 (#17929)
Co-authored-by: chenjiajun79 <[email protected]>
1 parent d1d8b7d commit 0172bb4

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

packages/taro-cli/templates/default/package.json.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"@types/node": "^20"
113113
}{{/if}}{{#if (eq compiler "Vite") }}
114114
"devDependencies": {
115+
"@tarojs/plugin-generator": "{{ version }}",
115116
"@babel/core": "^7.24.4",
116117
"@babel/plugin-transform-class-properties": "7.25.9",
117118
"@tarojs/cli": "{{ version }}",

packages/taro-plugin-generator/src/generators/es5/config.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ import { GeneratorError, GeneratorErrorType } from '../../utils/error'
1010

1111
import type { IPluginContext } from '@tarojs/service'
1212

13-
const modifiedConfigError = new GeneratorError({
14-
type: GeneratorErrorType.modifyConfig,
15-
message: dedent(`
13+
const createModifiedConfigError = (compilerType: CompilerType) =>
14+
new GeneratorError({
15+
type: GeneratorErrorType.modifyConfig,
16+
message:
17+
compilerType === 'vite'
18+
? dedent(`
19+
{
20+
h5: {
21+
legacy: true,
22+
}
23+
}
24+
`)
25+
: dedent(`
1626
{
1727
mini: {
1828
compile: {
@@ -30,13 +40,15 @@ const modifiedConfigError = new GeneratorError({
3040
}
3141
}
3242
`),
33-
})
43+
})
3444

3545
/**
3646
* 更新配置文件
3747
*/
38-
export async function updateConfig(ctx: IPluginContext) {
48+
export async function updateConfig(options: { ctx: IPluginContext, compilerType: CompilerType }) {
49+
const { ctx, compilerType } = options
3950
const { fs } = ctx.helper
51+
4052
const sourceCode = await fs.readFile(ctx.paths.configPath, { encoding: 'utf-8' })
4153

4254
const ast = parser.parse(sourceCode, { sourceType: 'module', plugins: ['typescript'] })
@@ -47,24 +59,45 @@ export async function updateConfig(ctx: IPluginContext) {
4759
ObjectProperty: (o) => {
4860
const { node } = o
4961
if (t.isIdentifier(node.key) && node.key.name === 'mini' && t.isObjectExpression(node.value)) {
50-
modifyCompileConfig(node.value)
62+
if (compilerType === 'webpack5') {
63+
modifyWebpackCompileConfig(node.value)
64+
} else {
65+
// vite-runner 小程序看着不支持 legacy 字段
66+
}
5167
miniUpdated = true
5268
}
5369
if (t.isIdentifier(node.key) && node.key.name === 'h5' && t.isObjectExpression(node.value)) {
54-
modifyCompileConfig(node.value)
70+
if (compilerType === 'webpack5') {
71+
modifyWebpackCompileConfig(node.value)
72+
} else {
73+
modifyViteCompileConfig(node.value)
74+
}
5575
h5Updated = true
5676
}
5777
},
5878
})
5979
if (!miniUpdated || !h5Updated) {
60-
throw modifiedConfigError
80+
throw createModifiedConfigError(compilerType)
6181
}
6282
const { code } = generate(ast)
6383
await fs.outputFile(ctx.paths.configPath, code, { encoding: 'utf-8' })
6484
console.log('✅ 更新配置文件成功\n')
6585
}
6686

67-
function modifyCompileConfig(config: t.ObjectExpression) {
87+
function modifyViteCompileConfig(config: t.ObjectExpression) {
88+
const legacyProp = config.properties.find(
89+
(p) => t.isObjectProperty(p) && t.isIdentifier(p.key) && p.key.name === 'legacy'
90+
)
91+
if (!legacyProp) {
92+
config.properties.push(t.objectProperty(t.identifier('legacy'), t.booleanLiteral(true)))
93+
} else if (t.isObjectProperty(legacyProp)) {
94+
legacyProp.value = t.booleanLiteral(true)
95+
} else {
96+
throw createModifiedConfigError('vite')
97+
}
98+
}
99+
100+
function modifyWebpackCompileConfig(config: t.ObjectExpression) {
68101
ensureNestedObjectProperty(config, ['compile'])
69102
const compileProp = config.properties.find(
70103
(p) => t.isObjectProperty(p) && t.isIdentifier(p.key) && p.key.name === 'compile'
@@ -94,7 +127,7 @@ function modifyCompileConfig(config: t.ObjectExpression) {
94127
} else if (t.isObjectProperty(includeProp) && t.isArrayExpression(includeProp.value)) {
95128
includeProp.value.elements.push(include)
96129
} else {
97-
throw modifiedConfigError
130+
throw createModifiedConfigError('webpack5')
98131
}
99132
}
100133
}

packages/taro-plugin-generator/src/generators/es5/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/* eslint-disable no-console */
2-
import { readPkgJson } from '../../utils'
2+
import { getCompilerType, readPkgJson } from '../../utils'
33
import { updateBabelConfig } from './babel'
44
import { updateConfig } from './config'
55

66
import type { IPluginContext } from '@tarojs/service'
77

88
export async function es5Generator(ctx: IPluginContext) {
9+
const compilerType = getCompilerType(ctx.initialConfig.compiler)
10+
if (!compilerType) {
11+
throw new Error('未找到编译器类型')
12+
}
913
await updateBrowserList(ctx)
10-
await updateConfig(ctx)
14+
await updateConfig({ ctx, compilerType })
1115
await updateBabelConfig(ctx)
1216
console.log('✅ 启用「编译为 ES5」成功')
1317
}
@@ -20,10 +24,7 @@ async function updateBrowserList(ctx: IPluginContext) {
2024
return
2125
}
2226
const pkgJson = readPkgJson(ctx)
23-
pkgJson.browserslist = {
24-
development: ['defaults and fully supports es6-module', 'maintained node versions'],
25-
production: ['last 3 versions', 'Android >= 4.1', 'ios >= 8'],
26-
}
27+
pkgJson.browserslist = ['last 3 versions', 'Android >= 4.1', 'ios >= 8']
2728
await fs.outputJson(`${ctx.paths.appPath}/package.json`, pkgJson, {
2829
encoding: 'utf-8',
2930
spaces: 2,

0 commit comments

Comments
 (0)