@spcsn/taro-cli 0.1.6 → 1.0.0

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.
Files changed (188) hide show
  1. package/README.md +10 -7
  2. package/define-config.d.ts +1 -0
  3. package/define-config.js +1 -0
  4. package/dist/cli.js +140 -110
  5. package/dist/cli.js.map +1 -1
  6. package/dist/commands/{customCommand.d.ts → custom-command.d.ts} +1 -1
  7. package/dist/commands/custom-command.js +23 -0
  8. package/dist/commands/custom-command.js.map +1 -0
  9. package/dist/create/creator.js +2 -0
  10. package/dist/create/creator.js.map +1 -1
  11. package/dist/create/{fetchTemplate.js → fetch-template.js} +26 -33
  12. package/dist/create/fetch-template.js.map +1 -0
  13. package/dist/create/project.d.ts +2 -6
  14. package/dist/create/project.js +240 -285
  15. package/dist/create/project.js.map +1 -1
  16. package/dist/create/template-creator.d.ts +75 -0
  17. package/dist/create/template-creator.js +275 -0
  18. package/dist/create/template-creator.js.map +1 -0
  19. package/dist/doctor/validators.d.ts +7 -6
  20. package/dist/doctor/validators.js +6 -7
  21. package/dist/doctor/validators.js.map +1 -1
  22. package/dist/platform-weapp/apis-list.d.ts +1 -0
  23. package/dist/platform-weapp/apis-list.js +37 -0
  24. package/dist/platform-weapp/apis-list.js.map +1 -0
  25. package/dist/platform-weapp/apis.d.ts +16 -0
  26. package/dist/platform-weapp/apis.js +34 -0
  27. package/dist/platform-weapp/apis.js.map +1 -0
  28. package/dist/platform-weapp/components-react.d.ts +80 -0
  29. package/dist/platform-weapp/components-react.js +86 -0
  30. package/dist/platform-weapp/components-react.js.map +1 -0
  31. package/dist/platform-weapp/components.d.ts +4 -0
  32. package/dist/platform-weapp/components.js +520 -0
  33. package/dist/platform-weapp/components.js.map +1 -0
  34. package/dist/platform-weapp/index.d.ts +11 -0
  35. package/dist/platform-weapp/index.js +17 -0
  36. package/dist/platform-weapp/index.js.map +1 -0
  37. package/dist/platform-weapp/program.d.ts +35 -0
  38. package/dist/platform-weapp/program.js +64 -0
  39. package/dist/platform-weapp/program.js.map +1 -0
  40. package/dist/platform-weapp/runtime-utils.d.ts +27 -0
  41. package/dist/platform-weapp/runtime-utils.js +48 -0
  42. package/dist/platform-weapp/runtime-utils.js.map +1 -0
  43. package/dist/platform-weapp/runtime.d.ts +1 -0
  44. package/dist/platform-weapp/runtime.js +7 -0
  45. package/dist/platform-weapp/runtime.js.map +1 -0
  46. package/dist/platform-weapp/template.d.ts +39 -0
  47. package/dist/platform-weapp/template.js +119 -0
  48. package/dist/platform-weapp/template.js.map +1 -0
  49. package/dist/presets/commands/build.js +25 -45
  50. package/dist/presets/commands/build.js.map +1 -1
  51. package/dist/presets/commands/init.js +2 -4
  52. package/dist/presets/commands/init.js.map +1 -1
  53. package/dist/presets/constant/hooks.d.ts +0 -2
  54. package/dist/presets/constant/hooks.js +1 -3
  55. package/dist/presets/constant/hooks.js.map +1 -1
  56. package/dist/presets/files/generate-framework-info.js +68 -0
  57. package/dist/presets/files/generate-framework-info.js.map +1 -0
  58. package/dist/presets/files/{generateProjectConfig.js → generate-project-config.js} +1 -1
  59. package/dist/presets/files/generate-project-config.js.map +1 -0
  60. package/dist/presets/files/{writeFileToDist.js → write-file-to-dist.js} +1 -1
  61. package/dist/presets/files/write-file-to-dist.js.map +1 -0
  62. package/dist/presets/hooks/build.js +1 -3
  63. package/dist/presets/hooks/build.js.map +1 -1
  64. package/dist/presets/index.js +3 -5
  65. package/dist/presets/index.js.map +1 -1
  66. package/dist/util/{appConfig.d.ts → app-config.d.ts} +2 -2
  67. package/dist/util/{appConfig.js → app-config.js} +4 -3
  68. package/dist/util/app-config.js.map +1 -0
  69. package/dist/util/defineConfig.d.ts +5 -5
  70. package/dist/util/defineConfig.js.map +1 -1
  71. package/dist/util/index.d.ts +9 -1
  72. package/dist/util/index.js +54 -23
  73. package/dist/util/index.js.map +1 -1
  74. package/package.json +36 -39
  75. package/postinstall.js +1 -17
  76. package/src/cli.ts +185 -132
  77. package/src/commands/custom-command.ts +22 -0
  78. package/src/create/{fetchTemplate.ts → fetch-template.ts} +30 -35
  79. package/src/create/project.ts +31 -78
  80. package/src/create/template-creator.ts +325 -0
  81. package/src/doctor/validators.js +1 -0
  82. package/src/doctor/validators.ts +8 -6
  83. package/src/platform-weapp/apis-list.ts +33 -0
  84. package/src/platform-weapp/apis.ts +48 -0
  85. package/src/platform-weapp/components-react.ts +81 -0
  86. package/src/platform-weapp/components.ts +521 -0
  87. package/src/platform-weapp/index.ts +25 -0
  88. package/src/platform-weapp/program.ts +70 -0
  89. package/src/platform-weapp/runtime-utils.ts +57 -0
  90. package/src/platform-weapp/runtime.ts +6 -0
  91. package/src/platform-weapp/template.ts +150 -0
  92. package/src/presets/commands/build.ts +20 -40
  93. package/src/presets/commands/init.ts +1 -4
  94. package/src/presets/constant/hooks.js +12 -0
  95. package/src/presets/constant/hooks.ts +0 -2
  96. package/src/presets/files/{generateFrameworkInfo.ts → generate-framework-info.ts} +7 -1
  97. package/src/presets/hooks/build.ts +1 -3
  98. package/src/presets/index.ts +3 -5
  99. package/src/util/app-config.js +1 -0
  100. package/src/util/{appConfig.ts → app-config.ts} +5 -4
  101. package/src/util/defineConfig.ts +6 -6
  102. package/src/util/index.ts +67 -25
  103. package/templates/default/config/dev.js +1 -1
  104. package/templates/default/config/index.js +28 -40
  105. package/templates/default/config/prod.js +1 -1
  106. package/templates/default/package.json.tmpl +2 -17
  107. package/templates/default/types/global.d.ts +0 -4
  108. package/templates/plugin-compile/README.md +3 -1
  109. package/templates/plugin-compile/package.json.tmpl +1 -2
  110. package/templates/plugin-compile/src/index.ts +18 -28
  111. package/LICENSE +0 -174
  112. package/dist/commands/customCommand.js +0 -23
  113. package/dist/commands/customCommand.js.map +0 -1
  114. package/dist/config/packagesManagement.d.ts +0 -19
  115. package/dist/config/packagesManagement.js +0 -22
  116. package/dist/config/packagesManagement.js.map +0 -1
  117. package/dist/create/fetchTemplate.js.map +0 -1
  118. package/dist/create/page.d.ts +0 -65
  119. package/dist/create/page.js +0 -266
  120. package/dist/create/page.js.map +0 -1
  121. package/dist/create/plugin.d.ts +0 -16
  122. package/dist/create/plugin.js +0 -77
  123. package/dist/create/plugin.js.map +0 -1
  124. package/dist/presets/commands/config.js +0 -121
  125. package/dist/presets/commands/config.js.map +0 -1
  126. package/dist/presets/commands/create.js +0 -112
  127. package/dist/presets/commands/create.js.map +0 -1
  128. package/dist/presets/commands/doctor.js +0 -15
  129. package/dist/presets/commands/doctor.js.map +0 -1
  130. package/dist/presets/commands/global-config.d.ts +0 -3
  131. package/dist/presets/commands/global-config.js +0 -175
  132. package/dist/presets/commands/global-config.js.map +0 -1
  133. package/dist/presets/commands/help.d.ts +0 -3
  134. package/dist/presets/commands/help.js +0 -16
  135. package/dist/presets/commands/help.js.map +0 -1
  136. package/dist/presets/commands/info.d.ts +0 -3
  137. package/dist/presets/commands/info.js +0 -96
  138. package/dist/presets/commands/info.js.map +0 -1
  139. package/dist/presets/commands/inspect.d.ts +0 -3
  140. package/dist/presets/commands/inspect.js +0 -155
  141. package/dist/presets/commands/inspect.js.map +0 -1
  142. package/dist/presets/commands/update.d.ts +0 -3
  143. package/dist/presets/commands/update.js +0 -187
  144. package/dist/presets/commands/update.js.map +0 -1
  145. package/dist/presets/files/generateFrameworkInfo.d.ts +0 -3
  146. package/dist/presets/files/generateFrameworkInfo.js +0 -30
  147. package/dist/presets/files/generateFrameworkInfo.js.map +0 -1
  148. package/dist/presets/files/generateProjectConfig.d.ts +0 -3
  149. package/dist/presets/files/generateProjectConfig.js.map +0 -1
  150. package/dist/presets/files/writeFileToDist.d.ts +0 -3
  151. package/dist/presets/files/writeFileToDist.js.map +0 -1
  152. package/dist/presets/hooks/create.d.ts +0 -3
  153. package/dist/presets/hooks/create.js +0 -42
  154. package/dist/presets/hooks/create.js.map +0 -1
  155. package/dist/presets/platforms/plugin.d.ts +0 -3
  156. package/dist/presets/platforms/plugin.js +0 -106
  157. package/dist/presets/platforms/plugin.js.map +0 -1
  158. package/dist/presets/platforms/rn.d.ts +0 -3
  159. package/dist/presets/platforms/rn.js +0 -130
  160. package/dist/presets/platforms/rn.js.map +0 -1
  161. package/dist/util/appConfig.js.map +0 -1
  162. package/dist/util/createPage.d.ts +0 -9
  163. package/dist/util/createPage.js +0 -114
  164. package/dist/util/createPage.js.map +0 -1
  165. package/src/commands/customCommand.ts +0 -22
  166. package/src/config/packagesManagement.ts +0 -20
  167. package/src/config/rn-stylelint.json +0 -4
  168. package/src/create/page.ts +0 -325
  169. package/src/create/plugin.ts +0 -55
  170. package/src/presets/commands/config.ts +0 -86
  171. package/src/presets/commands/create.ts +0 -104
  172. package/src/presets/commands/doctor.ts +0 -21
  173. package/src/presets/commands/global-config.ts +0 -140
  174. package/src/presets/commands/help.ts +0 -17
  175. package/src/presets/commands/info.ts +0 -81
  176. package/src/presets/commands/inspect.ts +0 -137
  177. package/src/presets/commands/update.ts +0 -175
  178. package/src/presets/hooks/create.ts +0 -9
  179. package/src/presets/platforms/plugin.ts +0 -75
  180. package/src/presets/platforms/rn.ts +0 -125
  181. package/src/util/createPage.ts +0 -139
  182. package/templates/default/babel.config.js +0 -14
  183. /package/dist/create/{fetchTemplate.d.ts → fetch-template.d.ts} +0 -0
  184. /package/dist/presets/{commands/config.d.ts → files/generate-framework-info.d.ts} +0 -0
  185. /package/dist/presets/{commands/create.d.ts → files/generate-project-config.d.ts} +0 -0
  186. /package/dist/presets/{commands/doctor.d.ts → files/write-file-to-dist.d.ts} +0 -0
  187. /package/src/presets/files/{generateProjectConfig.ts → generate-project-config.ts} +0 -0
  188. /package/src/presets/files/{writeFileToDist.ts → write-file-to-dist.ts} +0 -0
