@tarojs/service 3.3.20 → 3.4.0-beta.3

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/dist/Kernel.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
3
  import { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile';
4
- import { IPreset, IPlugin, IPaths, IHook, ICommand, IPlatform } from './utils/types';
4
+ import { IPreset, IPluginsObject, IPlugin, IPaths, IHook, ICommand, IPlatform } from './utils/types';
5
5
  import Plugin from './Plugin';
6
6
  import Config from './Config';
7
7
  interface IKernelOptions {
@@ -17,7 +17,7 @@ export default class Kernel extends EventEmitter {
17
17
  optsPlugins: PluginItem[] | void;
18
18
  plugins: Map<string, IPlugin>;
19
19
  paths: IPaths;
20
- extraPlugins: IPlugin[];
20
+ extraPlugins: IPluginsObject;
21
21
  config: Config;
22
22
  initialConfig: IProjectConfig;
23
23
  hooks: Map<string, IHook[]>;
@@ -51,6 +51,7 @@ export default class Kernel extends EventEmitter {
51
51
  }): Promise<any>;
52
52
  runWithPlatform(platform: any): any;
53
53
  setRunOpts(opts: any): void;
54
+ runHelp(name: string): void;
54
55
  run(args: string | {
55
56
  name: string;
56
57
  opts?: any;
package/dist/Kernel.js CHANGED
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const path = require("path");
13
13
  const events_1 = require("events");
14
+ const lodash_1 = require("lodash");
14
15
  const tapable_1 = require("tapable");
15
16
  const helper_1 = require("@tarojs/helper");
16
17
  const helper = require("@tarojs/helper");
@@ -76,7 +77,7 @@ class Kernel extends events_1.EventEmitter {
76
77
  only: [...Object.keys(allConfigPresets), ...Object.keys(allConfigPlugins)]
77
78
  });
78
79
  this.plugins = new Map();
79
- this.extraPlugins = [];
80
+ this.extraPlugins = {};
80
81
  this.resolvePresets(allConfigPresets);
81
82
  this.resolvePlugins(allConfigPlugins);
82
83
  }
@@ -87,12 +88,12 @@ class Kernel extends events_1.EventEmitter {
87
88
  }
88
89
  }
89
90
  resolvePlugins(plugins) {
91
+ plugins = lodash_1.merge(this.extraPlugins, plugins);
90
92
  const allPlugins = utils_1.resolvePresetsOrPlugins(this.appPath, plugins, constants_1.PluginType.Plugin);
91
- const _plugins = [...this.extraPlugins, ...allPlugins];
92
- while (_plugins.length) {
93
- this.initPlugin(_plugins.shift());
93
+ while (allPlugins.length) {
94
+ this.initPlugin(allPlugins.shift());
94
95
  }
95
- this.extraPlugins = [];
96
+ this.extraPlugins = {};
96
97
  }
97
98
  initPreset(preset) {
98
99
  this.debugger('initPreset', preset);
@@ -107,7 +108,7 @@ class Kernel extends events_1.EventEmitter {
107
108
  }
108
109
  }
109
110
  if (Array.isArray(plugins)) {
110
- this.extraPlugins.push(...utils_1.resolvePresetsOrPlugins(this.appPath, utils_1.convertPluginsToObject(plugins)(), constants_1.PluginType.Plugin));
111
+ this.extraPlugins = lodash_1.merge(this.extraPlugins, utils_1.convertPluginsToObject(plugins)());
111
112
  }
112
113
  }
113
114
  initPlugin(plugin) {
@@ -232,6 +233,17 @@ class Kernel extends events_1.EventEmitter {
232
233
  setRunOpts(opts) {
233
234
  this.runOpts = opts;
234
235
  }
236
+ runHelp(name) {
237
+ const command = this.commands.get(name);
238
+ const defaultOptionsMap = new Map();
239
+ defaultOptionsMap.set('-h, --help', 'output usage information');
240
+ let customOptionsMap = new Map();
241
+ if (command === null || command === void 0 ? void 0 : command.optionsMap) {
242
+ customOptionsMap = new Map(Object.entries(command === null || command === void 0 ? void 0 : command.optionsMap));
243
+ }
244
+ const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap]);
245
+ utils_1.printHelpLog(name, optionsMap, (command === null || command === void 0 ? void 0 : command.synopsisList) ? new Set(command === null || command === void 0 ? void 0 : command.synopsisList) : new Set());
246
+ }
235
247
  run(args) {
236
248
  var _a;
237
249
  return __awaiter(this, void 0, void 0, function* () {
@@ -256,16 +268,7 @@ class Kernel extends events_1.EventEmitter {
256
268
  throw new Error(`${name} 命令不存在`);
257
269
  }
258
270
  if (opts === null || opts === void 0 ? void 0 : opts.isHelp) {
259
- const command = this.commands.get(name);
260
- const defaultOptionsMap = new Map();
261
- defaultOptionsMap.set('-h, --help', 'output usage information');
262
- let customOptionsMap = new Map();
263
- if (command === null || command === void 0 ? void 0 : command.optionsMap) {
264
- customOptionsMap = new Map(Object.entries(command === null || command === void 0 ? void 0 : command.optionsMap));
265
- }
266
- const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap]);
267
- utils_1.printHelpLog(name, optionsMap, (command === null || command === void 0 ? void 0 : command.synopsisList) ? new Set(command === null || command === void 0 ? void 0 : command.synopsisList) : new Set());
268
- return;
271
+ return this.runHelp(name);
269
272
  }
270
273
  if ((_a = opts === null || opts === void 0 ? void 0 : opts.options) === null || _a === void 0 ? void 0 : _a.platform) {
271
274
  opts.config = this.runWithPlatform(opts.options.platform);
@@ -1,9 +1,9 @@
1
1
  import { PluginItem } from '@tarojs/taro/types/compile';
2
2
  import { PluginType } from './constants';
3
- import { IPlugin } from './types';
3
+ import { IPluginsObject, IPlugin } from './types';
4
4
  export declare const isNpmPkg: (name: string) => boolean;
5
5
  export declare function getPluginPath(pluginPath: string): string;
6
- export declare function convertPluginsToObject(items: PluginItem[]): () => {};
7
- export declare function mergePlugins(dist: PluginItem[], src: PluginItem[]): () => {};
6
+ export declare function convertPluginsToObject(items: PluginItem[]): () => IPluginsObject;
7
+ export declare function mergePlugins(dist: PluginItem[], src: PluginItem[]): () => IPluginsObject;
8
8
  export declare function resolvePresetsOrPlugins(root: string, args: any, type: PluginType): IPlugin[];
9
9
  export declare function printHelpLog(command: any, optionsList: Map<string, string>, synopsisList?: Set<string>): void;
@@ -43,10 +43,24 @@ exports.mergePlugins = mergePlugins;
43
43
  // getModuleDefaultExport
44
44
  function resolvePresetsOrPlugins(root, args, type) {
45
45
  return Object.keys(args).map(item => {
46
- const fPath = resolve.sync(item, {
47
- basedir: root,
48
- extensions: ['.js', '.ts']
49
- });
46
+ var _a;
47
+ let fPath;
48
+ try {
49
+ fPath = resolve.sync(item, {
50
+ basedir: root,
51
+ extensions: ['.js', '.ts']
52
+ });
53
+ }
54
+ catch (err) {
55
+ if ((_a = args[item]) === null || _a === void 0 ? void 0 : _a.backup) {
56
+ // 如果项目中没有,可以使用 CLI 中的插件
57
+ fPath = args[item].backup;
58
+ }
59
+ else {
60
+ console.log(helper_1.chalk.red(`找不到依赖 "${item}",请先在项目中安装`));
61
+ process.exit(1);
62
+ }
63
+ }
50
64
  return {
51
65
  id: fPath,
52
66
  path: fPath,
@@ -22,6 +22,7 @@ export interface IPaths {
22
22
  nodeModulesPath: string;
23
23
  }
24
24
  export declare type Func = (...args: any[]) => any;
25
+ export declare type IPluginsObject = Record<string, Record<any, any> | null>;
25
26
  export interface IPlugin {
26
27
  id: string;
27
28
  path: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/service",
3
- "version": "3.3.20",
3
+ "version": "3.4.0-beta.3",
4
4
  "description": "Taro Service",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -34,13 +34,13 @@
34
34
  "homepage": "https://github.com/NervJS/taro#readme",
35
35
  "dependencies": {
36
36
  "@hapi/joi": "17.1.1",
37
- "@tarojs/helper": "3.3.20",
38
- "@tarojs/shared": "3.3.20",
39
- "@tarojs/taro": "3.3.20",
40
- "fs-extra": "8.1.0",
41
- "lodash": "4.17.21",
42
- "resolve": "1.15.1",
43
- "tapable": "1.1.3"
37
+ "@tarojs/helper": "3.4.0-beta.3",
38
+ "@tarojs/shared": "3.4.0-beta.3",
39
+ "@tarojs/taro": "3.4.0-beta.3",
40
+ "fs-extra": "^8.0.1",
41
+ "lodash": "^4.17.21",
42
+ "resolve": "^1.6.0",
43
+ "tapable": "^1.1.3"
44
44
  },
45
- "gitHead": "f98ceefaa3e419db5a6c3bcd3bf123ce5a0a0124"
45
+ "gitHead": "2ef7e8c10c9e5784e98bb1ad3a803d5fd6a46c8b"
46
46
  }
package/src/Kernel.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as path from 'path'
2
2
  import { EventEmitter } from 'events'
3
-
3
+ import { merge } from 'lodash'
4
4
  import { AsyncSeriesWaterfallHook } from 'tapable'
5
5
  import { IProjectConfig, PluginItem } from '@tarojs/taro/types/compile'
6
6
  import {
@@ -14,6 +14,7 @@ import * as joi from '@hapi/joi'
14
14
 
15
15
  import {
16
16
  IPreset,
17
+ IPluginsObject,
17
18
  IPlugin,
18
19
  IPaths,
19
20
  IHook,
@@ -44,7 +45,7 @@ export default class Kernel extends EventEmitter {
44
45
  optsPlugins: PluginItem[] | void
45
46
  plugins: Map<string, IPlugin>
46
47
  paths: IPaths
47
- extraPlugins: IPlugin[]
48
+ extraPlugins: IPluginsObject
48
49
  config: Config
49
50
  initialConfig: IProjectConfig
50
51
  hooks: Map<string, IHook[]>
@@ -114,7 +115,7 @@ export default class Kernel extends EventEmitter {
114
115
  only: [...Object.keys(allConfigPresets), ...Object.keys(allConfigPlugins)]
115
116
  })
116
117
  this.plugins = new Map()
117
- this.extraPlugins = []
118
+ this.extraPlugins = {}
118
119
  this.resolvePresets(allConfigPresets)
119
120
  this.resolvePlugins(allConfigPlugins)
120
121
  }
@@ -127,12 +128,13 @@ export default class Kernel extends EventEmitter {
127
128
  }
128
129
 
129
130
  resolvePlugins (plugins) {
131
+ plugins = merge(this.extraPlugins, plugins)
130
132
  const allPlugins = resolvePresetsOrPlugins(this.appPath, plugins, PluginType.Plugin)
131
- const _plugins = [...this.extraPlugins, ...allPlugins]
132
- while (_plugins.length) {
133
- this.initPlugin(_plugins.shift()!)
133
+
134
+ while (allPlugins.length) {
135
+ this.initPlugin(allPlugins.shift()!)
134
136
  }
135
- this.extraPlugins = []
137
+ this.extraPlugins = {}
136
138
  }
137
139
 
138
140
  initPreset (preset: IPreset) {
@@ -148,7 +150,7 @@ export default class Kernel extends EventEmitter {
148
150
  }
149
151
  }
150
152
  if (Array.isArray(plugins)) {
151
- this.extraPlugins.push(...resolvePresetsOrPlugins(this.appPath, convertPluginsToObject(plugins)(), PluginType.Plugin))
153
+ this.extraPlugins = merge(this.extraPlugins, convertPluginsToObject(plugins)())
152
154
  }
153
155
  }
154
156
 
@@ -278,6 +280,18 @@ export default class Kernel extends EventEmitter {
278
280
  this.runOpts = opts
279
281
  }
280
282
 
283
+ runHelp (name: string) {
284
+ const command = this.commands.get(name)
285
+ const defaultOptionsMap = new Map()
286
+ defaultOptionsMap.set('-h, --help', 'output usage information')
287
+ let customOptionsMap = new Map()
288
+ if (command?.optionsMap) {
289
+ customOptionsMap = new Map(Object.entries(command?.optionsMap))
290
+ }
291
+ const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap])
292
+ printHelpLog(name, optionsMap, command?.synopsisList ? new Set(command?.synopsisList) : new Set())
293
+ }
294
+
281
295
  async run (args: string | { name: string, opts?: any }) {
282
296
  let name
283
297
  let opts
@@ -299,16 +313,7 @@ export default class Kernel extends EventEmitter {
299
313
  throw new Error(`${name} 命令不存在`)
300
314
  }
301
315
  if (opts?.isHelp) {
302
- const command = this.commands.get(name)
303
- const defaultOptionsMap = new Map()
304
- defaultOptionsMap.set('-h, --help', 'output usage information')
305
- let customOptionsMap = new Map()
306
- if (command?.optionsMap) {
307
- customOptionsMap = new Map(Object.entries(command?.optionsMap))
308
- }
309
- const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap])
310
- printHelpLog(name, optionsMap, command?.synopsisList ? new Set(command?.synopsisList) : new Set())
311
- return
316
+ return this.runHelp(name)
312
317
  }
313
318
  if (opts?.options?.platform) {
314
319
  opts.config = this.runWithPlatform(opts.options.platform)
@@ -2,12 +2,12 @@ import * as path from 'path'
2
2
 
3
3
  import { merge } from 'lodash'
4
4
  import * as resolve from 'resolve'
5
- import { getModuleDefaultExport } from '@tarojs/helper'
5
+ import { getModuleDefaultExport, chalk } from '@tarojs/helper'
6
6
 
7
7
  import { PluginItem } from '@tarojs/taro/types/compile'
8
8
 
9
9
  import { PluginType } from './constants'
10
- import { IPlugin } from './types'
10
+ import { IPluginsObject, IPlugin } from './types'
11
11
 
12
12
  export const isNpmPkg: (name: string) => boolean = name => !(/^(\.|\/)/.test(name))
13
13
 
@@ -16,9 +16,9 @@ export function getPluginPath (pluginPath: string) {
16
16
  throw new Error('plugin 和 preset 配置必须为绝对路径或者包名')
17
17
  }
18
18
 
19
- export function convertPluginsToObject (items: PluginItem[]) {
19
+ export function convertPluginsToObject (items: PluginItem[]): () => IPluginsObject {
20
20
  return () => {
21
- const obj = {}
21
+ const obj: IPluginsObject = {}
22
22
  if (Array.isArray(items)) {
23
23
  items.forEach(item => {
24
24
  if (typeof item === 'string') {
@@ -45,10 +45,21 @@ export function mergePlugins (dist: PluginItem[], src: PluginItem[]) {
45
45
  // getModuleDefaultExport
46
46
  export function resolvePresetsOrPlugins (root: string, args, type: PluginType): IPlugin[] {
47
47
  return Object.keys(args).map(item => {
48
- const fPath = resolve.sync(item, {
49
- basedir: root,
50
- extensions: ['.js', '.ts']
51
- })
48
+ let fPath
49
+ try {
50
+ fPath = resolve.sync(item, {
51
+ basedir: root,
52
+ extensions: ['.js', '.ts']
53
+ })
54
+ } catch (err) {
55
+ if (args[item]?.backup) {
56
+ // 如果项目中没有,可以使用 CLI 中的插件
57
+ fPath = args[item].backup
58
+ } else {
59
+ console.log(chalk.red(`找不到依赖 "${item}",请先在项目中安装`))
60
+ process.exit(1)
61
+ }
62
+ }
52
63
  return {
53
64
  id: fPath,
54
65
  path: fPath,
@@ -25,6 +25,8 @@ export interface IPaths {
25
25
 
26
26
  export type Func = (...args: any[]) => any
27
27
 
28
+ export type IPluginsObject = Record<string, Record<any, any> | null>
29
+
28
30
  export interface IPlugin {
29
31
  id: string
30
32
  path: string
package/types/index.d.ts CHANGED
@@ -8,6 +8,15 @@ export { default as Kernel } from './Kernel'
8
8
 
9
9
  export { TaroPlatformBase } from './platform-plugin-base'
10
10
 
11
+ interface IModifyWebpackChain {
12
+ componentConfig?: {
13
+ includes: Set<string>
14
+ exclude: Set<string>
15
+ thirdPartyComponents: Map<Tagname, Attrs>
16
+ includeAll: boolean
17
+ }
18
+ }
19
+
11
20
  export declare interface IPluginContext {
12
21
  /**
13
22
  * 获取当前所有挂载的插件
@@ -72,7 +81,7 @@ export declare interface IPluginContext {
72
81
  /**
73
82
  * 编译中修改 webpack 配置,在这个钩子中,你可以对 webpackChain 作出想要的调整,等同于配置 [`webpackChain`](./config-detail.md#miniwebpackchain)
74
83
  */
75
- modifyWebpackChain: (fn: (args: { chain: any }) => void) => void
84
+ modifyWebpackChain: (fn: (args: { chain: any, webpack: any, data?: IModifyWebpackChain }) => void) => void
76
85
  /**
77
86
  * 修改编译后的结果
78
87
  */