@tarojs/service 3.7.0-alpha.0 → 3.7.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Config.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  import {
2
2
  createSwcRegister,
3
3
  ENTRY,
4
+ fs,
4
5
  getModuleDefaultExport,
6
+ getUserHomeDir,
5
7
  OUTPUT_DIR,
6
8
  resolveScriptPath,
7
- SOURCE_DIR
9
+ SOURCE_DIR,
10
+ TARO_GLOBAL_CONFIG_DIR,
11
+ TARO_GLOBAL_CONFIG_FILE
8
12
  } from '@tarojs/helper'
9
- import * as fs from 'fs-extra'
13
+ import * as ora from 'ora'
10
14
  import * as path from 'path'
11
15
  import * as merge from 'webpack-merge'
12
16
 
@@ -19,23 +23,33 @@ import type { IProjectConfig } from '@tarojs/taro/types/compile'
19
23
 
20
24
  interface IConfigOptions {
21
25
  appPath: string
26
+ disableGlobalConfig?: boolean
22
27
  }
23
28
 
24
29
  export default class Config {
25
30
  appPath: string
26
31
  configPath: string
27
32
  initialConfig: IProjectConfig
33
+ initialGlobalConfig: IProjectConfig
28
34
  isInitSuccess: boolean
35
+ disableGlobalConfig: boolean
36
+
29
37
  constructor (opts: IConfigOptions) {
30
38
  this.appPath = opts.appPath
31
- this.init()
39
+ this.disableGlobalConfig = !!opts?.disableGlobalConfig
32
40
  }
33
41
 
34
- init () {
42
+ async init (configEnv: {
43
+ mode: string
44
+ command: string
45
+ }) {
46
+ this.initialConfig = {}
47
+ this.initialGlobalConfig = {}
48
+ this.isInitSuccess = false
35
49
  this.configPath = resolveScriptPath(path.join(this.appPath, CONFIG_DIR_NAME, DEFAULT_CONFIG_FILE))
36
50
  if (!fs.existsSync(this.configPath)) {
37
- this.initialConfig = {}
38
- this.isInitSuccess = false
51
+ if (this.disableGlobalConfig) return
52
+ this.initGlobalConfig()
39
53
  } else {
40
54
  createSwcRegister({
41
55
  only: [
@@ -43,16 +57,30 @@ export default class Config {
43
57
  ]
44
58
  })
45
59
  try {
46
- this.initialConfig = getModuleDefaultExport(require(this.configPath))(merge)
60
+ const userExport = getModuleDefaultExport(require(this.configPath))
61
+ this.initialConfig = typeof userExport === 'function' ? await userExport(merge, configEnv) : userExport
47
62
  this.isInitSuccess = true
48
63
  } catch (err) {
49
- this.initialConfig = {}
50
- this.isInitSuccess = false
51
64
  console.log(err)
52
65
  }
53
66
  }
54
67
  }
55
68
 
69
+ initGlobalConfig () {
70
+ const homedir = getUserHomeDir()
71
+ if (!homedir) return console.error('获取不到用户 home 路径')
72
+ const globalPluginConfigPath = path.join(getUserHomeDir(), TARO_GLOBAL_CONFIG_DIR, TARO_GLOBAL_CONFIG_FILE)
73
+ const spinner = ora(`开始获取 taro 全局配置文件: ${globalPluginConfigPath}`).start()
74
+ if (!fs.existsSync(globalPluginConfigPath)) return spinner.warn(`获取 taro 全局配置文件失败,不存在全局配置文件:${globalPluginConfigPath}`)
75
+ try {
76
+ this.initialGlobalConfig = fs.readJSONSync(globalPluginConfigPath) || {}
77
+ spinner.succeed('获取 taro 全局配置成功')
78
+ } catch (e){
79
+ spinner.stop()
80
+ console.warn(`获取全局配置失败,如果需要启用全局插件请查看配置文件: ${globalPluginConfigPath} `)
81
+ }
82
+ }
83
+
56
84
  getConfigWithNamed (platform, configName) {
57
85
  const initialConfig = this.initialConfig
58
86
  const sourceDirName = initialConfig.sourceRoot || SOURCE_DIR
package/src/Kernel.ts CHANGED
@@ -5,7 +5,6 @@ import { merge } from 'lodash'
5
5
  import * as path from 'path'
6
6
  import { AsyncSeriesWaterfallHook } from 'tapable'
7
7
 
8
- import Config from './Config'
9
8
  import Plugin from './Plugin'
10
9
  import { convertPluginsToObject, mergePlugins, printHelpLog, resolvePresetsOrPlugins } from './utils'
11
10
  import {
@@ -16,6 +15,7 @@ import {
16
15
  } from './utils/constants'
17
16
 
18
17
  import type { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile'
18
+ import type Config from './Config'
19
19
  import type {
20
20
  Func,
21
21
  ICommand,
@@ -29,6 +29,7 @@ import type {
29
29
 
30
30
  interface IKernelOptions {
31
31
  appPath: string
32
+ config: Config
32
33
  presets?: PluginItem[]
33
34
  plugins?: PluginItem[]
34
35
  }
@@ -42,10 +43,14 @@ export default class Kernel extends EventEmitter {
42
43
  plugins: Map<string, IPlugin>
43
44
  paths: IPaths
44
45
  extraPlugins: IPluginsObject
46
+ globalExtraPlugins: IPluginsObject
45
47
  config: Config
46
48
  initialConfig: IProjectConfig
49
+ initialGlobalConfig: IProjectConfig
47
50
  hooks: Map<string, IHook[]>
48
51
  methods: Map<string, Func[]>
52
+ cliCommands: string []
53
+ cliCommandsPath: string
49
54
  commands: Map<string, ICommand>
50
55
  platforms: Map<string, IPlatform>
51
56
  helper: any
@@ -58,6 +63,7 @@ export default class Kernel extends EventEmitter {
58
63
  this.appPath = options.appPath || process.cwd()
59
64
  this.optsPresets = options.presets
60
65
  this.optsPlugins = options.plugins
66
+ this.config = options.config
61
67
  this.hooks = new Map()
62
68
  this.methods = new Map()
63
69
  this.commands = new Map()
@@ -68,10 +74,8 @@ export default class Kernel extends EventEmitter {
68
74
  }
69
75
 
70
76
  initConfig () {
71
- this.config = new Config({
72
- appPath: this.appPath
73
- })
74
77
  this.initialConfig = this.config.initialConfig
78
+ this.initialGlobalConfig = this.config.initialGlobalConfig
75
79
  this.debugger('initConfig', this.initialConfig)
76
80
  }
77
81
 
@@ -97,50 +101,76 @@ export default class Kernel extends EventEmitter {
97
101
 
98
102
  initPresetsAndPlugins () {
99
103
  const initialConfig = this.initialConfig
100
- const allConfigPresets = mergePlugins(this.optsPresets || [], initialConfig.presets || [])()
101
- const allConfigPlugins = mergePlugins(this.optsPlugins || [], initialConfig.plugins || [])()
102
- this.debugger('initPresetsAndPlugins', allConfigPresets, allConfigPlugins)
104
+ const initialGlobalConfig = this.initialGlobalConfig
105
+ const cliAndProjectConfigPresets = mergePlugins(this.optsPresets || [], initialConfig.presets || [])()
106
+ const cliAndProjectPlugins = mergePlugins(this.optsPlugins || [], initialConfig.plugins || [])()
107
+ const globalPlugins = convertPluginsToObject(initialGlobalConfig.plugins || [])()
108
+ const globalPresets = convertPluginsToObject(initialGlobalConfig.presets || [])()
109
+ this.debugger('initPresetsAndPlugins', cliAndProjectConfigPresets, cliAndProjectPlugins)
110
+ this.debugger('globalPresetsAndPlugins', globalPlugins, globalPresets)
103
111
  process.env.NODE_ENV !== 'test' &&
104
112
  helper.createSwcRegister({
105
- only: [...Object.keys(allConfigPresets), ...Object.keys(allConfigPlugins)]
113
+ only: [
114
+ ...Object.keys(cliAndProjectConfigPresets),
115
+ ...Object.keys(cliAndProjectPlugins),
116
+ ...Object.keys(globalPresets),
117
+ ...Object.keys(globalPlugins)
118
+ ]
106
119
  })
107
120
  this.plugins = new Map()
108
121
  this.extraPlugins = {}
109
- this.resolvePresets(allConfigPresets)
110
- this.resolvePlugins(allConfigPlugins)
122
+ this.globalExtraPlugins = {}
123
+ this.resolvePresets(cliAndProjectConfigPresets, globalPresets)
124
+ this.resolvePlugins(cliAndProjectPlugins, globalPlugins)
111
125
  }
112
126
 
113
- resolvePresets (presets) {
114
- const allPresets = resolvePresetsOrPlugins(this.appPath, presets, PluginType.Preset)
115
- while (allPresets.length) {
116
- this.initPreset(allPresets.shift()!)
127
+ resolvePresets (cliAndProjectPresets: IPluginsObject, globalPresets: IPluginsObject) {
128
+ const resolvedCliAndProjectPresets = resolvePresetsOrPlugins(this.appPath, cliAndProjectPresets, PluginType.Preset)
129
+ while (resolvedCliAndProjectPresets.length) {
130
+ this.initPreset(resolvedCliAndProjectPresets.shift()!)
131
+ }
132
+
133
+ const globalConfigRootPath = path.join(helper.getUserHomeDir(), helper.TARO_GLOBAL_CONFIG_DIR)
134
+ const resolvedGlobalPresets = resolvePresetsOrPlugins(globalConfigRootPath, globalPresets, PluginType.Plugin, true)
135
+ while (resolvedGlobalPresets.length) {
136
+ this.initPreset(resolvedGlobalPresets.shift()!, true)
117
137
  }
118
138
  }
119
139
 
120
- resolvePlugins (plugins) {
121
- plugins = merge(this.extraPlugins, plugins)
122
- const allPlugins = resolvePresetsOrPlugins(this.appPath, plugins, PluginType.Plugin)
140
+ resolvePlugins (cliAndProjectPlugins: IPluginsObject, globalPlugins: IPluginsObject) {
141
+ cliAndProjectPlugins = merge(this.extraPlugins, cliAndProjectPlugins)
142
+ const resolvedCliAndProjectPlugins = resolvePresetsOrPlugins(this.appPath, cliAndProjectPlugins, PluginType.Plugin)
143
+
144
+ globalPlugins = merge(this.globalExtraPlugins, globalPlugins)
145
+ const globalConfigRootPath = path.join(helper.getUserHomeDir(), helper.TARO_GLOBAL_CONFIG_DIR)
146
+ const resolvedGlobalPlugins = resolvePresetsOrPlugins(globalConfigRootPath, globalPlugins, PluginType.Plugin, true)
147
+
148
+ const resolvedPlugins = resolvedCliAndProjectPlugins.concat(resolvedGlobalPlugins)
123
149
 
124
- while (allPlugins.length) {
125
- this.initPlugin(allPlugins.shift()!)
150
+ while (resolvedPlugins.length) {
151
+ this.initPlugin(resolvedPlugins.shift()!)
126
152
  }
153
+
127
154
  this.extraPlugins = {}
155
+ this.globalExtraPlugins = {}
128
156
  }
129
157
 
130
- initPreset (preset: IPreset) {
158
+ initPreset (preset: IPreset, isGlobalConfigPreset?: boolean) {
131
159
  this.debugger('initPreset', preset)
132
160
  const { id, path, opts, apply } = preset
133
161
  const pluginCtx = this.initPluginCtx({ id, path, ctx: this })
134
162
  const { presets, plugins } = apply()(pluginCtx, opts) || {}
135
163
  this.registerPlugin(preset)
136
164
  if (Array.isArray(presets)) {
137
- const _presets = resolvePresetsOrPlugins(this.appPath, convertPluginsToObject(presets)(), PluginType.Preset)
165
+ const _presets = resolvePresetsOrPlugins(this.appPath, convertPluginsToObject(presets)(), PluginType.Preset, isGlobalConfigPreset)
138
166
  while (_presets.length) {
139
- this.initPreset(_presets.shift()!)
167
+ this.initPreset(_presets.shift()!, isGlobalConfigPreset)
140
168
  }
141
169
  }
142
170
  if (Array.isArray(plugins)) {
143
- this.extraPlugins = merge(this.extraPlugins, convertPluginsToObject(plugins)())
171
+ isGlobalConfigPreset
172
+ ? (this.globalExtraPlugins = merge(this.globalExtraPlugins, convertPluginsToObject(plugins)()))
173
+ : (this.extraPlugins = merge(this.extraPlugins, convertPluginsToObject(plugins)()))
144
174
  }
145
175
  }
146
176
 
@@ -153,6 +183,21 @@ export default class Kernel extends EventEmitter {
153
183
  this.checkPluginOpts(pluginCtx, opts)
154
184
  }
155
185
 
186
+ applyCliCommandPlugin (commandNames: string[] = []) {
187
+ const existsCliCommand: string[] = []
188
+ for ( let i = 0; i < commandNames.length; i++ ) {
189
+ const commandName = commandNames[i]
190
+ const commandFilePath = path.resolve(this.cliCommandsPath, `${commandName}.js`)
191
+ if (this.cliCommands.includes(commandName)) existsCliCommand.push(commandFilePath)
192
+ }
193
+ const commandPlugins = convertPluginsToObject(existsCliCommand || [])()
194
+ helper.createSwcRegister({ only: [ ...Object.keys(commandPlugins) ] })
195
+ const resolvedCommandPlugins = resolvePresetsOrPlugins(this.appPath, commandPlugins, PluginType.Plugin)
196
+ while (resolvedCommandPlugins.length) {
197
+ this.initPlugin(resolvedCommandPlugins.shift()!)
198
+ }
199
+ }
200
+
156
201
  checkPluginOpts (pluginCtx, opts) {
157
202
  if (typeof pluginCtx.optsSchema !== 'function') {
158
203
  return
@@ -189,7 +234,8 @@ export default class Kernel extends EventEmitter {
189
234
  'helper',
190
235
  'runOpts',
191
236
  'initialConfig',
192
- 'applyPlugins'
237
+ 'applyPlugins',
238
+ 'applyCliCommandPlugin'
193
239
  ]
194
240
  internalMethods.forEach(name => {
195
241
  if (!this.methods.has(name)) {
package/src/index.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import Config from './Config'
1
2
  import Kernel from './Kernel'
2
3
  import { TaroPlatformBase, TaroPlatformWeb } from './platform-plugin-base'
3
4
 
4
5
  export * from './utils/types'
5
- export { Kernel, TaroPlatformBase, TaroPlatformWeb }
6
- export default { Kernel, TaroPlatformBase, TaroPlatformWeb }
6
+ export { Config, Kernel, TaroPlatformBase, TaroPlatformWeb }
7
+ export default { Config, Kernel, TaroPlatformBase, TaroPlatformWeb }
7
8
 
8
9
  export type { IPluginContext } from './utils/types'
@@ -1,5 +1,7 @@
1
- import { PLATFORM_TYPE } from '@tarojs/shared'
1
+ import { recursiveMerge } from '@tarojs/helper'
2
+ import { isObject, PLATFORM_TYPE } from '@tarojs/shared'
2
3
 
4
+ import { getPkgVersion } from '../utils/package'
3
5
  import TaroPlatform from './platform'
4
6
 
5
7
  import type { RecursiveTemplate, UnRecursiveTemplate } from '@tarojs/shared/dist/template'
@@ -33,9 +35,15 @@ export abstract class TaroPlatformBase<T extends TConfig = TConfig> extends Taro
33
35
  }
34
36
 
35
37
  private setupImpl () {
36
- const { needClearOutput } = this.config
37
- if (typeof needClearOutput === 'undefined' || !!needClearOutput) {
38
+ const { output } = this.config
39
+ // webpack5 原生支持 output.clean 选项,但是 webpack4 不支持, 为统一行为,这里做一下兼容
40
+ // (在 packages/taro-mini-runner/src/webpack/chain.ts 和 packages/taro-webpack-runner/src/utils/chain.ts 的 makeConfig 中对 clean 选项做了过滤)
41
+ // 仅 output.clean 为 false 时不清空输出目录
42
+ // eslint-disable-next-line eqeqeq
43
+ if (output == undefined || output.clean == undefined || output.clean === true) {
38
44
  this.emptyOutputDir()
45
+ } else if (isObject(output.clean)) {
46
+ this.emptyOutputDir(output.clean.keep || [])
39
47
  }
40
48
  this.printDevelopmentTip(this.platform)
41
49
  if (this.projectConfigJson) {
@@ -103,7 +111,16 @@ ${exampleCommand}`))
103
111
  * @param extraOptions 需要额外合入 Options 的配置项
104
112
  */
105
113
  protected getOptions (extraOptions = {}) {
106
- const { ctx, config, globalObject, fileType, template } = this
114
+ const { ctx, globalObject, fileType, template } = this
115
+
116
+ const config = recursiveMerge(Object.assign({}, this.config), {
117
+ env: {
118
+ FRAMEWORK: JSON.stringify(this.config.framework),
119
+ TARO_ENV: JSON.stringify(this.platform),
120
+ TARO_PLATFORM: JSON.stringify(this.platformType),
121
+ TARO_VERSION: JSON.stringify(getPkgVersion())
122
+ }
123
+ })
107
124
 
108
125
  return {
109
126
  ...config,
@@ -128,10 +145,15 @@ ${exampleCommand}`))
128
145
 
129
146
  private async buildImpl (extraOptions = {}) {
130
147
  const runner = await this.getRunner()
131
- const options = this.getOptions(Object.assign({
132
- runtimePath: this.runtimePath,
133
- taroComponentsPath: this.taroComponentsPath
134
- }, extraOptions))
148
+ const options = this.getOptions(
149
+ Object.assign(
150
+ {
151
+ runtimePath: this.runtimePath,
152
+ taroComponentsPath: this.taroComponentsPath
153
+ },
154
+ extraOptions
155
+ )
156
+ )
135
157
  await runner(options)
136
158
  }
137
159
 
@@ -152,7 +174,7 @@ ${exampleCommand}`))
152
174
  * 递归替换对象的 key 值
153
175
  */
154
176
  protected recursiveReplaceObjectKeys (obj, keyMap) {
155
- Object.keys(obj).forEach(key => {
177
+ Object.keys(obj).forEach((key) => {
156
178
  if (keyMap[key]) {
157
179
  obj[keyMap[key]] = obj[key]
158
180
  if (typeof obj[key] === 'object') {
@@ -53,9 +53,9 @@ export default abstract class TaroPlatform<T extends TConfig = TConfig> {
53
53
  this.compiler = typeof _compiler === 'object' ? _compiler.type : _compiler
54
54
  }
55
55
 
56
- protected emptyOutputDir () {
56
+ protected emptyOutputDir (excludes: Array<string | RegExp> = []) {
57
57
  const { outputPath } = this.ctx.paths
58
- this.helper.emptyDirectory(outputPath)
58
+ this.helper.emptyDirectory(outputPath, { excludes })
59
59
  }
60
60
 
61
61
  /**
@@ -63,7 +63,7 @@ export default abstract class TaroPlatform<T extends TConfig = TConfig> {
63
63
  */
64
64
  private updateOutputPath (config: TConfig) {
65
65
  const platformPath = config.output?.path
66
- if(platformPath) {
66
+ if (platformPath) {
67
67
  this.ctx.paths.outputPath = platformPath
68
68
  }
69
69
  }
@@ -1,4 +1,4 @@
1
- import { PLATFORM_TYPE } from '@tarojs/shared'
1
+ import { isObject, PLATFORM_TYPE } from '@tarojs/shared'
2
2
  import { get, merge } from 'lodash'
3
3
  import * as path from 'path'
4
4
 
@@ -20,9 +20,14 @@ export abstract class TaroPlatformWeb<T extends TConfig = TConfig> extends TaroP
20
20
  }
21
21
 
22
22
  private setupWebApp () {
23
- const { needClearOutput } = this.config
24
- if (typeof needClearOutput === 'undefined' || !!needClearOutput) {
23
+ const { output } = this.config
24
+ // webpack5 原生支持 output.clean 选项,但是 webpack4 不支持, 为统一行为,这里做一下兼容
25
+ // (在 packages/taro-mini-runner/src/webpack/chain.ts 和 packages/taro-webpack-runner/src/utils/chain.ts 的 makeConfig 中对 clean 选项做了过滤)
26
+ // eslint-disable-next-line eqeqeq
27
+ if (output == undefined || output.clean == undefined || output.clean === true) {
25
28
  this.emptyOutputDir()
29
+ } else if (isObject(output.clean)) {
30
+ this.emptyOutputDir(output.clean.keep || [])
26
31
  }
27
32
  this.printDevelopmentTip()
28
33
  }
@@ -55,6 +60,9 @@ export abstract class TaroPlatformWeb<T extends TConfig = TConfig> extends TaroP
55
60
  case 'webpack5':
56
61
  runnerPkg = '@tarojs/webpack5-runner'
57
62
  break
63
+ case 'vite':
64
+ runnerPkg = '@tarojs/vite-runner'
65
+ break
58
66
  default:
59
67
  runnerPkg = '@tarojs/webpack-runner'
60
68
  }
@@ -4,9 +4,9 @@ import * as path from 'path'
4
4
  import * as resolve from 'resolve'
5
5
 
6
6
  import { PluginType } from './constants'
7
- import { IPlugin, IPluginsObject } from './types'
8
7
 
9
8
  import type { PluginItem } from '@tarojs/taro/types/compile'
9
+ import type { IPlugin, IPluginsObject } from './types'
10
10
 
11
11
  export const isNpmPkg: (name: string) => boolean = name => !(/^(\.|\/)/.test(name))
12
12
 
@@ -42,8 +42,12 @@ export function mergePlugins (dist: PluginItem[], src: PluginItem[]) {
42
42
  }
43
43
 
44
44
  // getModuleDefaultExport
45
- export function resolvePresetsOrPlugins (root: string, args, type: PluginType): IPlugin[] {
46
- return Object.keys(args).map(item => {
45
+ export function resolvePresetsOrPlugins (root: string, args: IPluginsObject, type: PluginType, skipError?: boolean): IPlugin[] {
46
+ // 全局的插件引入报错,不抛出 Error 影响主流程,而是通过 log 提醒然后把插件 filter 掉,保证主流程不变
47
+ const resolvedPresetsOrPlugins: IPlugin[] = []
48
+ const presetsOrPluginsNames = Object.keys(args) || []
49
+ for ( let i = 0; i < presetsOrPluginsNames.length; i++ ) {
50
+ const item = presetsOrPluginsNames[i]
47
51
  let fPath
48
52
  try {
49
53
  fPath = resolve.sync(item, {
@@ -53,13 +57,17 @@ export function resolvePresetsOrPlugins (root: string, args, type: PluginType):
53
57
  } catch (err) {
54
58
  if (args[item]?.backup) {
55
59
  // 如果项目中没有,可以使用 CLI 中的插件
56
- fPath = args[item].backup
60
+ fPath = args[item]?.backup
61
+ } else if (skipError) {
62
+ // 如果跳过报错,那么 log 提醒,并且不使用该插件
63
+ console.log(chalk.yellow(`找不到插件依赖 "${item}",请先在项目中安装,项目路径:${root}`))
64
+ continue
57
65
  } else {
58
- console.log(chalk.red(`找不到依赖 "${item}",请先在项目中安装`))
66
+ console.log(chalk.red(`找不到插件依赖 "${item}",请先在项目中安装,项目路径:${root}`))
59
67
  process.exit(1)
60
68
  }
61
69
  }
62
- return {
70
+ const resolvedItem = {
63
71
  id: fPath,
64
72
  path: fPath,
65
73
  type,
@@ -69,11 +77,19 @@ export function resolvePresetsOrPlugins (root: string, args, type: PluginType):
69
77
  return getModuleDefaultExport(require(fPath))
70
78
  } catch (error) {
71
79
  console.error(error)
72
- throw new Error(`插件依赖 "${item}" 加载失败,请检查插件配置`)
80
+ // 全局的插件运行报错,不抛出 Error 影响主流程,而是通过 log 提醒然后把插件 filter 掉,保证主流程不变
81
+ if (skipError) {
82
+ console.error(`插件依赖 "${item}" 加载失败,请检查插件配置`)
83
+ } else {
84
+ throw new Error(`插件依赖 "${item}" 加载失败,请检查插件配置`)
85
+ }
73
86
  }
74
87
  }
75
88
  }
76
- })
89
+ resolvedPresetsOrPlugins.push(resolvedItem)
90
+ }
91
+
92
+ return resolvedPresetsOrPlugins
77
93
  }
78
94
 
79
95
  function supplementBlank (length) {
@@ -1,6 +1,6 @@
1
1
  import type helper from '@tarojs/helper'
2
- import type { IProjectConfig } from '@tarojs/taro/types/compile'
3
- import type { IModifyWebpackChain } from '@tarojs/taro/types/compile/hooks'
2
+ import type { IMiniFilesConfig, IProjectConfig } from '@tarojs/taro/types/compile'
3
+ import type { IModifyChainData } from '@tarojs/taro/types/compile/hooks'
4
4
  import type joi from 'joi'
5
5
  import type Webpack from 'webpack'
6
6
  import type Chain from 'webpack-chain'
@@ -138,11 +138,11 @@ export declare interface IPluginContext {
138
138
  /**
139
139
  * 编译中修改 webpack 配置,在这个钩子中,你可以对 webpackChain 作出想要的调整,等同于配置 [`webpackChain`](./config-detail.md#miniwebpackchain)
140
140
  */
141
- modifyWebpackChain: (fn: (args: { chain: Chain, webpack: typeof Webpack, data?: IModifyWebpackChain }) => void) => void
141
+ modifyWebpackChain: (fn: (args: { chain: Chain, webpack: typeof Webpack, data?: IModifyChainData }) => void) => void
142
142
  /**
143
143
  * 编译中修改 vite 配置
144
144
  */
145
- modifyViteConfig: (fn: (args: { viteConfig: any, componentConfig?: IModifyWebpackChain['componentConfig'] }) => void) => void
145
+ modifyViteConfig: (fn: (args: { viteConfig: any, data?: IModifyChainData }) => void) => void
146
146
  /**
147
147
  * 修改编译后的结果
148
148
  */
@@ -150,7 +150,11 @@ export declare interface IPluginContext {
150
150
  /**
151
151
  * 修改编译过程中的页面组件配置
152
152
  */
153
- modifyMiniConfigs: (fn: (args: { configMap: any }) => void) => void
153
+ modifyMiniConfigs: (fn: (args: { configMap: IMiniFilesConfig }) => void) => void
154
+ /**
155
+ * 修改 Taro 编译配置
156
+ */
157
+ modifyRunnerOpts: (fn: (args: { opts: any }) => void) => void
154
158
 
155
159
  [key: string]: any
156
160
  }
package/types/Config.d.ts CHANGED
@@ -1,14 +1,22 @@
1
+ import * as ora from 'ora';
1
2
  import type { IProjectConfig } from '@tarojs/taro/types/compile';
2
3
  interface IConfigOptions {
3
4
  appPath: string;
5
+ disableGlobalConfig?: boolean;
4
6
  }
5
7
  export default class Config {
6
8
  appPath: string;
7
9
  configPath: string;
8
10
  initialConfig: IProjectConfig;
11
+ initialGlobalConfig: IProjectConfig;
9
12
  isInitSuccess: boolean;
13
+ disableGlobalConfig: boolean;
10
14
  constructor(opts: IConfigOptions);
11
- init(): void;
15
+ init(configEnv: {
16
+ mode: string;
17
+ command: string;
18
+ }): Promise<void>;
19
+ initGlobalConfig(): void | ora.Ora;
12
20
  getConfigWithNamed(platform: any, configName: any): any;
13
21
  }
14
22
  export {};
package/types/Kernel.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import Config from './Config';
4
3
  import Plugin from './Plugin';
5
4
  import type { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile';
5
+ import type Config from './Config';
6
6
  import type { Func, ICommand, IHook, IPaths, IPlatform, IPlugin, IPluginsObject, IPreset } from './utils/types';
7
7
  interface IKernelOptions {
8
8
  appPath: string;
9
+ config: Config;
9
10
  presets?: PluginItem[];
10
11
  plugins?: PluginItem[];
11
12
  }
@@ -18,10 +19,14 @@ export default class Kernel extends EventEmitter {
18
19
  plugins: Map<string, IPlugin>;
19
20
  paths: IPaths;
20
21
  extraPlugins: IPluginsObject;
22
+ globalExtraPlugins: IPluginsObject;
21
23
  config: Config;
22
24
  initialConfig: IProjectConfig;
25
+ initialGlobalConfig: IProjectConfig;
23
26
  hooks: Map<string, IHook[]>;
24
27
  methods: Map<string, Func[]>;
28
+ cliCommands: string[];
29
+ cliCommandsPath: string;
25
30
  commands: Map<string, ICommand>;
26
31
  platforms: Map<string, IPlatform>;
27
32
  helper: any;
@@ -32,10 +37,11 @@ export default class Kernel extends EventEmitter {
32
37
  initPaths(): void;
33
38
  initHelper(): void;
34
39
  initPresetsAndPlugins(): void;
35
- resolvePresets(presets: any): void;
36
- resolvePlugins(plugins: any): void;
37
- initPreset(preset: IPreset): void;
40
+ resolvePresets(cliAndProjectPresets: IPluginsObject, globalPresets: IPluginsObject): void;
41
+ resolvePlugins(cliAndProjectPlugins: IPluginsObject, globalPlugins: IPluginsObject): void;
42
+ initPreset(preset: IPreset, isGlobalConfigPreset?: boolean): void;
38
43
  initPlugin(plugin: IPlugin): void;
44
+ applyCliCommandPlugin(commandNames?: string[]): void;
39
45
  checkPluginOpts(pluginCtx: any, opts: any): void;
40
46
  registerPlugin(plugin: IPlugin): void;
41
47
  initPluginCtx({ id, path, ctx }: {
package/types/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
+ import Config from './Config';
1
2
  import Kernel from './Kernel';
2
3
  import { TaroPlatformBase, TaroPlatformWeb } from './platform-plugin-base';
3
4
  export * from './utils/types';
4
- export { Kernel, TaroPlatformBase, TaroPlatformWeb };
5
+ export { Config, Kernel, TaroPlatformBase, TaroPlatformWeb };
5
6
  declare const _default: {
7
+ Config: typeof Config;
6
8
  Kernel: typeof Kernel;
7
9
  TaroPlatformBase: typeof TaroPlatformBase;
8
10
  TaroPlatformWeb: typeof TaroPlatformWeb;
@@ -32,14 +32,7 @@ export declare abstract class TaroPlatformBase<T extends TConfig = TConfig> exte
32
32
  * 准备 mini-runner 参数
33
33
  * @param extraOptions 需要额外合入 Options 的配置项
34
34
  */
35
- protected getOptions(extraOptions?: {}): T & {
36
- nodeModulesPath: string;
37
- buildAdapter: any;
38
- platformType: PLATFORM_TYPE;
39
- globalObject: string;
40
- fileType: IFileType;
41
- template: RecursiveTemplate | UnRecursiveTemplate;
42
- };
35
+ protected getOptions(extraOptions?: {}): any;
43
36
  /**
44
37
  * 调用 mini-runner 开始编译
45
38
  * @param extraOptions 需要额外传入 @tarojs/mini-runner 的配置项
@@ -22,7 +22,7 @@ export default abstract class TaroPlatform<T extends TConfig = TConfig> {
22
22
  protected setupTransaction: Transaction<this>;
23
23
  protected buildTransaction: Transaction<this>;
24
24
  constructor(ctx: IPluginContext, config: T);
25
- protected emptyOutputDir(): void;
25
+ protected emptyOutputDir(excludes?: Array<string | RegExp>): void;
26
26
  /**
27
27
  * 如果分端编译详情 webpack 配置了 output 则需更新 outputPath 位置
28
28
  */
@@ -1,9 +1,9 @@
1
1
  import { PluginType } from './constants';
2
- import { IPlugin, IPluginsObject } from './types';
3
2
  import type { PluginItem } from '@tarojs/taro/types/compile';
3
+ import type { IPlugin, IPluginsObject } from './types';
4
4
  export declare const isNpmPkg: (name: string) => boolean;
5
5
  export declare function getPluginPath(pluginPath: string): string;
6
6
  export declare function convertPluginsToObject(items: PluginItem[]): () => IPluginsObject;
7
7
  export declare function mergePlugins(dist: PluginItem[], src: PluginItem[]): () => IPluginsObject;
8
- export declare function resolvePresetsOrPlugins(root: string, args: any, type: PluginType): IPlugin[];
8
+ export declare function resolvePresetsOrPlugins(root: string, args: IPluginsObject, type: PluginType, skipError?: boolean): IPlugin[];
9
9
  export declare function printHelpLog(command: any, optionsList: Map<string, string>, synopsisList?: Set<string>): void;