Skip to content

Commit 4a4d818

Browse files
zhengyanan1zhengyanan18
andauthored
taro 4.x主包适配效能插件 (#17492)
* feat: cli安装后,如果内网自动安装效能插件 * fix: fetch换成axios,兼容3.x * feat: 精细化控制,Taro项目内执行某命令,只加载该命令相关的的全局插件 * fix: 更新效能插件包名 * fix: 全局命令默认加载所有全局插件 * feat: 1.暴露viteCompilerContext给modifyViteConfig;2.vite编译h5时补充onBuildFinish钩子 * refactor: 移除无用代码 * fix: 使用项目里的taro * fix: 修正项目内taro的引用路径 * refactor: review微调 * docs: 微调日志 --------- Co-authored-by: zhengyanan18 <zhengyanan18@jd.com>
1 parent 8ae3f2d commit 4a4d818

File tree

11 files changed

+74
-15
lines changed

11 files changed

+74
-15
lines changed

packages/taro-cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"types": "dist/index.d.ts",
99
"scripts": {
1010
"prod": "pnpm run build",
11+
"postinstall": "node postinstall.js",
1112
"test": "cross-env NODE_ENV=test jest",
1213
"test:ci": "cross-env NODE_ENV=test jest --ci -i --coverage --silent",
1314
"test:dev": "cross-env NODE_ENV=test jest --watch",
@@ -23,6 +24,7 @@
2324
"src",
2425
"dist",
2526
"templates",
27+
"postinstall.js",
2628
"index.js",
2729
"global.d.ts"
2830
],

packages/taro-cli/postinstall.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { exec } = require('child_process')
2+
const axios = require('axios')
3+
4+
axios.get('https://taro.jd.com/', { timeout: 5000 })
5+
.then(() => {
6+
exec('./bin/taro global-config add-plugin @jdtaro/plugin-build-report-performance@latest --registry http://registry.m.jd.com', (error, _stdout, _stderr) => {
7+
if (error) {
8+
console.error(`install performance plugin error: ${error}`)
9+
}
10+
})
11+
console.log('cli postinstall success')
12+
})
13+
.catch(() => {
14+
})

packages/taro-cli/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as minimist from 'minimist'
77
import customCommand from './commands/customCommand'
88
import { getPkgVersion } from './util'
99

10-
const DISABLE_GLOBAL_CONFIG_COMMANDS = ['build', 'global-config', 'doctor', 'update', 'config']
10+
const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config']
1111
const DEFAULT_FRAMEWORK = 'react'
1212

1313
export default class CLI {

packages/taro-cli/src/presets/commands/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,14 @@ export default (ctx: IPluginContext) => {
145145
},
146146
})
147147
},
148-
async modifyViteConfig(viteConfig, data) {
148+
async modifyViteConfig(viteConfig, data, viteCompilerContext) {
149149
await ctx.applyPlugins({
150150
name: hooks.MODIFY_VITE_CONFIG,
151151
initialVal: viteConfig,
152152
opts: {
153153
viteConfig,
154154
data,
155+
viteCompilerContext
155156
},
156157
})
157158
},

packages/taro-service/src/Config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import * as ora from 'ora'
1616
import { merge } from 'webpack-merge'
1717

18+
import { filterGlobalConfig } from './utils'
1819
import {
1920
CONFIG_DIR_NAME,
2021
DEFAULT_CONFIG_FILE
@@ -52,6 +53,7 @@ export default class Config {
5253
if (this.disableGlobalConfig) return
5354
this.initGlobalConfig()
5455
} else {
56+
this.initGlobalConfig(configEnv.command)
5557
createSwcRegister({
5658
only: [
5759
filePath => filePath.indexOf(path.join(this.appPath, CONFIG_DIR_NAME)) >= 0
@@ -67,14 +69,15 @@ export default class Config {
6769
}
6870
}
6971

70-
initGlobalConfig () {
72+
initGlobalConfig (command: string = '') {
7173
const homedir = getUserHomeDir()
7274
if (!homedir) return console.error('获取不到用户 home 路径')
7375
const globalPluginConfigPath = path.join(getUserHomeDir(), TARO_GLOBAL_CONFIG_DIR, TARO_GLOBAL_CONFIG_FILE)
7476
if (!fs.existsSync(globalPluginConfigPath)) return
7577
const spinner = ora(`开始获取 taro 全局配置文件: ${globalPluginConfigPath}`).start()
7678
try {
7779
this.initialGlobalConfig = fs.readJSONSync(globalPluginConfigPath) || {}
80+
this.initialGlobalConfig = filterGlobalConfig(this.initialGlobalConfig, command)
7881
spinner.succeed('获取 taro 全局配置成功')
7982
} catch (e) {
8083
spinner.stop()

packages/taro-service/src/utils/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as resolve from 'resolve'
66

77
import { PluginType } from './constants'
88

9-
import type { PluginItem } from '@tarojs/taro/types/compile'
9+
import type { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile'
1010
import type { IPlugin, IPluginsObject } from './types'
1111

1212
export const isNpmPkg: (name: string) => boolean = name => !(/^(\.|\/)/.test(name))
@@ -117,3 +117,19 @@ export function printHelpLog (command, optionsList: Map<string, string>, synopsi
117117
})
118118
}
119119
}
120+
121+
export function filterGlobalConfig (globalConfig: IProjectConfig, command: string) {
122+
if (!command) {
123+
return globalConfig
124+
}
125+
const config = globalConfig
126+
127+
const RelatedPluginTag = `@jdtaro/plugin-${command}-`
128+
if (config.plugins?.length) {
129+
config.plugins = config.plugins.filter(pluginName => {
130+
return pluginName.includes(RelatedPluginTag)
131+
})
132+
}
133+
134+
return config
135+
}

packages/taro-service/src/utils/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export declare interface IPluginContext {
151151
/**
152152
* 编译中修改 vite 配置
153153
*/
154-
modifyViteConfig: (fn: (args: { viteConfig: any, data?: IModifyChainData }) => void) => void
154+
modifyViteConfig: (fn: (args: { viteConfig: any, data?: IModifyChainData, viteCompilerContext: any }) => void) => void
155155
/**
156156
* 修改编译后的结果
157157
*/

packages/taro-vite-runner/src/h5/pipeline.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { VITE_COMPILER_LABEL } from '@tarojs/runner-utils'
2+
import { isFunction } from '@tarojs/shared'
23

34
import { getMode } from '../utils'
45

56
import type{ ViteH5CompilerContext } from '@tarojs/taro/types/compile/viteCompilerContext'
67
import type { PluginOption } from 'vite'
78

89
export default function (viteCompilerContext: ViteH5CompilerContext): PluginOption {
10+
const { taroConfig } = viteCompilerContext
911
return {
1012
name: 'taro:vite-h5-pipeline',
1113
enforce: 'pre',
1214
async buildStart () {
13-
const { taroConfig } = viteCompilerContext
1415
const isProd = getMode(taroConfig) === 'production'
1516
// 下面这么写 是因为生产环境不需要异步,开发环境需要异步。是因为插件的执行顺序正确而这么写的
1617
isProd
@@ -25,6 +26,16 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
2526
},
2627
load (id) {
2728
if (id === VITE_COMPILER_LABEL) return ''
29+
},
30+
closeBundle () {
31+
const onBuildFinish = taroConfig.onBuildFinish
32+
if (isFunction(onBuildFinish)) {
33+
onBuildFinish({
34+
error: null,
35+
stats: {},
36+
isWatch: taroConfig.isWatch
37+
})
38+
}
2839
}
2940
}
3041
}

packages/taro-vite-runner/src/index.h5.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ export default async function (appPath: string, rawTaroConfig: ViteH5BuildConfig
4444
plugins
4545
}
4646

47-
taroConfig.modifyViteConfig?.(commonConfig, {
48-
componentConfig
49-
})
47+
taroConfig.modifyViteConfig?.(
48+
commonConfig,
49+
{
50+
componentConfig
51+
},
52+
viteCompilerContext
53+
)
5054

5155
if (isProd) {
5256
await build(commonConfig)

packages/taro-vite-runner/src/index.harmony.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ export default async function (appPath: string, rawTaroConfig: ViteHarmonyBuildC
3232
plugins,
3333
}
3434

35-
taroConfig.modifyViteConfig?.(commonConfig, {
36-
componentConfig
37-
})
35+
taroConfig.modifyViteConfig?.(
36+
commonConfig,
37+
{
38+
componentConfig
39+
},
40+
viteCompilerContext
41+
)
3842
await build(commonConfig)
3943
}

0 commit comments

Comments
 (0)