@spcsn/taro-cli 0.1.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 (213) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -0
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +266 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/commands/customCommand.d.ts +5 -0
  8. package/dist/commands/customCommand.js +23 -0
  9. package/dist/commands/customCommand.js.map +1 -0
  10. package/dist/config/index.d.ts +8 -0
  11. package/dist/config/index.js +10 -0
  12. package/dist/config/index.js.map +1 -0
  13. package/dist/config/packagesManagement.d.ts +19 -0
  14. package/dist/config/packagesManagement.js +22 -0
  15. package/dist/config/packagesManagement.js.map +1 -0
  16. package/dist/create/constants.d.ts +1 -0
  17. package/dist/create/constants.js +5 -0
  18. package/dist/create/constants.js.map +1 -0
  19. package/dist/create/creator.d.ts +9 -0
  20. package/dist/create/creator.js +64 -0
  21. package/dist/create/creator.js.map +1 -0
  22. package/dist/create/fetchTemplate.d.ts +8 -0
  23. package/dist/create/fetchTemplate.js +169 -0
  24. package/dist/create/fetchTemplate.js.map +1 -0
  25. package/dist/create/page.d.ts +65 -0
  26. package/dist/create/page.js +266 -0
  27. package/dist/create/page.js.map +1 -0
  28. package/dist/create/plugin.d.ts +16 -0
  29. package/dist/create/plugin.js +77 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +70 -0
  32. package/dist/create/project.js +481 -0
  33. package/dist/create/project.js.map +1 -0
  34. package/dist/doctor/index.d.ts +4 -0
  35. package/dist/doctor/index.js +23 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +24 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/presets/commands/build.d.ts +3 -0
  41. package/dist/presets/commands/build.js +236 -0
  42. package/dist/presets/commands/build.js.map +1 -0
  43. package/dist/presets/commands/config.d.ts +3 -0
  44. package/dist/presets/commands/config.js +121 -0
  45. package/dist/presets/commands/config.js.map +1 -0
  46. package/dist/presets/commands/create.d.ts +3 -0
  47. package/dist/presets/commands/create.js +112 -0
  48. package/dist/presets/commands/create.js.map +1 -0
  49. package/dist/presets/commands/global-config.d.ts +3 -0
  50. package/dist/presets/commands/global-config.js +175 -0
  51. package/dist/presets/commands/global-config.js.map +1 -0
  52. package/dist/presets/commands/help.d.ts +3 -0
  53. package/dist/presets/commands/help.js +16 -0
  54. package/dist/presets/commands/help.js.map +1 -0
  55. package/dist/presets/commands/info.d.ts +3 -0
  56. package/dist/presets/commands/info.js +96 -0
  57. package/dist/presets/commands/info.js.map +1 -0
  58. package/dist/presets/commands/init.d.ts +3 -0
  59. package/dist/presets/commands/init.js +46 -0
  60. package/dist/presets/commands/init.js.map +1 -0
  61. package/dist/presets/commands/inspect.d.ts +3 -0
  62. package/dist/presets/commands/inspect.js +155 -0
  63. package/dist/presets/commands/inspect.js.map +1 -0
  64. package/dist/presets/commands/update.d.ts +3 -0
  65. package/dist/presets/commands/update.js +187 -0
  66. package/dist/presets/commands/update.js.map +1 -0
  67. package/dist/presets/constant/hooks.d.ts +13 -0
  68. package/dist/presets/constant/hooks.js +17 -0
  69. package/dist/presets/constant/hooks.js.map +1 -0
  70. package/dist/presets/constant/index.d.ts +1 -0
  71. package/dist/presets/constant/index.js +18 -0
  72. package/dist/presets/constant/index.js.map +1 -0
  73. package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
  74. package/dist/presets/files/generateFrameworkInfo.js +30 -0
  75. package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
  76. package/dist/presets/files/generateProjectConfig.d.ts +3 -0
  77. package/dist/presets/files/generateProjectConfig.js +72 -0
  78. package/dist/presets/files/generateProjectConfig.js.map +1 -0
  79. package/dist/presets/files/writeFileToDist.d.ts +3 -0
  80. package/dist/presets/files/writeFileToDist.js +50 -0
  81. package/dist/presets/files/writeFileToDist.js.map +1 -0
  82. package/dist/presets/hooks/build.d.ts +3 -0
  83. package/dist/presets/hooks/build.js +55 -0
  84. package/dist/presets/hooks/build.js.map +1 -0
  85. package/dist/presets/hooks/create.d.ts +3 -0
  86. package/dist/presets/hooks/create.js +42 -0
  87. package/dist/presets/hooks/create.js.map +1 -0
  88. package/dist/presets/index.d.ts +4 -0
  89. package/dist/presets/index.js +50 -0
  90. package/dist/presets/index.js.map +1 -0
  91. package/dist/presets/platforms/plugin.d.ts +3 -0
  92. package/dist/presets/platforms/plugin.js +106 -0
  93. package/dist/presets/platforms/plugin.js.map +1 -0
  94. package/dist/presets/platforms/rn.d.ts +3 -0
  95. package/dist/presets/platforms/rn.js +130 -0
  96. package/dist/presets/platforms/rn.js.map +1 -0
  97. package/dist/util/appConfig.d.ts +11 -0
  98. package/dist/util/appConfig.js +27 -0
  99. package/dist/util/appConfig.js.map +1 -0
  100. package/dist/util/createPage.d.ts +9 -0
  101. package/dist/util/createPage.js +114 -0
  102. package/dist/util/createPage.js.map +1 -0
  103. package/dist/util/defineConfig.d.ts +17 -0
  104. package/dist/util/defineConfig.js +11 -0
  105. package/dist/util/defineConfig.js.map +1 -0
  106. package/dist/util/index.d.ts +23 -0
  107. package/dist/util/index.js +149 -0
  108. package/dist/util/index.js.map +1 -0
  109. package/dist/util/types.d.ts +58 -0
  110. package/dist/util/types.js +3 -0
  111. package/dist/util/types.js.map +1 -0
  112. package/global.d.ts +4 -0
  113. package/index.js +3 -0
  114. package/package.json +74 -0
  115. package/postinstall.js +17 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +66 -0
  118. package/src/__tests__/cli.spec.ts +226 -0
  119. package/src/__tests__/config.spec.ts +224 -0
  120. package/src/__tests__/doctor-config.spec.ts +943 -0
  121. package/src/__tests__/doctor-recommand.spec.ts +136 -0
  122. package/src/__tests__/doctor.spec.ts +94 -0
  123. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  124. package/src/__tests__/env/.env +2 -0
  125. package/src/__tests__/fixtures/default/.env +3 -0
  126. package/src/__tests__/fixtures/default/.env.development +1 -0
  127. package/src/__tests__/fixtures/default/.env.local +1 -0
  128. package/src/__tests__/fixtures/default/.env.pre +3 -0
  129. package/src/__tests__/fixtures/default/.env.production +1 -0
  130. package/src/__tests__/fixtures/default/.env.uat +5 -0
  131. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  132. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  133. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  134. package/src/__tests__/fixtures/default/config/index.js +84 -0
  135. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  136. package/src/__tests__/fixtures/default/package.json +27 -0
  137. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  138. package/src/__tests__/fixtures/default/src/app.js +16 -0
  139. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  140. package/src/__tests__/fixtures/default/src/index.html +19 -0
  141. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  144. package/src/__tests__/info.spec.ts +72 -0
  145. package/src/__tests__/inspect.spec.ts +160 -0
  146. package/src/__tests__/update.spec.ts +310 -0
  147. package/src/__tests__/utils/index.ts +54 -0
  148. package/src/cli.ts +240 -0
  149. package/src/commands/customCommand.ts +22 -0
  150. package/src/config/index.ts +7 -0
  151. package/src/config/manifest.default.json +33 -0
  152. package/src/config/packagesManagement.ts +20 -0
  153. package/src/config/rn-stylelint.json +4 -0
  154. package/src/config/tsconfig.json +22 -0
  155. package/src/create/constants.ts +1 -0
  156. package/src/create/creator.ts +37 -0
  157. package/src/create/fetchTemplate.ts +161 -0
  158. package/src/create/page.ts +325 -0
  159. package/src/create/plugin.ts +55 -0
  160. package/src/create/project.ts +539 -0
  161. package/src/doctor/index.ts +21 -0
  162. package/src/index.ts +24 -0
  163. package/src/presets/commands/build.ts +215 -0
  164. package/src/presets/commands/config.ts +86 -0
  165. package/src/presets/commands/create.ts +104 -0
  166. package/src/presets/commands/global-config.ts +140 -0
  167. package/src/presets/commands/help.ts +17 -0
  168. package/src/presets/commands/info.ts +81 -0
  169. package/src/presets/commands/init.ts +63 -0
  170. package/src/presets/commands/inspect.ts +137 -0
  171. package/src/presets/commands/update.ts +175 -0
  172. package/src/presets/constant/hooks.ts +14 -0
  173. package/src/presets/constant/index.ts +1 -0
  174. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  175. package/src/presets/files/generateProjectConfig.ts +41 -0
  176. package/src/presets/files/writeFileToDist.ts +17 -0
  177. package/src/presets/hooks/build.ts +22 -0
  178. package/src/presets/hooks/create.ts +9 -0
  179. package/src/presets/index.ts +15 -0
  180. package/src/presets/platforms/plugin.ts +75 -0
  181. package/src/presets/platforms/rn.ts +125 -0
  182. package/src/util/appConfig.ts +32 -0
  183. package/src/util/createPage.ts +139 -0
  184. package/src/util/defineConfig.ts +28 -0
  185. package/src/util/index.ts +126 -0
  186. package/src/util/types.ts +81 -0
  187. package/templates/default/_editorconfig +12 -0
  188. package/templates/default/_env.development +2 -0
  189. package/templates/default/_env.production +1 -0
  190. package/templates/default/_env.test +1 -0
  191. package/templates/default/_gitignore +8 -0
  192. package/templates/default/babel.config.js +14 -0
  193. package/templates/default/config/dev.js +5 -0
  194. package/templates/default/config/index.js +61 -0
  195. package/templates/default/config/prod.js +5 -0
  196. package/templates/default/package.json.tmpl +52 -0
  197. package/templates/default/project.config.json +15 -0
  198. package/templates/default/src/app.config.js +11 -0
  199. package/templates/default/src/app.css +0 -0
  200. package/templates/default/src/app.js +15 -0
  201. package/templates/default/src/pages/index/index.config.js +3 -0
  202. package/templates/default/src/pages/index/index.css +0 -0
  203. package/templates/default/src/pages/index/index.jsx +15 -0
  204. package/templates/default/template_creator.js +61 -0
  205. package/templates/default/tsconfig.json +30 -0
  206. package/templates/default/types/global.d.ts +27 -0
  207. package/templates/global-config/index.json +4 -0
  208. package/templates/global-config/package.json +5 -0
  209. package/templates/plugin-compile/README.md +32 -0
  210. package/templates/plugin-compile/index.js +3 -0
  211. package/templates/plugin-compile/package.json.tmpl +43 -0
  212. package/templates/plugin-compile/src/index.ts +153 -0
  213. package/templates/plugin-compile/tsconfig.json +28 -0
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "{{ projectName }}",
3
+ "version": "1.0.0",
4
+ "private": true,
5
+ "description": "{{ description }}",
6
+ "templateInfo": {
7
+ "name": "{{ template }}",
8
+ "typescript": {{ typescript }},
9
+ "css": "{{ css }}",
10
+ "framework": "{{ framework }}"
11
+ },
12
+ "scripts": {
13
+ "new": "taro new",
14
+ "build:weapp": "taro build --type weapp",
15
+ "dev:weapp": "npm run build:weapp -- --watch"
16
+ },
17
+ {{#if buildEs5 }}
18
+ "browserslist": {
19
+ "development": [
20
+ "defaults and fully supports es6-module",
21
+ "maintained node versions"
22
+ ],
23
+ "production": [
24
+ "last 3 versions",
25
+ "Android >= 4.1",
26
+ "ios >= 8"
27
+ ]
28
+ },
29
+ {{else}}
30
+ "browserslist": [
31
+ "defaults and fully supports es6-module",
32
+ "maintained node versions"
33
+ ],
34
+ {{/if}}
35
+ "author": "",
36
+ "dependencies": {
37
+ "@spcsn/taro-components": "{{ version }}",
38
+ "@spcsn/taro": "{{ version }}",
39
+ "react-dom": "^19.2.0",
40
+ "react": "^19.2.0"
41
+ },
42
+ "devDependencies": {
43
+ "@spcsn/taro-cli": "{{ version }}",
44
+ "@types/react": "^19.2.14",
45
+ "@types/react-dom": "^19.2.3",{{#if (eq css "Sass") }}
46
+ "sass": "^1.75.0",{{/if}}{{#if (eq css "Less") }}
47
+ "less": "^4.2.0",{{/if}}{{#if (eq css "Stylus") }}
48
+ "stylus": "^0.63.0",{{/if}}{{#if typescript }}
49
+ "typescript": "^5.9.3",{{/if}}
50
+ "@types/minimatch": "^5"
51
+ }
52
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "miniprogramRoot": "./dist",
3
+ "projectname": "{{ projectName }}",
4
+ "description": "{{ description }}",
5
+ "appid": "touristappid",
6
+ "setting": {
7
+ "urlCheck": true,
8
+ "es6": false,
9
+ "enhance": false,
10
+ "compileHotReLoad": false,
11
+ "postcss": false,
12
+ "minified": false
13
+ },
14
+ "compileType": "miniprogram"
15
+ }
@@ -0,0 +1,11 @@
1
+ export default defineAppConfig({
2
+ pages: [
3
+ 'pages/index/index'
4
+ ],
5
+ window: {
6
+ backgroundTextStyle: 'light',
7
+ navigationBarBackgroundColor: '#fff',
8
+ navigationBarTitleText: 'WeChat',
9
+ navigationBarTextStyle: 'black'
10
+ }
11
+ })
File without changes
@@ -0,0 +1,15 @@
1
+ {{#if typescript }}import { PropsWithChildren } from 'react'{{/if}}
2
+ import { useLaunch } from '@spcsn/taro'
3
+
4
+ import './app.{{ cssExt }}'
5
+
6
+ function App({ children }{{#if typescript }}: PropsWithChildren<any>{{/if}}) {
7
+ useLaunch(() => {
8
+ console.log('App launched.')
9
+ })
10
+
11
+ // children 是将要会渲染的页面
12
+ return children
13
+ }
14
+
15
+ export default App
@@ -0,0 +1,3 @@
1
+ export default definePageConfig({
2
+ navigationBarTitleText: '首页'
3
+ })
File without changes
@@ -0,0 +1,15 @@
1
+ import { View, Text } from '@spcsn/taro-components'
2
+ import { useLoad } from '@spcsn/taro'
3
+ import './index.{{ cssExt }}'
4
+
5
+ export default function {{ to_pascal_case pageName }} () {
6
+ useLoad(() => {
7
+ console.log('Page loaded.')
8
+ })
9
+
10
+ return (
11
+ <View className='{{ pageName }}'>
12
+ <Text>Hello world!</Text>
13
+ </View>
14
+ )
15
+ }
@@ -0,0 +1,61 @@
1
+ const path = require('path')
2
+
3
+ function createWhenTs (err, params) {
4
+ return !!params.typescript
5
+ }
6
+
7
+ function normalizePath (path) {
8
+ return path.replace(/\\/g, '/').replace(/\/{2,}/g, '/')
9
+ }
10
+
11
+ const SOURCE_ENTRY = '/src'
12
+ const PAGES_ENTRY = '/src/pages'
13
+
14
+ const handler = {
15
+ '/tsconfig.json': createWhenTs,
16
+ '/types/global.d.ts': createWhenTs,
17
+ '/src/pages/index/index.jsx' (err, { pageDir = '', pageName = '', subPkg = '' }) {
18
+ return {
19
+ setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.jsx')),
20
+ setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.jsx'))
21
+ }
22
+ },
23
+ '/src/pages/index/index.css' (err, { pageDir = '', pageName = '', subPkg = '' }) {
24
+ return {
25
+ setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.css')),
26
+ setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.css'))
27
+ }
28
+ },
29
+ '/src/pages/index/index.config.js' (err, { pageDir = '', pageName = '', subPkg = '' }) {
30
+ return {
31
+ setPageName: normalizePath(path.join(PAGES_ENTRY, pageDir, pageName, 'index.config.js')),
32
+ setSubPkgName: normalizePath(path.join(SOURCE_ENTRY, subPkg, pageDir, pageName, 'index.config.js'))
33
+ }
34
+ },
35
+ '/_editorconfig' () {
36
+ return { setPageName: `/.editorconfig` }
37
+ },
38
+ '/_env.development' () {
39
+ return { setPageName: `/.env.development` }
40
+ },
41
+ '/_env.production' () {
42
+ return { setPageName: `/.env.production` }
43
+ },
44
+ '/_env.test' () {
45
+ return { setPageName: `/.env.test` }
46
+ },
47
+ '/_gitignore' () {
48
+ return { setPageName: `/.gitignore` }
49
+ }
50
+ }
51
+
52
+ const basePageFiles = [
53
+ '/src/pages/index/index.jsx',
54
+ '/src/pages/index/index.css',
55
+ '/src/pages/index/index.config.js'
56
+ ]
57
+
58
+ module.exports = {
59
+ handler,
60
+ basePageFiles
61
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2017",
4
+ "module": "commonjs",
5
+ "removeComments": false,
6
+ "preserveConstEnums": true,
7
+ "moduleResolution": "bundler",
8
+ "experimentalDecorators": true,
9
+ "noImplicitAny": false,
10
+ "allowSyntheticDefaultImports": true,
11
+ "outDir": "lib",
12
+ "noUnusedLocals": true,
13
+ "noUnusedParameters": true,
14
+ "strictNullChecks": true,
15
+ "sourceMap": true,
16
+ "rootDir": ".",
17
+ "jsx": "react-jsx",
18
+ "allowJs": true,
19
+ "resolveJsonModule": true,
20
+ "typeRoots": [
21
+ "node_modules/@types"
22
+ ],
23
+ "paths": {
24
+ // TS5090 leading './'
25
+ "@/*": ["./src/*"]
26
+ }
27
+ },
28
+ "include": ["./src", "./types", "./config"],
29
+ "compileOnSave": false
30
+ }
@@ -0,0 +1,27 @@
1
+ /// <reference types="@spcsn/taro" />
2
+
3
+ declare module '*.png';
4
+ declare module '*.gif';
5
+ declare module '*.jpg';
6
+ declare module '*.jpeg';
7
+ declare module '*.svg';
8
+ declare module '*.css';
9
+ declare module '*.less';
10
+ declare module '*.scss';
11
+ declare module '*.sass';
12
+ declare module '*.styl';
13
+
14
+ declare namespace NodeJS {
15
+ interface ProcessEnv {
16
+ /** NODE 内置环境变量, 会影响到最终构建生成产物 */
17
+ NODE_ENV: 'development' | 'production',
18
+ /** 当前构建的平台(当前 fork 仅支持 weapp) */
19
+ TARO_ENV: 'weapp'
20
+ /**
21
+ * 当前构建的小程序 appid
22
+ * @description 若不同环境有不同的小程序,可通过在 env 文件中配置环境变量`TARO_APP_ID`来方便快速切换 appid, 而不必手动去修改 dist/project.config.json 文件
23
+ * @see https://taro-docs.jd.com/docs/next/env-mode-config#特殊环境变量-taro_app_id
24
+ */
25
+ TARO_APP_ID: string
26
+ }
27
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "plugins": [],
3
+ "presets": []
4
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "taro-global-config",
3
+ "description": "taro-global-config",
4
+ "license": "ISC"
5
+ }
@@ -0,0 +1,32 @@
1
+ # {{ projectName }}
2
+
3
+ > {{ description }}
4
+
5
+ ## 使用
6
+
7
+ ### 安装
8
+ ```
9
+ npm i {{ projectName }} -D
10
+ ```
11
+
12
+ ### 使用插件
13
+ `/config/index.js`
14
+
15
+ ```js
16
+ {{#if (eq pluginType "plugin-template") }}
17
+ /**插件参数为 IPluginOpts {
18
+ * installPath: string 安装的路径
19
+ * css?: 'none' | 'sass' | 'stylus' | 'less'
20
+ * typescript?: boolean
21
+ * compiler?: 'webpack5' | 'vite'
22
+ * }
23
+ * 这些参数后续会被模版文件解析所用
24
+ * 如果不传,会从 package.json 的 templateInfo
25
+ */
26
+ {{/if}}
27
+ const config = {
28
+ plugins: [
29
+ ["{{ projectName }}"{{#if (eq pluginType "plugin-template") }}, { installPath:'/xxx/xx/x' }{{/if}}]
30
+ ]
31
+ }
32
+ ```
@@ -0,0 +1,3 @@
1
+ module.exports = require('./dist/index.js').default
2
+
3
+ module.exports.default = module.exports
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "{{ projectName }}",
3
+ "version": "1.0.0",
4
+ "description": "{{ description }}",
5
+ "keywords": [
6
+ "Taro",
7
+ "Plugin"
8
+ ],
9
+ "author": "",
10
+ "homepage": "",
11
+ "license": "MIT",
12
+ "main": "index.js",
13
+ "typings": "types/index.d.ts",
14
+ "files": [
15
+ "dist",
16
+ "index.js",
17
+ "types"
18
+ ],
19
+ "repository": {
20
+ "type": "git",
21
+ "url": ""
22
+ },
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "dev": "tsc -w"
26
+ },
27
+ "bugs": {
28
+ "url": ""
29
+ },
30
+ "dependencies": {
31
+ "@spcsn/taro-service": "^{{ version }}"{{#if (eq pluginType "plugin-build") }},
32
+ "webpack-chain": "^6.5.1"{{/if}}{{#if (eq pluginType "plugin-template") }},
33
+ "download": "^8.0.0",
34
+ "fs-extra": "^11.2.0",
35
+ "unzip": "^0.1.11"{{/if}}
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.19.19"
39
+ }{{#if (eq pluginType "plugin-template") }},
40
+ "resolutions": {
41
+ "graceful-fs": "^4.2.11"
42
+ }{{/if}}
43
+ }
@@ -0,0 +1,153 @@
1
+ import type { IPluginContext } from '@spcsn/taro-service'
2
+ {{#if (eq pluginType "plugin-build") }}
3
+ import webpackChain from 'webpack-chain'
4
+
5
+ /**
6
+ * 编译过程扩展
7
+ */
8
+ export default (ctx: IPluginContext, pluginOpts) => {
9
+ ctx.onBuildStart(() => {
10
+ console.log('插件入参:', pluginOpts)
11
+ console.log('编译开始')
12
+ })
13
+
14
+ ctx.modifyWebpackChain(({ chain }: { chain: webpackChain }) => {
15
+ console.log('这里可以修改webpack配置')
16
+ // 示例:利用webpackChain向html中插入脚本
17
+ if (process.env.TARO_ENV !== 'h5') return
18
+ chain
19
+ .plugin('htmlWebpackPlugin')
20
+ .tap(([pluginConfig]) => {
21
+ return [
22
+ {
23
+ ...pluginConfig,
24
+ script: pluginConfig.script + 'console.log("向html中插入代码");'
25
+ }
26
+ ]
27
+ })
28
+ })
29
+
30
+ ctx.onBuildComplete(() => {
31
+ console.log('Taro 构建完成!')
32
+ })
33
+
34
+ ctx.modifyBuildAssets(({ assets }) => {
35
+ console.log('修改编译后的结果')
36
+ // 示例:修改html产物内容
37
+ const indexHtml = assets['index.html']
38
+ if (indexHtml && indexHtml._value) {
39
+ indexHtml._value = indexHtml._value.replace(/<title>(.*?)<\/title>/,'<title>被插件修改过的标题</title>')
40
+ }
41
+ })
42
+
43
+ ctx.onBuildFinish(() => {
44
+ console.log('Webpack 编译结束!')
45
+ })
46
+ }
47
+ {{/if}}
48
+ {{#if (eq pluginType "plugin-command") }}
49
+
50
+ /**
51
+ * 命令行扩展
52
+ */
53
+ export default (ctx: IPluginContext, pluginOpts) => {
54
+ ctx.registerCommand({
55
+ // 命令名
56
+ name: 'say',
57
+ // 参数说明,执行 taro say --help 时输出的 options 信息
58
+ optionsMap: {
59
+ '--msg': '输出的信息',
60
+ },
61
+ // 执行 taro say --help 时输出的使用例子的信息
62
+ synopsisList: ['taro say --msg Hello!'],
63
+ // 命令钩子
64
+ async fn() {
65
+ console.log('插件入参:', pluginOpts)
66
+ const { msg } = ctx.runOpts.options
67
+ console.log('Taro say:', msg)
68
+ },
69
+ })
70
+ }
71
+ {{/if}}
72
+ {{#if (eq pluginType "plugin-template") }}
73
+ import * as fs from 'fs-extra'
74
+ const path = require('path')
75
+ const download = require('download')
76
+ const unzip = require("unzip")
77
+ /**
78
+ * 创建 page 自定义模版
79
+ */
80
+
81
+ interface ITemplateInfo {
82
+ css: 'none' | 'sass' | 'stylus' | 'less'
83
+ typescript?: boolean
84
+ compiler?: 'webpack5' | 'vite'
85
+ template?: string
86
+ }
87
+
88
+ type TCustomTemplateInfo = Omit<ITemplateInfo & {
89
+ isCustomTemplate?: boolean
90
+ customTemplatePath?: string
91
+ }, 'template'>
92
+
93
+ type TSetCustomTemplateConfig = (customTemplateConfig: TCustomTemplateInfo) => void
94
+
95
+ interface IPluginOpts extends ITemplateInfo {
96
+ installPath: string
97
+ }
98
+
99
+ export default (ctx: IPluginContext, pluginOpts:IPluginOpts) => {
100
+ ctx.modifyCreateTemplate(async (setCustomTemplateConfig: TSetCustomTemplateConfig)=> {
101
+ const { installPath, css, typescript, compiler } = pluginOpts
102
+ const templateName = 'mobx'
103
+ const templatePath = path.join(installPath, templateName)
104
+ const customTemplateConfig = {
105
+ //自定义模版路径
106
+ customTemplatePath: templatePath,
107
+ css,
108
+ typescript,
109
+ compiler
110
+ }
111
+
112
+ /**
113
+ * 下载模版到电脑本地,可以自行进行判断,看是否需要重新下载
114
+ * 从哪里下载,如何下载,taro 官方不做限定
115
+ * 模版格式和社区模版一样
116
+ * 只要保证下载后的文件目录为 `${templatePath}` 即可,taro 会在该目录下获取模版
117
+ * 如果下载模版失败,请不要调用 setCustomTemplateConfig,taro 会根据默认流程进行兜底创建
118
+ */
119
+ if (!fs.existsSync(templatePath)) {
120
+ //如果文件不存在,就下载文件到指定路径
121
+ await downloadTemplate(customTemplateConfig)
122
+ }
123
+
124
+ if (fs.existsSync(templatePath)) {
125
+ //如果文件下载成功,调用 setCustomTemplateConfig
126
+ setCustomTemplateConfig(customTemplateConfig)
127
+ }
128
+ })
129
+ }
130
+
131
+
132
+ const downloadTemplate = async (customTemplateConfig) => {
133
+ return new Promise<void>(async (resolve, reject)=>{
134
+ const url = 'https://storage.360buyimg.com/olympic-models-test/mobx.zip'
135
+ const { name, templatePath } = customTemplateConfig
136
+ const zipName = `${name}.zip`
137
+ const zipPath = path.join(templatePath, zipName)
138
+ fs.writeFileSync(zipPath, await download(url))
139
+ const extract = unzip.Extract({ path: templatePath })
140
+ fs.createReadStream(zipPath).pipe(extract)
141
+ extract.on('close', function () {
142
+ console.log("解压完成!!")
143
+ //删除
144
+ fs.unlinkSync(zipPath)
145
+ resolve()
146
+ })
147
+ extract.on('error', function (err) {
148
+ console.log(err)
149
+ reject()
150
+ })
151
+ })
152
+ }
153
+ {{/if}}
@@ -0,0 +1,28 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "allowSyntheticDefaultImports": true,
5
+ "experimentalDecorators": true,
6
+ "noImplicitAny": false,
7
+ "noUnusedLocals": true,
8
+ "noUnusedParameters": true,
9
+ "removeComments": false,
10
+ "resolveJsonModule": true,
11
+ "skipLibCheck": true,
12
+ "strictNullChecks": true,
13
+ "target": "ES2017",
14
+ "module": "commonjs",
15
+ "preserveConstEnums": true,
16
+ "outDir": "./dist",
17
+ "traceResolution": false,
18
+ "sourceMap": true,
19
+ "rootDir": "./src",
20
+ "declaration": true,
21
+ "declarationDir": "types"
22
+ },
23
+ "include": ["./src"],
24
+ "exclude": [
25
+ "node_modules",
26
+ "dist"
27
+ ]
28
+ }