@tarojs/service 3.6.22-alpha.5 → 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.
@@ -1,212 +0,0 @@
1
- import { recursiveMerge } from '@tarojs/helper'
2
- import { isObject, PLATFORM_TYPE } from '@tarojs/shared'
3
- import * as path from 'path'
4
-
5
- import { getPkgVersion } from '../utils/package'
6
- import TaroPlatform from './platform'
7
-
8
- import type { RecursiveTemplate, UnRecursiveTemplate } from '@tarojs/shared/dist/template'
9
- import type { TConfig } from '../utils/types'
10
-
11
- interface IFileType {
12
- templ: string
13
- style: string
14
- config: string
15
- script: string
16
- xs?: string
17
- }
18
-
19
- export abstract class TaroPlatformBase<T extends TConfig = TConfig> extends TaroPlatform<T> {
20
- platformType = PLATFORM_TYPE.MINI
21
-
22
- abstract globalObject: string
23
- abstract fileType: IFileType
24
- abstract template: RecursiveTemplate | UnRecursiveTemplate
25
- projectConfigJson?: string
26
- taroComponentsPath?: string
27
-
28
- private projectConfigJsonOutputPath: string
29
-
30
- /**
31
- * 1. 清空 dist 文件夹
32
- * 2. 输出编译提示
33
- * 3. 生成 project.config.json
34
- */
35
- private async setup () {
36
- await this.setupTransaction.perform(this.setupImpl, this)
37
- this.ctx.onSetupClose?.(this)
38
- }
39
-
40
- private setupImpl () {
41
- const { output } = this.config
42
- // webpack5 原生支持 output.clean 选项,但是 webpack4 不支持, 为统一行为,这里做一下兼容
43
- // (在 packages/taro-mini-runner/src/webpack/chain.ts 和 packages/taro-webpack-runner/src/utils/chain.ts 的 makeConfig 中对 clean 选项做了过滤)
44
- // 仅 output.clean 为 false 时不清空输出目录
45
- // eslint-disable-next-line eqeqeq
46
- if (output == undefined || output.clean == undefined || output.clean === true) {
47
- this.emptyOutputDir()
48
- } else if (isObject(output.clean)) {
49
- this.emptyOutputDir(output.clean.keep || [])
50
- }
51
- this.printDevelopmentTip(this.platform)
52
- if (this.projectConfigJson) {
53
- this.generateProjectConfig(this.projectConfigJson)
54
- }
55
- if (this.ctx.initialConfig.logger?.quiet === false) {
56
- const { printLog, processTypeEnum } = this.ctx.helper
57
- printLog(processTypeEnum.START, '开发者工具-项目目录', `${this.ctx.paths.outputPath}`)
58
- }
59
- // Webpack5 代码自动热重载
60
- if (this.compiler === 'webpack5' && this.config.isWatch && this.projectConfigJsonOutputPath) {
61
- try {
62
- const projectConfig = require(this.projectConfigJsonOutputPath)
63
- if (projectConfig.setting?.compileHotReLoad === true) {
64
- this.ctx.modifyWebpackChain(({ chain }) => {
65
- chain.plugin('TaroMiniHMRPlugin')
66
- .use(require(path.join(__dirname, './webpack/hmr-plugin.js')).default)
67
- })
68
- }
69
- } catch (e) {} // eslint-disable-line no-empty
70
- }
71
- }
72
-
73
- protected printDevelopmentTip (platform: string) {
74
- const tips: string[] = []
75
- const config = this.config
76
- const { chalk } = this.helper
77
-
78
- if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
79
- const { isWindows } = this.helper
80
- const exampleCommand = isWindows
81
- ? `$ set NODE_ENV=production && taro build --type ${platform} --watch`
82
- : `$ NODE_ENV=production taro build --type ${platform} --watch`
83
-
84
- tips.push(chalk.yellowBright(`预览模式生成的文件较大,设置 NODE_ENV 为 production 可以开启压缩。
85
- Example:
86
- ${exampleCommand}`))
87
- }
88
-
89
- if (this.compiler === 'webpack5' && !config.cache?.enable) {
90
- tips.push(chalk.yellowBright('建议开启持久化缓存功能,能有效提升二次编译速度,详情请参考: https://docs.taro.zone/docs/config-detail#cache。'))
91
- }
92
-
93
- if (tips.length) {
94
- console.log(chalk.yellowBright('Tips:'))
95
- tips.forEach((item, index) => console.log(`${chalk.yellowBright(index + 1)}. ${item}`))
96
- console.log('\n')
97
- }
98
- }
99
-
100
- /**
101
- * 返回当前项目内的 @tarojs/mini-runner 包
102
- */
103
- protected async getRunner () {
104
- const { appPath } = this.ctx.paths
105
- const { npm } = this.helper
106
-
107
- let runnerPkg: string
108
- switch (this.compiler) {
109
- case 'webpack5':
110
- runnerPkg = '@tarojs/webpack5-runner'
111
- break
112
- default:
113
- runnerPkg = '@tarojs/mini-runner'
114
- }
115
-
116
- const runner = await npm.getNpmPkg(runnerPkg, appPath)
117
-
118
- return runner.bind(null, appPath)
119
- }
120
-
121
- /**
122
- * 准备 mini-runner 参数
123
- * @param extraOptions 需要额外合入 Options 的配置项
124
- */
125
- protected getOptions (extraOptions = {}) {
126
- const { ctx, globalObject, fileType, template } = this
127
-
128
- const config = recursiveMerge(Object.assign({}, this.config), {
129
- env: {
130
- FRAMEWORK: JSON.stringify(this.config.framework),
131
- TARO_ENV: JSON.stringify(this.platform),
132
- TARO_PLATFORM: JSON.stringify(this.platformType),
133
- TARO_VERSION: JSON.stringify(getPkgVersion())
134
- }
135
- })
136
-
137
- return {
138
- ...config,
139
- nodeModulesPath: ctx.paths.nodeModulesPath,
140
- buildAdapter: config.platform,
141
- platformType: this.platformType,
142
- globalObject,
143
- fileType,
144
- template,
145
- ...extraOptions
146
- }
147
- }
148
-
149
- /**
150
- * 调用 mini-runner 开始编译
151
- * @param extraOptions 需要额外传入 @tarojs/mini-runner 的配置项
152
- */
153
- private async build (extraOptions = {}) {
154
- this.ctx.onBuildInit?.(this)
155
- await this.buildTransaction.perform(this.buildImpl, this, extraOptions)
156
- }
157
-
158
- private async buildImpl (extraOptions = {}) {
159
- const runner = await this.getRunner()
160
- const options = this.getOptions(
161
- Object.assign(
162
- {
163
- runtimePath: this.runtimePath,
164
- taroComponentsPath: this.taroComponentsPath
165
- },
166
- extraOptions
167
- )
168
- )
169
- await runner(options)
170
- }
171
-
172
- /**
173
- * 生成 project.config.json
174
- * @param src 项目源码中配置文件的名称
175
- * @param dist 编译后配置文件的名称,默认为 'project.config.json'
176
- */
177
- protected generateProjectConfig (src: string, dist = 'project.config.json') {
178
- if (this.config.isBuildNativeComp) return
179
- this.ctx.generateProjectConfig({
180
- srcConfigName: src,
181
- distConfigName: dist
182
- })
183
- this.projectConfigJsonOutputPath = `${this.ctx.paths.outputPath}/${dist}`
184
- }
185
-
186
- /**
187
- * 递归替换对象的 key 值
188
- */
189
- protected recursiveReplaceObjectKeys (obj, keyMap) {
190
- Object.keys(obj).forEach((key) => {
191
- if (keyMap[key]) {
192
- obj[keyMap[key]] = obj[key]
193
- if (typeof obj[key] === 'object') {
194
- this.recursiveReplaceObjectKeys(obj[keyMap[key]], keyMap)
195
- }
196
- delete obj[key]
197
- } else if (keyMap[key] === false) {
198
- delete obj[key]
199
- } else if (typeof obj[key] === 'object') {
200
- this.recursiveReplaceObjectKeys(obj[key], keyMap)
201
- }
202
- })
203
- }
204
-
205
- /**
206
- * 调用 mini-runner 开启编译
207
- */
208
- public async start () {
209
- await this.setup()
210
- await this.build()
211
- }
212
- }
@@ -1,75 +0,0 @@
1
- import { PLATFORM_TYPE } from '@tarojs/shared'
2
-
3
- import type { Func, IPluginContext, TConfig } from '../utils/types'
4
-
5
- interface IWrapper {
6
- init? (): void
7
- close? (): void
8
- }
9
-
10
- export class Transaction<T = TaroPlatform> {
11
- wrappers: IWrapper[] = []
12
-
13
- async perform (fn: Func, scope: T, ...args: any[]) {
14
- this.initAll(scope)
15
- await fn.call(scope, ...args)
16
- this.closeAll(scope)
17
- }
18
-
19
- initAll (scope: T) {
20
- const wrappers = this.wrappers
21
- wrappers.forEach(wrapper => wrapper.init?.call(scope))
22
- }
23
-
24
- closeAll (scope: T) {
25
- const wrappers = this.wrappers
26
- wrappers.forEach(wrapper => wrapper.close?.call(scope))
27
- }
28
-
29
- addWrapper (wrapper: IWrapper) {
30
- this.wrappers.push(wrapper)
31
- }
32
- }
33
-
34
- export default abstract class TaroPlatform<T extends TConfig = TConfig> {
35
- protected ctx: IPluginContext
36
- protected config: T
37
- protected helper: IPluginContext['helper']
38
- protected compiler: string
39
-
40
- abstract platformType: PLATFORM_TYPE
41
- abstract platform: string
42
- abstract runtimePath: string | string[]
43
-
44
- protected setupTransaction = new Transaction<this>()
45
- protected buildTransaction = new Transaction<this>()
46
-
47
- constructor (ctx: IPluginContext, config: T) {
48
- this.ctx = ctx
49
- this.helper = ctx.helper
50
- this.config = config
51
- this.updateOutputPath(config)
52
- const _compiler = config.compiler
53
- this.compiler = typeof _compiler === 'object' ? _compiler.type : _compiler
54
- }
55
-
56
- protected emptyOutputDir (excludes: Array<string | RegExp> = []) {
57
- const { outputPath } = this.ctx.paths
58
- this.helper.emptyDirectory(outputPath, { excludes })
59
- }
60
-
61
- /**
62
- * 如果分端编译详情 webpack 配置了 output 则需更新 outputPath 位置
63
- */
64
- private updateOutputPath (config: TConfig) {
65
- const platformPath = config.output?.path
66
- if(platformPath) {
67
- this.ctx.paths.outputPath = platformPath
68
- }
69
- }
70
-
71
- /**
72
- * 调用 runner 开启编译
73
- */
74
- abstract start(): Promise<void>
75
- }
@@ -1,133 +0,0 @@
1
- import { isObject, PLATFORM_TYPE } from '@tarojs/shared'
2
- import { get, merge } from 'lodash'
3
- import * as path from 'path'
4
-
5
- import { getPkgVersion } from '../utils/package'
6
- import TaroPlatform from './platform'
7
-
8
- import type { TConfig } from '../utils/types'
9
-
10
- export abstract class TaroPlatformWeb<T extends TConfig = TConfig> extends TaroPlatform<T> {
11
- platformType = PLATFORM_TYPE.WEB
12
-
13
- /**
14
- * 1. 清空 dist 文件夹
15
- * 2. 输出编译提示
16
- */
17
- private async setup () {
18
- await this.setupTransaction.perform(this.setupWebApp, this)
19
- this.ctx.onSetupClose?.(this)
20
- }
21
-
22
- private setupWebApp () {
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) {
28
- this.emptyOutputDir()
29
- } else if (isObject(output.clean)) {
30
- this.emptyOutputDir(output.clean.keep || [])
31
- }
32
- this.printDevelopmentTip()
33
- }
34
-
35
- protected printDevelopmentTip () {
36
- const tips: string[] = []
37
- const config = this.config
38
- const { chalk } = this.helper
39
-
40
- if (this.compiler === 'webpack5' && !config.cache?.enable) {
41
- tips.push(chalk.yellowBright('建议开启持久化缓存功能,能有效提升二次编译速度,详情请参考: https://docs.taro.zone/docs/config-detail#cache。'))
42
- }
43
-
44
- if (tips.length) {
45
- console.log(chalk.yellowBright('Tips:'))
46
- tips.forEach((item, index) => console.log(`${chalk.yellowBright(index + 1)}. ${item}`))
47
- console.log('\n')
48
- }
49
- }
50
-
51
- /**
52
- * 返回当前项目内的 runner 包
53
- */
54
- protected async getRunner () {
55
- const { appPath } = this.ctx.paths
56
- const { npm } = this.helper
57
-
58
- let runnerPkg: string
59
- switch (this.compiler) {
60
- case 'webpack5':
61
- runnerPkg = '@tarojs/webpack5-runner'
62
- break
63
- default:
64
- runnerPkg = '@tarojs/webpack-runner'
65
- }
66
-
67
- const runner = await npm.getNpmPkg(runnerPkg, appPath)
68
-
69
- return runner.bind(null, appPath)
70
- }
71
-
72
- /**
73
- * 准备 runner 参数
74
- * @param extraOptions 需要额外合入 Options 的配置项
75
- */
76
- protected getOptions (extraOptions = {}) {
77
- const { sourcePath } = this.ctx.paths
78
- const { initialConfig } = this.ctx
79
- const { port } = this.ctx.runOpts.options
80
- const { recursiveMerge, ENTRY, SOURCE_DIR, OUTPUT_DIR } = this.ctx.helper
81
- const entryFileName = `${ENTRY}.config`
82
- const entryFile = path.basename(entryFileName)
83
- const defaultEntry = {
84
- [ENTRY]: [path.join(sourcePath, entryFile)]
85
- }
86
- const customEntry = get(initialConfig, 'h5.entry')
87
- const config = recursiveMerge(Object.assign({}, this.config), {
88
- entryFileName: ENTRY,
89
- env: {
90
- FRAMEWORK: JSON.stringify(this.config.framework),
91
- TARO_ENV: JSON.stringify(this.platform),
92
- TARO_PLATFORM: JSON.stringify(this.platformType),
93
- TARO_VERSION: JSON.stringify(getPkgVersion())
94
- },
95
- devServer: { port },
96
- sourceRoot: this.config.sourceRoot || SOURCE_DIR,
97
- outputRoot: this.config.outputRoot || OUTPUT_DIR
98
- })
99
- config.entry = merge(defaultEntry, customEntry)
100
-
101
- return {
102
- ...config,
103
- buildAdapter: config.platform,
104
- platformType: this.platformType,
105
- ...extraOptions
106
- }
107
- }
108
-
109
- /**
110
- * 调用 runner 开始编译
111
- * @param extraOptions 需要额外传入 runner 的配置项
112
- */
113
- private async build (extraOptions = {}) {
114
- this.ctx.onBuildInit?.(this)
115
- await this.buildTransaction.perform(this.buildWebApp, this, extraOptions)
116
- }
117
-
118
- private async buildWebApp (extraOptions = {}) {
119
- const runner = await this.getRunner()
120
- const options = this.getOptions(Object.assign({
121
- runtimePath: this.runtimePath,
122
- }, extraOptions))
123
- await runner(options)
124
- }
125
-
126
- /**
127
- * 调用 runner 开启编译
128
- */
129
- public async start () {
130
- await this.setup()
131
- await this.build()
132
- }
133
- }
@@ -1,37 +0,0 @@
1
- export default class TaroMiniHMRPlugin {
2
- apply (compiler) {
3
- compiler.hooks.thisCompilation.tap('TaroMiniHMRPlugin', compilation => {
4
- compilation.hooks.beforeChunkAssets.tap('TaroMiniHMRPlugin', () => {
5
- compilation.chunks.forEach(chunk => {
6
- if (chunk.hasRuntime() && chunk.name === 'runtime') {
7
- const runtimeModules = compilation.chunkGraph.getChunkRuntimeModulesInOrder(chunk)
8
- for (const module of runtimeModules) {
9
- if (module.name === 'jsonp chunk loading') {
10
- const runtimeSource = compilation.codeGenerationResults.getSource(
11
- module,
12
- chunk.runtime,
13
- 'runtime'
14
- )
15
- runtimeSource._value += `
16
- var miniHMRCallback = function(parentChunkLoadingFunction, data) {
17
- var chunkIds = data[0];
18
- var moreModules = data[1];
19
- if(chunkIds.some(function(id) { return installedChunks[id] === 0 })) {
20
- chunkIds.forEach(id => {
21
- delete installedChunks[id]
22
- })
23
- Object.keys(moreModules).forEach(id => {
24
- delete __webpack_module_cache__[id]
25
- })
26
- }
27
- if(parentChunkLoadingFunction) parentChunkLoadingFunction(data);
28
- }
29
- chunkLoadingGlobal.push = miniHMRCallback.bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal));`
30
- }
31
- }
32
- }
33
- })
34
- })
35
- })
36
- }
37
- }
@@ -1,21 +0,0 @@
1
- export const CONFIG_DIR_NAME = 'config'
2
- export const DEFAULT_CONFIG_FILE = 'index'
3
-
4
- export const PRESET_PREFIX = '@tarojs/preset-'
5
- export const PLUGIN_PREFIX = '@tarojs/plugin-'
6
-
7
- export const IS_EVENT_HOOK = /^on/
8
- export const IS_ADD_HOOK = /^add/
9
- export const IS_MODIFY_HOOK = /^modify/
10
-
11
- export const presetOrPluginPrefixReg = new RegExp(`^${PRESET_PREFIX}|${PLUGIN_PREFIX}`)
12
-
13
- export enum PluginType {
14
- Preset = 'Preset',
15
- Plugin = 'Plugin'
16
- }
17
-
18
- export const PluginNamePrefix = {
19
- [PluginType.Preset]: PLUGIN_PREFIX,
20
- [PluginType.Plugin]: PLUGIN_PREFIX
21
- }
@@ -1,118 +0,0 @@
1
- import { chalk, getModuleDefaultExport } from '@tarojs/helper'
2
- import { merge } from 'lodash'
3
- import * as path from 'path'
4
- import * as resolve from 'resolve'
5
-
6
- import { PluginType } from './constants'
7
-
8
- import type { PluginItem } from '@tarojs/taro/types/compile'
9
- import type { IPlugin, IPluginsObject } from './types'
10
-
11
- export const isNpmPkg: (name: string) => boolean = name => !(/^(\.|\/)/.test(name))
12
-
13
- export function getPluginPath (pluginPath: string) {
14
- if (isNpmPkg(pluginPath) || path.isAbsolute(pluginPath)) return pluginPath
15
- throw new Error('plugin 和 preset 配置必须为绝对路径或者包名')
16
- }
17
-
18
- export function convertPluginsToObject (items: PluginItem[]): () => IPluginsObject {
19
- return () => {
20
- const obj: IPluginsObject = {}
21
- if (Array.isArray(items)) {
22
- items.forEach(item => {
23
- if (typeof item === 'string') {
24
- const name = getPluginPath(item)
25
- obj[name] = null
26
- } else if (Array.isArray(item)) {
27
- const name = getPluginPath(item[0])
28
- obj[name] = item[1]
29
- }
30
- })
31
- }
32
- return obj
33
- }
34
- }
35
-
36
- export function mergePlugins (dist: PluginItem[], src: PluginItem[]) {
37
- return () => {
38
- const srcObj = convertPluginsToObject(src)()
39
- const distObj = convertPluginsToObject(dist)()
40
- return merge(distObj, srcObj)
41
- }
42
- }
43
-
44
- // getModuleDefaultExport
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]
51
- let fPath
52
- try {
53
- fPath = resolve.sync(item, {
54
- basedir: root,
55
- extensions: ['.js', '.ts']
56
- })
57
- } catch (err) {
58
- if (args[item]?.backup) {
59
- // 如果项目中没有,可以使用 CLI 中的插件
60
- fPath = args[item]?.backup
61
- } else if (skipError) {
62
- // 如果跳过报错,那么 log 提醒,并且不使用该插件
63
- console.log(chalk.yellow(`找不到插件依赖 "${item}",请先在项目中安装,项目路径:${root}`))
64
- continue
65
- } else {
66
- console.log(chalk.red(`找不到插件依赖 "${item}",请先在项目中安装,项目路径:${root}`))
67
- process.exit(1)
68
- }
69
- }
70
- const resolvedItem = {
71
- id: fPath,
72
- path: fPath,
73
- type,
74
- opts: args[item] || {},
75
- apply () {
76
- try {
77
- return getModuleDefaultExport(require(fPath))
78
- } catch (error) {
79
- console.error(error)
80
- // 全局的插件运行报错,不抛出 Error 影响主流程,而是通过 log 提醒然后把插件 filter 掉,保证主流程不变
81
- if(skipError) {
82
- console.error(`插件依赖 "${item}" 加载失败,请检查插件配置`)
83
- } else {
84
- throw new Error(`插件依赖 "${item}" 加载失败,请检查插件配置`)
85
- }
86
- }
87
- }
88
- }
89
- resolvedPresetsOrPlugins.push(resolvedItem)
90
- }
91
-
92
- return resolvedPresetsOrPlugins
93
- }
94
-
95
- function supplementBlank (length) {
96
- return Array(length).map(() => '').join(' ')
97
- }
98
-
99
- export function printHelpLog (command, optionsList: Map<string, string>, synopsisList?: Set<string>) {
100
- console.log(`Usage: taro ${command} [options]`)
101
- console.log()
102
- console.log('Options:')
103
- const keys = Array.from(optionsList.keys())
104
- const maxLength = keys.reduce((v1, v2) => {
105
- return v1.length > v2.length ? v1 : v2
106
- }).length + 3
107
- optionsList.forEach((v, k) => {
108
- const supplementBlankLength = maxLength - k.length
109
- console.log(` ${k}${supplementBlank(supplementBlankLength)}${v}`)
110
- })
111
- if (synopsisList && synopsisList.size) {
112
- console.log()
113
- console.log('Synopsis:')
114
- synopsisList.forEach(item => {
115
- console.log(` $ ${item}`)
116
- })
117
- }
118
- }
@@ -1,15 +0,0 @@
1
- /* eslint-disable no-console */
2
- import * as path from 'path'
3
-
4
- export function getRootPath (): string {
5
- return path.resolve(__dirname, '../../')
6
- }
7
-
8
- export function getPkgVersion (): string {
9
- return require(path.join(getRootPath(), 'package.json')).version
10
- }
11
-
12
- export function printPkgVersion () {
13
- console.log(`👽 Taro v${getPkgVersion()}`)
14
- console.log()
15
- }