@@ -10,9 +10,19 @@ import { GeneratorError, GeneratorErrorType } from '../../utils/error'
1010
1111import 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}
0 commit comments