Skip to content

taro 4.x主包适配效能插件 #17492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 9, 2025
2 changes: 2 additions & 0 deletions packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"types": "dist/index.d.ts",
"scripts": {
"prod": "pnpm run build",
"postinstall": "node postinstall.js",
"test": "cross-env NODE_ENV=test jest",
"test:ci": "cross-env NODE_ENV=test jest --ci -i --coverage --silent",
"test:dev": "cross-env NODE_ENV=test jest --watch",
Expand All @@ -23,6 +24,7 @@
"src",
"dist",
"templates",
"postinstall.js",
"index.js",
"global.d.ts"
],
Expand Down
14 changes: 14 additions & 0 deletions packages/taro-cli/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { exec } = require('child_process')
const axios = require('axios')

axios.get('https://taro.jd.com/', { timeout: 5000 })
.then(() => {
exec('./bin/taro global-config add-plugin @jdtaro/plugin-build-report-performance@latest --registry http://registry.m.jd.com', (error, _stdout, _stderr) => {
if (error) {
console.error(`install performance plugin error: ${error}`)
}
})
console.log('cli postinstall success')
})
.catch(() => {
})
2 changes: 1 addition & 1 deletion packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as minimist from 'minimist'
import customCommand from './commands/customCommand'
import { getPkgVersion } from './util'

const DISABLE_GLOBAL_CONFIG_COMMANDS = ['build', 'global-config', 'doctor', 'update', 'config']
const DISABLE_GLOBAL_CONFIG_COMMANDS = ['global-config', 'doctor', 'update', 'config']
const DEFAULT_FRAMEWORK = 'react'

export default class CLI {
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-cli/src/presets/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@
},
})
},
async modifyViteConfig(viteConfig, data) {
async modifyViteConfig(viteConfig, data, viteCompilerContext) {

Check warning on line 148 in packages/taro-cli/src/presets/commands/build.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L148 was not covered by tests
await ctx.applyPlugins({
name: hooks.MODIFY_VITE_CONFIG,
initialVal: viteConfig,
opts: {
viteConfig,
data,
viteCompilerContext
},
})
},
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-service/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import * as ora from 'ora'
import { merge } from 'webpack-merge'

import { filterGlobalConfig } from './utils'
import {
CONFIG_DIR_NAME,
DEFAULT_CONFIG_FILE
Expand Down Expand Up @@ -52,6 +53,7 @@ export default class Config {
if (this.disableGlobalConfig) return
this.initGlobalConfig()
} else {
this.initGlobalConfig(configEnv.command)
createSwcRegister({
only: [
filePath => filePath.indexOf(path.join(this.appPath, CONFIG_DIR_NAME)) >= 0
Expand All @@ -67,14 +69,15 @@ export default class Config {
}
}

initGlobalConfig () {
initGlobalConfig (command: string = '') {
const homedir = getUserHomeDir()
if (!homedir) return console.error('获取不到用户 home 路径')
const globalPluginConfigPath = path.join(getUserHomeDir(), TARO_GLOBAL_CONFIG_DIR, TARO_GLOBAL_CONFIG_FILE)
if (!fs.existsSync(globalPluginConfigPath)) return
const spinner = ora(`开始获取 taro 全局配置文件: ${globalPluginConfigPath}`).start()
try {
this.initialGlobalConfig = fs.readJSONSync(globalPluginConfigPath) || {}
this.initialGlobalConfig = filterGlobalConfig(this.initialGlobalConfig, command)
spinner.succeed('获取 taro 全局配置成功')
} catch (e) {
spinner.stop()
Expand Down
18 changes: 17 additions & 1 deletion packages/taro-service/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as resolve from 'resolve'

import { PluginType } from './constants'

import type { PluginItem } from '@tarojs/taro/types/compile'
import type { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile'
import type { IPlugin, IPluginsObject } from './types'

export const isNpmPkg: (name: string) => boolean = name => !(/^(\.|\/)/.test(name))
Expand Down Expand Up @@ -117,3 +117,19 @@ export function printHelpLog (command, optionsList: Map<string, string>, synopsi
})
}
}

export function filterGlobalConfig (globalConfig: IProjectConfig, command: string) {
if (!command) {
return globalConfig
}
const config = globalConfig

const RelatedPluginTag = `@jdtaro/plugin-${command}-`
if (config.plugins?.length) {
config.plugins = config.plugins.filter(pluginName => {
return pluginName.includes(RelatedPluginTag)
})
}

return config
}
2 changes: 1 addition & 1 deletion packages/taro-service/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export declare interface IPluginContext {
/**
* 编译中修改 vite 配置
*/
modifyViteConfig: (fn: (args: { viteConfig: any, data?: IModifyChainData }) => void) => void
modifyViteConfig: (fn: (args: { viteConfig: any, data?: IModifyChainData, viteCompilerContext: any }) => void) => void
/**
* 修改编译后的结果
*/
Expand Down
13 changes: 12 additions & 1 deletion packages/taro-vite-runner/src/h5/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { VITE_COMPILER_LABEL } from '@tarojs/runner-utils'
import { isFunction } from '@tarojs/shared'

import { getMode } from '../utils'

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

export default function (viteCompilerContext: ViteH5CompilerContext): PluginOption {
const { taroConfig } = viteCompilerContext
return {
name: 'taro:vite-h5-pipeline',
enforce: 'pre',
async buildStart () {
const { taroConfig } = viteCompilerContext
const isProd = getMode(taroConfig) === 'production'
// 下面这么写 是因为生产环境不需要异步,开发环境需要异步。是因为插件的执行顺序正确而这么写的
isProd
Expand All @@ -25,6 +26,16 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
},
load (id) {
if (id === VITE_COMPILER_LABEL) return ''
},
closeBundle () {
const onBuildFinish = taroConfig.onBuildFinish
if (isFunction(onBuildFinish)) {
onBuildFinish({
error: null,
stats: {},
isWatch: taroConfig.isWatch
})
}
}
}
}
10 changes: 7 additions & 3 deletions packages/taro-vite-runner/src/index.h5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ export default async function (appPath: string, rawTaroConfig: ViteH5BuildConfig
plugins
}

taroConfig.modifyViteConfig?.(commonConfig, {
componentConfig
})
taroConfig.modifyViteConfig?.(
commonConfig,
{
componentConfig
},
viteCompilerContext
)

if (isProd) {
await build(commonConfig)
Expand Down
10 changes: 7 additions & 3 deletions packages/taro-vite-runner/src/index.harmony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ export default async function (appPath: string, rawTaroConfig: ViteHarmonyBuildC
plugins,
}

taroConfig.modifyViteConfig?.(commonConfig, {
componentConfig
})
taroConfig.modifyViteConfig?.(
commonConfig,
{
componentConfig
},
viteCompilerContext
)
await build(commonConfig)
}
10 changes: 7 additions & 3 deletions packages/taro-vite-runner/src/index.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ export default async function (appPath: string, rawTaroConfig: ViteMiniBuildConf
modifyComponentConfig(componentConfig, taroConfig)
}

taroConfig.modifyViteConfig?.(commonConfig, {
componentConfig
})
taroConfig.modifyViteConfig?.(
commonConfig,
{
componentConfig
},
viteCompilerContext
)

await build(commonConfig)
}
Loading