Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"types": "dist/index.d.ts",
"scripts": {
"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 @@ -22,6 +23,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 @@ -6,7 +6,7 @@ import * as path from 'path'
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
5 changes: 4 additions & 1 deletion packages/taro-service/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as ora from 'ora'
import * as path from 'path'
import * as merge from 'webpack-merge'

import { filterGlobalConfig } from './utils'
import {
CONFIG_DIR_NAME,
DEFAULT_CONFIG_FILE
Expand Down Expand Up @@ -51,6 +52,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 @@ -66,14 +68,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)
const spinner = ora(`开始获取 taro 全局配置文件: ${globalPluginConfigPath}`).start()
if (!fs.existsSync(globalPluginConfigPath)) return spinner.warn(`获取 taro 全局配置文件失败,不存在全局配置文件:${globalPluginConfigPath}`)
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 @@ -5,7 +5,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 @@ -116,3 +116,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
}
1 change: 1 addition & 0 deletions packages/taro-webpack-runner/src/config/dev.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
/** hooks & methods */
onCompilerMake: config.onCompilerMake,
onParseCreateElement: config.onParseCreateElement,
modifyAppConfig: config.modifyAppConfig
})

if (enableExtract) {
Expand Down
1 change: 1 addition & 0 deletions packages/taro-webpack-runner/src/config/prod.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
/** hooks & methods */
onCompilerMake: config.onCompilerMake,
onParseCreateElement: config.onParseCreateElement,
modifyAppConfig: config.modifyAppConfig
})

if (enableExtract) {
Expand Down
6 changes: 6 additions & 0 deletions packages/taro-webpack-runner/src/utils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
resolveMainFilePath,
SCRIPT_EXT
} from '@tarojs/helper'
import { Func } from '@tarojs/taro/types/compile'
import { defaults } from 'lodash'
import * as path from 'path'

Expand All @@ -14,6 +15,7 @@
sourceDir: string
entryFileName: string
frameworkExts: string[]
modifyAppConfig?: Func
}

type TEntry = string | string[] | Entry | EntryFunc
Expand Down Expand Up @@ -57,6 +59,10 @@
if (isEmptyObject(appConfig)) {
throw new Error('缺少 app 全局配置,请检查!')
}
const { modifyAppConfig } = this.options
if (typeof modifyAppConfig === 'function') {
modifyAppConfig(appConfig)

Check warning on line 64 in packages/taro-webpack-runner/src/utils/app.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-webpack-runner/src/utils/app.ts#L64

Added line #L64 was not covered by tests
}
this.#appConfig = appConfig
}
return this.#appConfig as AppConfig
Expand Down
6 changes: 6 additions & 0 deletions packages/taro-webpack5-runner/src/utils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
resolveMainFilePath,
SCRIPT_EXT
} from '@tarojs/helper'
import { Func } from '@tarojs/taro/types/compile'
import { defaults } from 'lodash'
import path from 'path'

Expand All @@ -16,6 +17,7 @@ interface IOptions {
frameworkExts: string[]
alias: Record<string, any>
defineConstants: Record<string, any>
modifyAppConfig?: Func
}

export default class AppHelper {
Expand Down Expand Up @@ -59,6 +61,10 @@ export default class AppHelper {
if (isEmptyObject(appConfig)) {
throw new Error('缺少 app 全局配置,请检查!')
}
const { modifyAppConfig } = this.options
if (typeof modifyAppConfig === 'function') {
modifyAppConfig(appConfig)
}
this.#appConfig = appConfig
}
return this.#appConfig as AppConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class H5WebpackPlugin {
prebundle: prebundleOptions.enable,
isBuildNativeComp: this.combination.isBuildNativeComp,
/** hooks & methods */
modifyAppConfig: config.modifyAppConfig,
onCompilerMake: config.onCompilerMake,
onParseCreateElement: config.onParseCreateElement,
}
Expand Down
Loading