@@ -0,0 +1,150 @@
1
+ import { Shortcuts, UnRecursiveTemplate } from '@spcsn/taro-shared/dist/template.js';
2
+
3
+ type WeappTemplateOptions = {
4
+ enablekeyboardAccessory?: boolean;
5
+ };
6
+
7
+ type MiniComponents = Record<string, Record<string, string>>;
8
+
9
+ type ComponentAlias = {
10
+ _num?: string;
11
+ mapkey?: string;
12
+ };
13
+
14
+ type PageTemplateConfig = {
15
+ enablePageMeta?: boolean;
16
+ };
17
+
18
+ type PageTemplateData = {
19
+ content?: PageTemplateConfig;
20
+ };
21
+
22
+ export class Template extends UnRecursiveTemplate {
23
+ pluginOptions: WeappTemplateOptions;
24
+ supportXS = true;
25
+ Adapter = {
26
+ if: 'wx:if',
27
+ else: 'wx:else',
28
+ elseif: 'wx:elif',
29
+ for: 'wx:for',
30
+ forItem: 'wx:for-item',
31
+ forIndex: 'wx:for-index',
32
+ key: 'wx:key',
33
+ xs: 'wxs',
34
+ type: 'weapp',
35
+ };
36
+
37
+ transferComponents: Record<string, Record<string, string>> = {};
38
+
39
+ constructor(pluginOptions?: WeappTemplateOptions) {
40
+ super();
41
+ this.pluginOptions = pluginOptions || {};
42
+ this.nestElements.set('root-portal', 3);
43
+ }
44
+
45
+ buildXsTemplate(filePath = './utils') {
46
+ return `<wxs module="xs" src="${filePath}.wxs" />`;
47
+ }
48
+
49
+ createMiniComponents(components: MiniComponents): MiniComponents {
50
+ const result = super.createMiniComponents(components);
51
+
52
+ // PageMeta & NavigationBar
53
+ this.transferComponents['page-meta'] = result['page-meta'];
54
+ this.transferComponents['navigation-bar'] = result['navigation-bar'];
55
+ delete result['page-meta'];
56
+ delete result['navigation-bar'];
57
+
58
+ return result;
59
+ }
60
+
61
+ replacePropName(name: string, value: string, componentName: string, componentAlias: ComponentAlias) {
62
+ if (value === 'eh') {
63
+ const nameLowerCase = name.toLowerCase();
64
+ if (nameLowerCase === 'bindlongtap' && componentName !== 'canvas') return 'bindlongpress';
65
+ return nameLowerCase;
66
+ }
67
+ if (componentName === 'share-element') {
68
+ const mapKeyAlias = componentAlias.mapkey;
69
+ if (value === `i.${mapKeyAlias}`) return 'key';
70
+ }
71
+ return name;
72
+ }
73
+
74
+ buildXSTepFocus(nn: string) {
75
+ if (this.pluginOptions.enablekeyboardAccessory) {
76
+ const textarea = this.componentsAlias.textarea._num;
77
+ const input = this.componentsAlias.input._num;
78
+ const ka = this.componentsAlias['keyboard-accessory']._num;
79
+ return `function(i, prefix) {
80
+ var s = i.focus !== undefined ? 'focus' : 'blur'
81
+ var r = prefix + i.${nn} + '_' + s
82
+ if ((i.nn === '${textarea}' || i.nn === '${input}') && i.cn[0] && i.cn[0].nn === '${ka}') {
83
+ r = r + '_ka'
84
+ }
85
+ return r
86
+ }`;
87
+ } else {
88
+ return super.buildXSTepFocus(nn);
89
+ }
90
+ }
91
+
92
+ modifyTemplateResult = (res: string, nodeName: string, _level: number, children: string) => {
93
+ if (nodeName === 'keyboard-accessory') return '';
94
+
95
+ if ((nodeName === 'textarea' || nodeName === 'input') && this.pluginOptions.enablekeyboardAccessory) {
96
+ const list = res.split('</template>');
97
+ const componentAlias = this.componentsAlias[nodeName];
98
+ const nodeNameAlias = componentAlias._num;
99
+ const xs = `xs.a(c, item.${Shortcuts.NodeName}, l)`;
100
+
101
+ const target = `
102
+ <keyboard-accessory style="{{i.cn[0].st}}" class="{{i.cn[0].cl}}" bindtap="eh" id="{{i.cn[0].uid||i.cn[0].sid}}" data-sid="{{i.cn[0].sid}}">
103
+ <block wx:for="{{i.cn[0].cn}}" wx:key="sid">
104
+ <template is="{{${xs}}}" data="{{i:item,c:c+1,l:xs.f(l,item.${Shortcuts.NodeName})}}" />
105
+ </block>
106
+ </keyboard-accessory>
107
+ `;
108
+
109
+ const templateFocus = list[1]
110
+ .replace(children, target)
111
+ .replace(`_${nodeNameAlias}_focus`, `_${nodeNameAlias}_focus_ka`);
112
+
113
+ const templateBlur = list[2]
114
+ .replace(children, target)
115
+ .replace(`_${nodeNameAlias}_blur`, `_${nodeNameAlias}_blur_ka`);
116
+
117
+ list.splice(3, 0, templateFocus, templateBlur);
118
+ return list.join('</template>');
119
+ }
120
+
121
+ return res;
122
+ };
123
+
124
+ buildPageTemplate = (baseTempPath: string, page?: PageTemplateData) => {
125
+ let pageMetaTemplate = '';
126
+ const pageConfig = page?.content;
127
+
128
+ if (pageConfig?.enablePageMeta) {
129
+ const getComponentAttrs = (componentName: string, dataPath: string) => {
130
+ return Object.entries(this.transferComponents[componentName]).reduce((sum, [key, value]) => {
131
+ sum += `${key}="${value === 'eh' ? value : `{{${value.replace('i.', dataPath)}}}`}" `;
132
+ return sum;
133
+ }, '');
134
+ };
135
+ const pageMetaAttrs = getComponentAttrs('page-meta', 'pageMeta.');
136
+ const navigationBarAttrs = getComponentAttrs('navigation-bar', 'navigationBar.');
137
+
138
+ pageMetaTemplate = `
139
+ <wxs module="xs" src="${baseTempPath.replace('base.wxml', 'utils.wxs')}" />
140
+ <page-meta data-sid="{{pageMeta.sid}}" ${pageMetaAttrs}>
141
+ <navigation-bar ${navigationBarAttrs}/>
142
+ </page-meta>`;
143
+ }
144
+
145
+ const template = `<import src="${baseTempPath}"/>${pageMetaTemplate}
146
+ <template is="taro_tmpl" data="{{${this.dataKeymap('root:root')}}}" />`;
147
+
148
+ return template;
149
+ };
150
+ }
@@ -1,10 +1,14 @@
1
- import { MessageKind, validateConfig } from '../../doctor/validators';
2
-
3
- import { extractCompileEntry } from '../../util/appConfig';
4
- import * as hooks from '../constant';
1
+ import * as validatorsModule from '../../doctor/validators.js';
2
+ import * as appConfigModule from '../../util/app-config.js';
3
+ import * as hooks from '../constant/hooks.js';
5
4
 
