@tarojs/service 3.6.22-nightly.0 → 3.6.22
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/package.json +5 -6
- package/src/Config.ts +0 -125
- package/src/Kernel.ts +0 -385
- package/src/Plugin.ts +0 -80
- package/src/index.ts +0 -9
- package/src/platform-plugin-base/index.ts +0 -2
- package/src/platform-plugin-base/mini.ts +0 -212
- package/src/platform-plugin-base/platform.ts +0 -75
- package/src/platform-plugin-base/web.ts +0 -133
- package/src/platform-plugin-base/webpack/hmr-plugin.ts +0 -37
- package/src/utils/constants.ts +0 -21
- package/src/utils/index.ts +0 -118
- package/src/utils/package.ts +0 -15
- package/src/utils/types.ts +0 -154
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/service",
|
|
3
|
-
"version": "3.6.22
|
|
3
|
+
"version": "3.6.22",
|
|
4
4
|
"description": "Taro Service",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"index.js",
|
|
13
13
|
"dist",
|
|
14
|
-
"types"
|
|
15
|
-
"src"
|
|
14
|
+
"types"
|
|
16
15
|
],
|
|
17
16
|
"keywords": [
|
|
18
17
|
"taro"
|
|
@@ -30,9 +29,9 @@
|
|
|
30
29
|
"tapable": "^1.1.3",
|
|
31
30
|
"webpack-merge": "^4.2.2",
|
|
32
31
|
"ora": "^5.0.0",
|
|
33
|
-
"@tarojs/helper": "3.6.22
|
|
34
|
-
"@tarojs/taro": "3.6.22
|
|
35
|
-
"@tarojs/shared": "3.6.22
|
|
32
|
+
"@tarojs/helper": "3.6.22",
|
|
33
|
+
"@tarojs/taro": "3.6.22",
|
|
34
|
+
"@tarojs/shared": "3.6.22"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
37
|
"webpack": "5.78.0"
|
package/src/Config.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createSwcRegister,
|
|
3
|
-
ENTRY,
|
|
4
|
-
fs,
|
|
5
|
-
getModuleDefaultExport,
|
|
6
|
-
getUserHomeDir,
|
|
7
|
-
OUTPUT_DIR,
|
|
8
|
-
resolveScriptPath,
|
|
9
|
-
SOURCE_DIR,
|
|
10
|
-
TARO_GLOBAL_CONFIG_DIR,
|
|
11
|
-
TARO_GLOBAL_CONFIG_FILE
|
|
12
|
-
} from '@tarojs/helper'
|
|
13
|
-
import * as ora from 'ora'
|
|
14
|
-
import * as path from 'path'
|
|
15
|
-
import * as merge from 'webpack-merge'
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
CONFIG_DIR_NAME,
|
|
19
|
-
DEFAULT_CONFIG_FILE
|
|
20
|
-
} from './utils/constants'
|
|
21
|
-
|
|
22
|
-
import type { IProjectConfig } from '@tarojs/taro/types/compile'
|
|
23
|
-
|
|
24
|
-
interface IConfigOptions {
|
|
25
|
-
appPath: string
|
|
26
|
-
disableGlobalConfig?: boolean
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default class Config {
|
|
30
|
-
appPath: string
|
|
31
|
-
configPath: string
|
|
32
|
-
initialConfig: IProjectConfig
|
|
33
|
-
initialGlobalConfig: IProjectConfig
|
|
34
|
-
isInitSuccess: boolean
|
|
35
|
-
disableGlobalConfig: boolean
|
|
36
|
-
|
|
37
|
-
constructor (opts: IConfigOptions) {
|
|
38
|
-
this.appPath = opts.appPath
|
|
39
|
-
this.disableGlobalConfig = !!opts?.disableGlobalConfig
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async init (configEnv: {
|
|
43
|
-
mode: string
|
|
44
|
-
command: string
|
|
45
|
-
}) {
|
|
46
|
-
this.initialConfig = {}
|
|
47
|
-
this.initialGlobalConfig = {}
|
|
48
|
-
this.isInitSuccess = false
|
|
49
|
-
this.configPath = resolveScriptPath(path.join(this.appPath, CONFIG_DIR_NAME, DEFAULT_CONFIG_FILE))
|
|
50
|
-
if (!fs.existsSync(this.configPath)) {
|
|
51
|
-
if(this.disableGlobalConfig) return
|
|
52
|
-
this.initGlobalConfig()
|
|
53
|
-
} else {
|
|
54
|
-
createSwcRegister({
|
|
55
|
-
only: [
|
|
56
|
-
filePath => filePath.indexOf(path.join(this.appPath, CONFIG_DIR_NAME)) >= 0
|
|
57
|
-
]
|
|
58
|
-
})
|
|
59
|
-
try {
|
|
60
|
-
const userExport = getModuleDefaultExport(require(this.configPath))
|
|
61
|
-
this.initialConfig = typeof userExport === 'function' ? await userExport(merge, configEnv) : userExport
|
|
62
|
-
this.isInitSuccess = true
|
|
63
|
-
} catch (err) {
|
|
64
|
-
console.log(err)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
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
|
-
|
|
84
|
-
getConfigWithNamed (platform, configName) {
|
|
85
|
-
const initialConfig = this.initialConfig
|
|
86
|
-
const sourceDirName = initialConfig.sourceRoot || SOURCE_DIR
|
|
87
|
-
const outputDirName = initialConfig.outputRoot || OUTPUT_DIR
|
|
88
|
-
const sourceDir = path.join(this.appPath, sourceDirName)
|
|
89
|
-
const entryName = ENTRY
|
|
90
|
-
const entryFilePath = resolveScriptPath(path.join(sourceDir, entryName))
|
|
91
|
-
|
|
92
|
-
const entry = {
|
|
93
|
-
[entryName]: [entryFilePath]
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
entry,
|
|
98
|
-
alias: initialConfig.alias || {},
|
|
99
|
-
copy: initialConfig.copy,
|
|
100
|
-
sourceRoot: sourceDirName,
|
|
101
|
-
outputRoot: outputDirName,
|
|
102
|
-
platform,
|
|
103
|
-
framework: initialConfig.framework,
|
|
104
|
-
compiler: initialConfig.compiler,
|
|
105
|
-
cache: initialConfig.cache,
|
|
106
|
-
logger: initialConfig.logger,
|
|
107
|
-
baseLevel: initialConfig.baseLevel,
|
|
108
|
-
csso: initialConfig.csso,
|
|
109
|
-
sass: initialConfig.sass,
|
|
110
|
-
uglify: initialConfig.uglify,
|
|
111
|
-
plugins: initialConfig.plugins,
|
|
112
|
-
projectName: initialConfig.projectName,
|
|
113
|
-
env: initialConfig.env,
|
|
114
|
-
defineConstants: initialConfig.defineConstants,
|
|
115
|
-
designWidth: initialConfig.designWidth,
|
|
116
|
-
deviceRatio: initialConfig.deviceRatio,
|
|
117
|
-
projectConfigName: initialConfig.projectConfigName,
|
|
118
|
-
jsMinimizer: initialConfig.jsMinimizer,
|
|
119
|
-
cssMinimizer: initialConfig.cssMinimizer,
|
|
120
|
-
terser: initialConfig.terser,
|
|
121
|
-
esbuild: initialConfig.esbuild,
|
|
122
|
-
...initialConfig[configName]
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
package/src/Kernel.ts
DELETED
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
import * as helper from '@tarojs/helper'
|
|
2
|
-
import { getPlatformType } from '@tarojs/shared'
|
|
3
|
-
import { EventEmitter } from 'events'
|
|
4
|
-
import { merge } from 'lodash'
|
|
5
|
-
import * as path from 'path'
|
|
6
|
-
import { AsyncSeriesWaterfallHook } from 'tapable'
|
|
7
|
-
|
|
8
|
-
import Plugin from './Plugin'
|
|
9
|
-
import { convertPluginsToObject, mergePlugins, printHelpLog, resolvePresetsOrPlugins } from './utils'
|
|
10
|
-
import {
|
|
11
|
-
IS_ADD_HOOK,
|
|
12
|
-
IS_EVENT_HOOK,
|
|
13
|
-
IS_MODIFY_HOOK,
|
|
14
|
-
PluginType
|
|
15
|
-
} from './utils/constants'
|
|
16
|
-
|
|
17
|
-
import type { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile'
|
|
18
|
-
import type Config from './Config'
|
|
19
|
-
import type {
|
|
20
|
-
Func,
|
|
21
|
-
ICommand,
|
|
22
|
-
IHook,
|
|
23
|
-
IPaths,
|
|
24
|
-
IPlatform,
|
|
25
|
-
IPlugin,
|
|
26
|
-
IPluginsObject,
|
|
27
|
-
IPreset
|
|
28
|
-
} from './utils/types'
|
|
29
|
-
|
|
30
|
-
interface IKernelOptions {
|
|
31
|
-
appPath: string
|
|
32
|
-
config: Config
|
|
33
|
-
presets?: PluginItem[]
|
|
34
|
-
plugins?: PluginItem[]
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export default class Kernel extends EventEmitter {
|
|
38
|
-
appPath: string
|
|
39
|
-
isWatch: boolean
|
|
40
|
-
isProduction: boolean
|
|
41
|
-
optsPresets: PluginItem[] | void
|
|
42
|
-
optsPlugins: PluginItem[] | void
|
|
43
|
-
plugins: Map<string, IPlugin>
|
|
44
|
-
paths: IPaths
|
|
45
|
-
extraPlugins: IPluginsObject
|
|
46
|
-
globalExtraPlugins: IPluginsObject
|
|
47
|
-
config: Config
|
|
48
|
-
initialConfig: IProjectConfig
|
|
49
|
-
initialGlobalConfig: IProjectConfig
|
|
50
|
-
hooks: Map<string, IHook[]>
|
|
51
|
-
methods: Map<string, Func[]>
|
|
52
|
-
cliCommands: string []
|
|
53
|
-
cliCommandsPath: string
|
|
54
|
-
commands: Map<string, ICommand>
|
|
55
|
-
platforms: Map<string, IPlatform>
|
|
56
|
-
helper: any
|
|
57
|
-
runOpts: any
|
|
58
|
-
debugger: any
|
|
59
|
-
|
|
60
|
-
constructor (options: IKernelOptions) {
|
|
61
|
-
super()
|
|
62
|
-
this.debugger = process.env.DEBUG === 'Taro:Kernel' ? helper.createDebug('Taro:Kernel') : function () {}
|
|
63
|
-
this.appPath = options.appPath || process.cwd()
|
|
64
|
-
this.optsPresets = options.presets
|
|
65
|
-
this.optsPlugins = options.plugins
|
|
66
|
-
this.config = options.config
|
|
67
|
-
this.hooks = new Map()
|
|
68
|
-
this.methods = new Map()
|
|
69
|
-
this.commands = new Map()
|
|
70
|
-
this.platforms = new Map()
|
|
71
|
-
this.initHelper()
|
|
72
|
-
this.initConfig()
|
|
73
|
-
this.initPaths()
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
initConfig () {
|
|
77
|
-
this.initialConfig = this.config.initialConfig
|
|
78
|
-
this.initialGlobalConfig = this.config.initialGlobalConfig
|
|
79
|
-
this.debugger('initConfig', this.initialConfig)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
initPaths () {
|
|
83
|
-
this.paths = {
|
|
84
|
-
appPath: this.appPath,
|
|
85
|
-
nodeModulesPath: helper.recursiveFindNodeModules(path.join(this.appPath, helper.NODE_MODULES))
|
|
86
|
-
} as IPaths
|
|
87
|
-
if (this.config.isInitSuccess) {
|
|
88
|
-
Object.assign(this.paths, {
|
|
89
|
-
configPath: this.config.configPath,
|
|
90
|
-
sourcePath: path.join(this.appPath, this.initialConfig.sourceRoot as string),
|
|
91
|
-
outputPath: path.resolve(this.appPath, this.initialConfig.outputRoot as string)
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
this.debugger(`initPaths:${JSON.stringify(this.paths, null, 2)}`)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
initHelper () {
|
|
98
|
-
this.helper = helper
|
|
99
|
-
this.debugger('initHelper')
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
initPresetsAndPlugins () {
|
|
103
|
-
const initialConfig = this.initialConfig
|
|
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)
|
|
111
|
-
process.env.NODE_ENV !== 'test' &&
|
|
112
|
-
helper.createSwcRegister({
|
|
113
|
-
only: [
|
|
114
|
-
...Object.keys(cliAndProjectConfigPresets),
|
|
115
|
-
...Object.keys(cliAndProjectPlugins),
|
|
116
|
-
...Object.keys(globalPresets),
|
|
117
|
-
...Object.keys(globalPlugins)
|
|
118
|
-
]
|
|
119
|
-
})
|
|
120
|
-
this.plugins = new Map()
|
|
121
|
-
this.extraPlugins = {}
|
|
122
|
-
this.globalExtraPlugins = {}
|
|
123
|
-
this.resolvePresets(cliAndProjectConfigPresets, globalPresets)
|
|
124
|
-
this.resolvePlugins(cliAndProjectPlugins, globalPlugins)
|
|
125
|
-
}
|
|
126
|
-
|
|
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)
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
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)
|
|
149
|
-
|
|
150
|
-
while (resolvedPlugins.length) {
|
|
151
|
-
this.initPlugin(resolvedPlugins.shift()!)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
this.extraPlugins = {}
|
|
155
|
-
this.globalExtraPlugins = {}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
initPreset (preset: IPreset, isGlobalConfigPreset?: boolean) {
|
|
159
|
-
this.debugger('initPreset', preset)
|
|
160
|
-
const { id, path, opts, apply } = preset
|
|
161
|
-
const pluginCtx = this.initPluginCtx({ id, path, ctx: this })
|
|
162
|
-
const { presets, plugins } = apply()(pluginCtx, opts) || {}
|
|
163
|
-
this.registerPlugin(preset)
|
|
164
|
-
if (Array.isArray(presets)) {
|
|
165
|
-
const _presets = resolvePresetsOrPlugins(this.appPath, convertPluginsToObject(presets)(), PluginType.Preset, isGlobalConfigPreset)
|
|
166
|
-
while (_presets.length) {
|
|
167
|
-
this.initPreset(_presets.shift()!, isGlobalConfigPreset)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (Array.isArray(plugins)) {
|
|
171
|
-
isGlobalConfigPreset
|
|
172
|
-
? (this.globalExtraPlugins = merge(this.globalExtraPlugins, convertPluginsToObject(plugins)()))
|
|
173
|
-
: (this.extraPlugins = merge(this.extraPlugins, convertPluginsToObject(plugins)()))
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
initPlugin (plugin: IPlugin) {
|
|
178
|
-
const { id, path, opts, apply } = plugin
|
|
179
|
-
const pluginCtx = this.initPluginCtx({ id, path, ctx: this })
|
|
180
|
-
this.debugger('initPlugin', plugin)
|
|
181
|
-
this.registerPlugin(plugin)
|
|
182
|
-
apply()(pluginCtx, opts)
|
|
183
|
-
this.checkPluginOpts(pluginCtx, opts)
|
|
184
|
-
}
|
|
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
|
-
|
|
201
|
-
checkPluginOpts (pluginCtx, opts) {
|
|
202
|
-
if (typeof pluginCtx.optsSchema !== 'function') {
|
|
203
|
-
return
|
|
204
|
-
}
|
|
205
|
-
this.debugger('checkPluginOpts', pluginCtx)
|
|
206
|
-
const joi = require('joi')
|
|
207
|
-
const schema = pluginCtx.optsSchema(joi)
|
|
208
|
-
if (!joi.isSchema(schema)) {
|
|
209
|
-
throw new Error(`插件${pluginCtx.id}中设置参数检查 schema 有误,请检查!`)
|
|
210
|
-
}
|
|
211
|
-
const { error } = schema.validate(opts)
|
|
212
|
-
if (error) {
|
|
213
|
-
error.message = `插件${pluginCtx.id}获得的参数不符合要求,请检查!`
|
|
214
|
-
throw error
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
registerPlugin (plugin: IPlugin) {
|
|
219
|
-
this.debugger('registerPlugin', plugin)
|
|
220
|
-
if (this.plugins.has(plugin.id)) {
|
|
221
|
-
throw new Error(`插件 ${plugin.id} 已被注册`)
|
|
222
|
-
}
|
|
223
|
-
this.plugins.set(plugin.id, plugin)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
initPluginCtx ({ id, path, ctx }: { id: string, path: string, ctx: Kernel }) {
|
|
227
|
-
const pluginCtx = new Plugin({ id, path, ctx })
|
|
228
|
-
const internalMethods = ['onReady', 'onStart']
|
|
229
|
-
const kernelApis = [
|
|
230
|
-
'appPath',
|
|
231
|
-
'plugins',
|
|
232
|
-
'platforms',
|
|
233
|
-
'paths',
|
|
234
|
-
'helper',
|
|
235
|
-
'runOpts',
|
|
236
|
-
'initialConfig',
|
|
237
|
-
'applyPlugins',
|
|
238
|
-
'applyCliCommandPlugin'
|
|
239
|
-
]
|
|
240
|
-
internalMethods.forEach(name => {
|
|
241
|
-
if (!this.methods.has(name)) {
|
|
242
|
-
pluginCtx.registerMethod(name)
|
|
243
|
-
}
|
|
244
|
-
})
|
|
245
|
-
return new Proxy(pluginCtx, {
|
|
246
|
-
get: (target, name: string) => {
|
|
247
|
-
if (this.methods.has(name)) {
|
|
248
|
-
const method = this.methods.get(name)
|
|
249
|
-
if (Array.isArray(method)) {
|
|
250
|
-
return (...arg) => {
|
|
251
|
-
method.forEach(item => {
|
|
252
|
-
item.apply(this, arg)
|
|
253
|
-
})
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return method
|
|
257
|
-
}
|
|
258
|
-
if (kernelApis.includes(name)) {
|
|
259
|
-
return typeof this[name] === 'function' ? this[name].bind(this) : this[name]
|
|
260
|
-
}
|
|
261
|
-
return target[name]
|
|
262
|
-
}
|
|
263
|
-
})
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async applyPlugins (args: string | { name: string, initialVal?: any, opts?: any }) {
|
|
267
|
-
let name
|
|
268
|
-
let initialVal
|
|
269
|
-
let opts
|
|
270
|
-
if (typeof args === 'string') {
|
|
271
|
-
name = args
|
|
272
|
-
} else {
|
|
273
|
-
name = args.name
|
|
274
|
-
initialVal = args.initialVal
|
|
275
|
-
opts = args.opts
|
|
276
|
-
}
|
|
277
|
-
this.debugger('applyPlugins')
|
|
278
|
-
this.debugger(`applyPlugins:name:${name}`)
|
|
279
|
-
this.debugger(`applyPlugins:initialVal:${initialVal}`)
|
|
280
|
-
this.debugger(`applyPlugins:opts:${opts}`)
|
|
281
|
-
if (typeof name !== 'string') {
|
|
282
|
-
throw new Error('调用失败,未传入正确的名称!')
|
|
283
|
-
}
|
|
284
|
-
const hooks = this.hooks.get(name) || []
|
|
285
|
-
if (!hooks.length) {
|
|
286
|
-
return await initialVal
|
|
287
|
-
}
|
|
288
|
-
const waterfall = new AsyncSeriesWaterfallHook(['arg'])
|
|
289
|
-
if (hooks.length) {
|
|
290
|
-
const resArr: any[] = []
|
|
291
|
-
for (const hook of hooks) {
|
|
292
|
-
waterfall.tapPromise({
|
|
293
|
-
name: hook.plugin!,
|
|
294
|
-
stage: hook.stage || 0,
|
|
295
|
-
// @ts-ignore
|
|
296
|
-
before: hook.before
|
|
297
|
-
}, async arg => {
|
|
298
|
-
const res = await hook.fn(opts, arg)
|
|
299
|
-
if (IS_MODIFY_HOOK.test(name) && IS_EVENT_HOOK.test(name)) {
|
|
300
|
-
return res
|
|
301
|
-
}
|
|
302
|
-
if (IS_ADD_HOOK.test(name)) {
|
|
303
|
-
resArr.push(res)
|
|
304
|
-
return resArr
|
|
305
|
-
}
|
|
306
|
-
return null
|
|
307
|
-
})
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
return await waterfall.promise(initialVal)
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
runWithPlatform (platform) {
|
|
314
|
-
if (!this.platforms.has(platform)) {
|
|
315
|
-
throw new Error(`不存在编译平台 ${platform}`)
|
|
316
|
-
}
|
|
317
|
-
const config = this.platforms.get(platform)!
|
|
318
|
-
const withNameConfig = this.config.getConfigWithNamed(config.name, config.useConfigName)
|
|
319
|
-
process.env.TARO_PLATFORM = getPlatformType(config.name, config.useConfigName)
|
|
320
|
-
return withNameConfig
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
setRunOpts (opts) {
|
|
324
|
-
this.runOpts = opts
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
runHelp (name: string) {
|
|
328
|
-
const command = this.commands.get(name)
|
|
329
|
-
const defaultOptionsMap = new Map()
|
|
330
|
-
defaultOptionsMap.set('-h, --help', 'output usage information')
|
|
331
|
-
let customOptionsMap = new Map()
|
|
332
|
-
if (command?.optionsMap) {
|
|
333
|
-
customOptionsMap = new Map(Object.entries(command?.optionsMap))
|
|
334
|
-
}
|
|
335
|
-
const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap])
|
|
336
|
-
printHelpLog(name, optionsMap, command?.synopsisList ? new Set(command?.synopsisList) : new Set())
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
async run (args: string | { name: string, opts?: any }) {
|
|
340
|
-
let name
|
|
341
|
-
let opts
|
|
342
|
-
if (typeof args === 'string') {
|
|
343
|
-
name = args
|
|
344
|
-
} else {
|
|
345
|
-
name = args.name
|
|
346
|
-
opts = args.opts
|
|
347
|
-
}
|
|
348
|
-
this.debugger('command:run')
|
|
349
|
-
this.debugger(`command:run:name:${name}`)
|
|
350
|
-
this.debugger('command:runOpts')
|
|
351
|
-
this.debugger(`command:runOpts:${JSON.stringify(opts, null, 2)}`)
|
|
352
|
-
this.setRunOpts(opts)
|
|
353
|
-
|
|
354
|
-
this.debugger('initPresetsAndPlugins')
|
|
355
|
-
this.initPresetsAndPlugins()
|
|
356
|
-
|
|
357
|
-
await this.applyPlugins('onReady')
|
|
358
|
-
|
|
359
|
-
this.debugger('command:onStart')
|
|
360
|
-
await this.applyPlugins('onStart')
|
|
361
|
-
|
|
362
|
-
if (!this.commands.has(name)) {
|
|
363
|
-
throw new Error(`${name} 命令不存在`)
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
if (opts?.isHelp) {
|
|
367
|
-
return this.runHelp(name)
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
if (opts?.options?.platform) {
|
|
371
|
-
opts.config = this.runWithPlatform(opts.options.platform)
|
|
372
|
-
await this.applyPlugins({
|
|
373
|
-
name: 'modifyRunnerOpts',
|
|
374
|
-
opts: {
|
|
375
|
-
opts: opts?.config
|
|
376
|
-
}
|
|
377
|
-
})
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
await this.applyPlugins({
|
|
381
|
-
name,
|
|
382
|
-
opts
|
|
383
|
-
})
|
|
384
|
-
}
|
|
385
|
-
}
|
package/src/Plugin.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { addPlatforms } from '@tarojs/helper'
|
|
2
|
-
|
|
3
|
-
import type Kernel from './Kernel'
|
|
4
|
-
import type { Func, ICommand, IHook, IPlatform } from './utils/types'
|
|
5
|
-
|
|
6
|
-
export default class Plugin {
|
|
7
|
-
id: string
|
|
8
|
-
path: string
|
|
9
|
-
ctx: Kernel
|
|
10
|
-
optsSchema: Func
|
|
11
|
-
|
|
12
|
-
constructor (opts) {
|
|
13
|
-
this.id = opts.id
|
|
14
|
-
this.path = opts.path
|
|
15
|
-
this.ctx = opts.ctx
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
register (hook: IHook) {
|
|
19
|
-
if (typeof hook.name !== 'string') {
|
|
20
|
-
throw new Error(`插件 ${this.id} 中注册 hook 失败, hook.name 必须是 string 类型`)
|
|
21
|
-
}
|
|
22
|
-
if (typeof hook.fn !== 'function') {
|
|
23
|
-
throw new Error(`插件 ${this.id} 中注册 hook 失败, hook.fn 必须是 function 类型`)
|
|
24
|
-
}
|
|
25
|
-
const hooks = this.ctx.hooks.get(hook.name) || []
|
|
26
|
-
hook.plugin = this.id
|
|
27
|
-
this.ctx.hooks.set(hook.name, hooks.concat(hook))
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
registerCommand (command: ICommand) {
|
|
31
|
-
if (this.ctx.commands.has(command.name)) {
|
|
32
|
-
throw new Error(`命令 ${command.name} 已存在`)
|
|
33
|
-
}
|
|
34
|
-
this.ctx.commands.set(command.name, command)
|
|
35
|
-
this.register(command)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
registerPlatform (platform: IPlatform) {
|
|
39
|
-
if (this.ctx.platforms.has(platform.name)) {
|
|
40
|
-
throw new Error(`适配平台 ${platform.name} 已存在`)
|
|
41
|
-
}
|
|
42
|
-
addPlatforms(platform.name)
|
|
43
|
-
this.ctx.platforms.set(platform.name, platform)
|
|
44
|
-
this.register(platform)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
registerMethod (...args) {
|
|
48
|
-
const { name, fn } = processArgs(args)
|
|
49
|
-
const methods = this.ctx.methods.get(name) || []
|
|
50
|
-
methods.push(fn || function (fn: Func) {
|
|
51
|
-
this.register({
|
|
52
|
-
name,
|
|
53
|
-
fn
|
|
54
|
-
})
|
|
55
|
-
}.bind(this))
|
|
56
|
-
this.ctx.methods.set(name, methods)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
addPluginOptsSchema (schema) {
|
|
60
|
-
this.optsSchema = schema
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function processArgs (args) {
|
|
65
|
-
let name, fn
|
|
66
|
-
if (!args.length) {
|
|
67
|
-
throw new Error('参数为空')
|
|
68
|
-
} else if (args.length === 1) {
|
|
69
|
-
if (typeof args[0] === 'string') {
|
|
70
|
-
name = args[0]
|
|
71
|
-
} else {
|
|
72
|
-
name = args[0].name
|
|
73
|
-
fn = args[0].fn
|
|
74
|
-
}
|
|
75
|
-
} else {
|
|
76
|
-
name = args[0]
|
|
77
|
-
fn = args[1]
|
|
78
|
-
}
|
|
79
|
-
return { name, fn }
|
|
80
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import Config from './Config'
|
|
2
|
-
import Kernel from './Kernel'
|
|
3
|
-
import { TaroPlatformBase, TaroPlatformWeb } from './platform-plugin-base'
|
|
4
|
-
|
|
5
|
-
export * from './utils/types'
|
|
6
|
-
export { Config, Kernel, TaroPlatformBase, TaroPlatformWeb }
|
|
7
|
-
export default { Config, Kernel, TaroPlatformBase, TaroPlatformWeb }
|
|
8
|
-
|
|
9
|
-
export type { IPluginContext } from './utils/types'
|