6
5
  import type { IPluginContext } from '@spcsn/taro-service';
7
6
 
7
+ const validators = (validatorsModule as any).default || validatorsModule;
8
+ const appConfig = (appConfigModule as any).default || appConfigModule;
9
+ const { MessageKind, validateConfig } = validators;
10
+ const { extractCompileEntry } = appConfig;
11
+
8
12
  export default (ctx: IPluginContext) => {
9
13
  ctx.registerCommand({
10
14
  name: 'build',
@@ -20,29 +24,26 @@ export default (ctx: IPluginContext) => {
20
24
  '--blended': 'Blended Taro project in an original MiniApp project',
21
25
  '--new-blended':
22
26
  'Blended Taro project in an original MiniApp project while supporting building components independently',
23
- '--plugin [typeName]': 'Build Taro plugin project, weapp',
24
27
  '--env-prefix [envPrefix]': "Provide the dotEnv varables's prefix",
25
28
  '--no-check': 'Do not check config is valid or not',
26
29
  },
27
30
  synopsisList: [
28
- 'taro build --type weapp',
29
- 'taro build --type weapp --watch',
30
- 'taro build --type weapp --watch --pages pages/index/index',
31
- 'taro build --type weapp --env production',
32
- 'taro build --type weapp --blended',
33
- 'taro build --type weapp --no-build',
34
- 'taro build native-components --type weapp',
35
- 'taro build --type weapp --new-blended',
36
- 'taro build --plugin weapp --watch',
37
- 'taro build --plugin weapp',
38
- 'taro build --type weapp --mode prepare --env-prefix TARO_APP_',
31
+ 'taro build',
32
+ 'taro build --watch',
33
+ 'taro build --watch --pages pages/index/index',
34
+ 'taro build --env production',
35
+ 'taro build --blended',
36
+ 'taro build --no-build',
37
+ 'taro build native-components',
38
+ 'taro build --new-blended',
39
+ 'taro build --mode prepare --env-prefix TARO_APP_',
39
40
  ],
40
41
  async fn(opts) {
41
42
  const { options, config, _ } = opts;
42
43
  const { platform, isWatch, blended, newBlended, withoutBuild, noInjectGlobalStyle, noCheck } = options;
43
44
  const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
44
45
  const { outputPath, configPath } = ctx.paths;
45
- const { args } = options;
46
+ const args = options.args || {};
46
47
 
47
48
  if (!configPath || !fs.existsSync(configPath)) {
48
49
  console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
@@ -51,7 +52,7 @@ export default (ctx: IPluginContext) => {
51
52
 
52
53
  if (typeof platform !== 'string') {
53
54
  console.log(chalk.red('请传入正确的编译类型!'));
54
- process.exit(0);
55
+ process.exit(1);
55
56
  }
56
57
 
57
58
  // 校验 Taro 项目配置
@@ -87,7 +88,7 @@ export default (ctx: IPluginContext) => {
87
88
  }
88
89
  });
89
90
  console.log('');
90
- process.exit(0);
91
+ process.exit(1);
91
92
  }
92
93
  }
93
94
 
@@ -122,17 +123,6 @@ export default (ctx: IPluginContext) => {
122
123
  },
123
124
  });
124
125
  },
125
- async modifyWebpackChain(chain, webpack, data) {
126
- await ctx.applyPlugins({
127
- name: hooks.MODIFY_WEBPACK_CHAIN,
128
- initialVal: chain,
129
- opts: {
130
- chain,
131
- webpack,
132
- data,
133
- },
134
- });
135
- },
136
126
  async modifyViteConfig(viteConfig, data, viteCompilerContext) {
137
127
  await ctx.applyPlugins({
138
128
  name: hooks.MODIFY_VITE_CONFIG,
@@ -172,16 +162,6 @@ export default (ctx: IPluginContext) => {
172
162
  },
173
163
  });
174
164
  },
175
- async onCompilerMake(compilation, compiler, plugin) {
176
- await ctx.applyPlugins({
177
- name: hooks.ON_COMPILER_MAKE,
178
- opts: {
179
- compilation,
180
- compiler,
181
- plugin,
182
- },
183
- });
184
- },
185
165
  async onParseCreateElement(nodeName, componentConfig) {
186
166
  await ctx.applyPlugins({
187
167
  name: hooks.ON_PARSE_CREATE_ELEMENT,
@@ -7,12 +7,11 @@ export default (ctx: IPluginContext) => {
7
7
  '--name [name]': '项目名称',
8
8
  '--description [description]': '项目介绍',
9
9
  '--typescript': '使用TypeScript',
10
- '--build-es5': '是否需要编译为es5',
11
10
  '--npm [npm]': '包管理工具',
12
11
  '--template-source [templateSource]': '项目模板源',
13
12
  '--clone [clone]': '拉取远程模板时使用git clone',
14
13
  '--template [template]': '项目模板',
15
- '--css [css]': 'CSS预处理器(sass/less/stylus/none)',
14
+ '--css [css]': 'CSS 预处理器选项已收敛,仅支持 none',
16
15
  '--autoInstall': '自动安装依赖',
17
16
  '-h, --help': 'output usage information',
18
17
  },
@@ -26,7 +25,6 @@ export default (ctx: IPluginContext) => {
26
25
  template,
27
26
  description,
28
27
  typescript,
29
- buildEs5,
30
28
  css,
31
29
  npm,
32
30
  framework,
@@ -48,7 +46,6 @@ export default (ctx: IPluginContext) => {
48
46
  template,
49
47
  description,
50
48
  typescript,
51
- buildEs5,
52
49
  framework,
53
50
  compiler,
54
51
  hideDefaultTemplate,
@@ -0,0 +1,12 @@
1
+ exports.MODIFY_APP_CONFIG = 'modifyAppConfig';
2
+ exports.MODIFY_VITE_CONFIG = 'modifyViteConfig';
3
+ exports.MODIFY_BUILD_ASSETS = 'modifyBuildAssets';
4
+ exports.MODIFY_MINI_CONFIGS = 'modifyMiniConfigs';
5
+ exports.MODIFY_COMPONENT_CONFIG = 'modifyComponentConfig';
6
+ exports.MODIFY_RUNNER_OPTS = 'modifyRunnerOpts';
7
+ exports.ON_PARSE_CREATE_ELEMENT = 'onParseCreateElement';
8
+ exports.ON_BUILD_START = 'onBuildStart';
9
+ exports.ON_BUILD_FINISH = 'onBuildFinish';
10
+ exports.ON_BUILD_COMPLETE = 'onBuildComplete';
11
+
12
+ exports.MODIFY_CREATE_TEMPLATE = 'modifyCreateTemplate';
@@ -1,11 +1,9 @@
1
1
  export const MODIFY_APP_CONFIG = 'modifyAppConfig';
2
- export const MODIFY_WEBPACK_CHAIN = 'modifyWebpackChain';
3
2
  export const MODIFY_VITE_CONFIG = 'modifyViteConfig';
4
3
  export const MODIFY_BUILD_ASSETS = 'modifyBuildAssets';
5
4
  export const MODIFY_MINI_CONFIGS = 'modifyMiniConfigs';
6
5
  export const MODIFY_COMPONENT_CONFIG = 'modifyComponentConfig';
7
6
  export const MODIFY_RUNNER_OPTS = 'modifyRunnerOpts';
8
- export const ON_COMPILER_MAKE = 'onCompilerMake';
9
7
  export const ON_PARSE_CREATE_ELEMENT = 'onParseCreateElement';
10
8
  export const ON_BUILD_START = 'onBuildStart';
11
9
  export const ON_BUILD_FINISH = 'onBuildFinish';
@@ -1,7 +1,13 @@
1
- import { getPkgVersion } from '../../util';
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
2
3
 
3
4
  import type { IPluginContext } from '@spcsn/taro-service';
4
5
 
6
+ function getPkgVersion(): string {
7
+ const packageJsonPath = path.resolve(__dirname, '../../../package.json');
8
+ return JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).version;
9
+ }
10
+
5
11
  export default (ctx: IPluginContext) => {
6
12
  ctx.registerMethod('generateFrameworkInfo', () => {
7
13
  const { getInstalledNpmPkgVersion, processTypeEnum, printLog, chalk } = ctx.helper;
@@ -1,16 +1,14 @@
1
- import * as hooks from '../constant';
1
+ import * as hooks from '../constant/hooks.js';
2
2
 
3
3
  import type { IPluginContext } from '@spcsn/taro-service';
4
4
 
5
5
  export default (ctx: IPluginContext) => {
6
6
  [
7
7
  hooks.MODIFY_APP_CONFIG,
8
- hooks.MODIFY_WEBPACK_CHAIN,
9
8
  hooks.MODIFY_VITE_CONFIG,
10
9
  hooks.MODIFY_BUILD_ASSETS,
11
10
  hooks.MODIFY_MINI_CONFIGS,
12
11
  hooks.MODIFY_COMPONENT_CONFIG,
13
- hooks.ON_COMPILER_MAKE,
14
12
  hooks.ON_PARSE_CREATE_ELEMENT,
15
13
  hooks.ON_BUILD_START,
16
14
  hooks.ON_BUILD_FINISH,
@@ -5,11 +5,9 @@ export default () => {
5
5
  plugins: [
6
6
  // hooks
7
7
  path.resolve(__dirname, 'hooks', 'build.js'),
8
- path.resolve(__dirname, 'hooks', 'create.js'),
9
- // 兼容其他平台小程序插件
10
- path.resolve(__dirname, 'files', 'writeFileToDist.js'),
11
- path.resolve(__dirname, 'files', 'generateProjectConfig.js'),
12
- path.resolve(__dirname, 'files', 'generateFrameworkInfo.js'),
8
+ path.resolve(__dirname, 'files', 'write-file-to-dist.js'),
9
+ path.resolve(__dirname, 'files', 'generate-project-config.js'),
10
+ path.resolve(__dirname, 'files', 'generate-framework-info.js'),
13
11
  ],
14
12
  };
15
13
  };
@@ -0,0 +1 @@
1
+ module.exports = require('./app-config.ts');
@@ -1,6 +1,6 @@
1
- import { IPluginContext } from '@spcsn/taro-service';
1
+ import type { IPluginContext } from '@spcsn/taro-service';
2
2
  import { isString } from '@spcsn/taro-shared';
3
- import { AppConfig } from '@spcsn/taro';
3
+ import type { AppConfig } from '@spcsn/taro';
4
4
 
5
5
  /**
6
6
  * 按需编译功能,只编译指定的页面或组件
@@ -14,10 +14,11 @@ export function extractCompileEntry(
14
14
  ): void {
15
15
  const { chalk } = ctx.helper;
16
16
 
17
- const extractType = isString(args.pages) ? 'pages' : isString(args.components) ? 'components' : '';
17
+ const extractArgs = args || {};
18
+ const extractType = isString(extractArgs.pages) ? 'pages' : isString(extractArgs.components) ? 'components' : '';
18
19
  if (!extractType) return;
19
20
 
20
- const entries = args[extractType]
21
+ const entries = extractArgs[extractType]
21
22
  .split(',')
22
23
  .map((item) => item.trim())
23
24
  .filter(Boolean);
@@ -1,7 +1,7 @@
1
1
  import type { IProjectConfig } from '@spcsn/taro/types/compile';
2
- import type { CompilerTypes, CompilerWebpackTypes } from '@spcsn/taro/types/compile/compiler';
2
+ import type { CompilerTypes, CompilerViteTypes } from '@spcsn/taro/types/compile/compiler';
3
3
 
4
- type WebpackMerge = (...configs: Array<object | null | undefined>) => object;
4
+ type ConfigMerge = (...configs: Array<object | null | undefined>) => object;
5
5
 
6
6
  export interface ConfigEnv {
7
7
  /** taro 当前执行的命令 */
@@ -10,11 +10,11 @@ export interface ConfigEnv {
10
10
  mode: string;
11
11
  }
12
12
 
13
- export type UserConfigFn<T extends CompilerTypes = CompilerWebpackTypes> = (
14
- merge: WebpackMerge,
13
+ export type UserConfigFn<T extends CompilerTypes = CompilerViteTypes> = (
14
+ merge: ConfigMerge,
15
15
  env: ConfigEnv,
16
16
  ) => IProjectConfig<T> | Promise<IProjectConfig<T>>;
17
- export type UserConfigExport<T extends CompilerTypes = CompilerWebpackTypes> =
17
+ export type UserConfigExport<T extends CompilerTypes = CompilerViteTypes> =
18
18
  | IProjectConfig<T>
19
19
  | Promise<IProjectConfig<T>>
20
20
  | UserConfigFn;
@@ -23,6 +23,6 @@ export type UserConfigExport<T extends CompilerTypes = CompilerWebpackTypes> =
23
23
  * @since v3.6.9
24
24
  * @warning 暂不支持 react native
25
25
  */
26
- export function defineConfig<T extends CompilerTypes = CompilerWebpackTypes>(config: UserConfigExport<T>) {
26
+ export function defineConfig<T extends CompilerTypes = CompilerViteTypes>(config: UserConfigExport<T>) {
27
27
  return config;
28
28
  }
package/src/util/index.ts CHANGED
@@ -1,18 +1,80 @@
1
+ import { exec } from 'node:child_process';
1
2
  import * as path from 'node:path';
3
+ import * as readline from 'node:readline';
2
4
 
3
- import { chalk, fs, isWindows } from '@spcsn/taro-helper';
4
- import { exec } from 'child_process';
5
+ import { fs } from '@spcsn/taro-helper';
6
+
7
+ type ProfileEntry = {
8
+ name: string;
9
+ durationMs: number;
10
+ };
11
+
12
+ function isProfileEnabled(): boolean {
13
+ const rawValue = process.env.TARO_PROFILE;
14
+ if (!rawValue) return false;
15
+
16
+ return !['0', 'false', 'off', 'no'].includes(rawValue.toLowerCase());
17
+ }
18
+
19
+ function nowMs(): number {
20
+ return Number(process.hrtime.bigint()) / 1_000_000;
21
+ }
22
+
23
+ function formatDuration(durationMs: number): string {
24
+ return `${durationMs.toFixed(1)}ms`;
25
+ }
26
+
27
+ class CliProfiler {
28
+ private readonly enabled = isProfileEnabled();
29
+ private entries: ProfileEntry[] = [];
30
+
31
+ start(): number {
32
+ return this.enabled ? nowMs() : 0;
33
+ }
34
+
35
+ end(name: string, startMs: number): void {
36
+ if (!this.enabled) return;
37
+
38
+ this.entries.push({
39
+ name,
40
+ durationMs: nowMs() - startMs,
41
+ });
42
+ }
43
+
44
+ async measure<T>(name: string, task: () => Promise<T>): Promise<T> {
45
+ const startMs = this.start();
46
+ try {
47
+ return await task();
48
+ } finally {
49
+ this.end(name, startMs);
50
+ }
51
+ }
52
+
53
+ print(): void {
54
+ if (!this.enabled || this.entries.length === 0) return;
55
+
56
+ const nameWidth = Math.max(...this.entries.map((entry) => entry.name.length));
57
+ const lines = this.entries.map((entry) => {
58
+ return ` ${entry.name.padEnd(nameWidth)} ${formatDuration(entry.durationMs)}`;
59
+ });
60
+
61
+ console.info(['[taro:profile] cli timings', ...lines].join('\n'));
62
+ this.entries = [];
63
+ }
64
+ }
65
+
66
+ export const cliProfiler = new CliProfiler();
5
67
 
6
68
  export function getRootPath(): string {
7
69
  return path.resolve(__dirname, '../../');
8
70
  }
9
71
 
10
72
  export function getPkgVersion(): string {
11
- return require(path.join(getRootPath(), 'package.json')).version;
73
+ return fs.readJSONSync(path.join(getRootPath(), 'package.json')).version;
12
74
  }
13
75
 
14
76
  export function getPkgItemByKey(key: string) {
15
- const packageMap = require(path.join(getRootPath(), 'package.json'));
77
+ const packageMap = fs.readJSONSync(path.join(getRootPath(), 'package.json'));
16
78
  if (Object.keys(packageMap).indexOf(key) === -1) {
17
79
  return {};
18
80
  } else {
@@ -22,10 +84,9 @@ export function getPkgItemByKey(key: string) {
22
84
 
23
85
  export function printPkgVersion() {
24
86
  const taroVersion = getPkgVersion();
25
- console.log(`👽 Taro v${taroVersion}`);
87
+ console.log(`👽 SPCSN Taro v${taroVersion}`);
26
88
  console.log();
27
89
  }
28
-
29
90
  export const getAllFilesInFolder = async (folder: string, filter: string[] = []): Promise<string[]> => {
30
91
  let files: string[] = [];
31
92
  const list = readDirWithFileTypes(folder);
@@ -73,26 +134,7 @@ export function readDirWithFileTypes(folder: string): FileStat[] {
73
134
  });
74
135
  return res;
75
136
  }
76
-
77
- export function printDevelopmentTip(platform: string) {
78
- if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
79
- let exampleCommand;
80
- if (isWindows) {
81
- exampleCommand = `$ set NODE_ENV=production && taro build --type ${platform} --watch`;
82
- } else {
83
- exampleCommand = `$ NODE_ENV=production taro build --type ${platform} --watch`;
84
- }
85
- console.log(
86
- chalk.yellowBright(`Tips: 预览模式生成的文件较大,设置 NODE_ENV 为 production 可以开启压缩。
87
- Example:
88
- ${exampleCommand}
89
- `),
90
- );
91
- }
92
- }
93
-
94
137
  export function clearConsole() {
95
- const readline = require('readline');
96
138
  if (process.stdout.isTTY) {
97
139
  const blank = '\n'.repeat(process.stdout.rows);
98
140
  console.log(blank);
@@ -1,4 +1,4 @@
1
- {{#if typescript }}import type { UserConfigExport } from "@spcsn/taro-cli"{{/if}}
1
+ {{#if typescript }}import type { UserConfigExport } from "@spcsn/taro-cli/define-config"{{/if}}
2
2
 
3
3
  export default {
4
4
  mini: {}
@@ -1,61 +1,49 @@
1
- import { defineConfig{{#if typescript }}, type UserConfigExport{{/if}} } from '@spcsn/taro-cli'
2
- import devConfig from './dev'
3
- import prodConfig from './prod'
1
+ import { defineConfig } from "@spcsn/taro-cli/define-config";
2
+ import devConfig from "./dev";
3
+ import prodConfig from "./prod";
4
4
 
5
5
  // https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
6
- export default defineConfig{{#if typescript }}<'vite'>{{/if}}(async (merge, { command, mode }) => {
7
- const baseConfig{{#if typescript }}: UserConfigExport<'vite'>{{/if}} = {
8
- projectName: '{{ projectName }}',
9
- date: '{{ date }}',
6
+ export default defineConfig(async (merge, { command, mode }) => {
7
+ const baseConfig = {
8
+ projectName: "{{ projectName }}",
9
+ date: "{{ date }}",
10
10
  designWidth: 750,
11
11
  deviceRatio: {
12
12
  640: 2.34 / 2,
13
13
  750: 1,
14
14
  375: 2,
15
- 828: 1.81 / 2
16
- },
17
- sourceRoot: 'src',
18
- outputRoot: 'dist',
19
- plugins: [
20
- "@spcsn/taro-plugin-generator"
21
- ],
22
- defineConstants: {
15
+ 828: 1.81 / 2,
23
16
  },
17
+ sourceRoot: "src",
18
+ outputRoot: "dist",
19
+ plugins: [],
20
+ defineConstants: {},
24
21
  copy: {
25
- patterns: [
26
- ],
27
- options: {
28
- }
22
+ patterns: [],
23
+ options: {},
29
24
  },
30
- framework: 'react',
31
- compiler: 'vite',
25
+ framework: "react",
26
+ compiler: "vite",
32
27
  mini: {
33
28
  postcss: {
34
29
  pxtransform: {
35
30
  enable: true,
36
- config: {
37
-
38
- }
31
+ config: {},
39
32
  },
40
33
  cssModules: {
41
34
  enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
42
35
  config: {
43
- namingPattern: 'module', // 转换模式,取值为 global/module
44
- generateScopedName: '[name]__[local]___[hash:base64:5]'
45
- }
46
- }
47
- }
48
- }
49
- }
50
-
51
- {{#if buildEs5 }}
52
- process.env.BROWSERSLIST_ENV = process.env.NODE_ENV
53
- {{/if}}
54
-
55
- if (process.env.NODE_ENV === 'development') {
36
+ namingPattern: "module", // 转换模式,取值为 global/module
37
+ generateScopedName: "[name]__[local]___[hash:base64:5]",
38
+ },
39
+ },
40
+ },
41
+ },
42
+ };
43
+ if (process.env.NODE_ENV === "development") {
56
44
  // 本地开发构建配置(不混淆压缩)
57
- return merge({}, baseConfig, devConfig)
45
+ return merge({}, baseConfig, devConfig);
58
46
  }
59
47
  // 生产构建配置(默认开启压缩混淆等)
60
- return merge({}, baseConfig, prodConfig)
61
- })
48
+ return merge({}, baseConfig, prodConfig);
49
+ });
@@ -1,4 +1,4 @@
1
- {{#if typescript }}import type { UserConfigExport } from "@spcsn/taro-cli"{{/if}}
1
+ {{#if typescript }}import type { UserConfigExport } from "@spcsn/taro-cli/define-config"{{/if}}
2
2
 
3
3
  export default {
4
4
  mini: {}