@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.0-beta.1

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 (223) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -3
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +252 -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 +31 -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 +137 -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 +252 -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 +48 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +80 -0
  32. package/dist/create/project.js +519 -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 +32 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +21 -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 +239 -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 +88 -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 +91 -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 +139 -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 +29 -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 +79 -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 +59 -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 +118 -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 +171 -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 +40 -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 +17 -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 +22 -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 +11 -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 +17 -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 +76 -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 +101 -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 +25 -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 +117 -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 +12 -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 +125 -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 +76 -4
  115. package/postinstall.js +14 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +99 -0
  118. package/src/__tests__/cli.spec.ts +233 -0
  119. package/src/__tests__/config.spec.ts +232 -0
  120. package/src/__tests__/doctor-config.spec.ts +805 -0
  121. package/src/__tests__/doctor-eslint.spec.ts +31 -0
  122. package/src/__tests__/doctor-recommand.spec.ts +158 -0
  123. package/src/__tests__/doctor.spec.ts +77 -0
  124. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  125. package/src/__tests__/env/.env +2 -0
  126. package/src/__tests__/fixtures/default/.env +3 -0
  127. package/src/__tests__/fixtures/default/.env.development +1 -0
  128. package/src/__tests__/fixtures/default/.env.local +1 -0
  129. package/src/__tests__/fixtures/default/.env.pre +3 -0
  130. package/src/__tests__/fixtures/default/.env.production +1 -0
  131. package/src/__tests__/fixtures/default/.env.uat +5 -0
  132. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  133. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  134. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  135. package/src/__tests__/fixtures/default/config/index.js +84 -0
  136. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  137. package/src/__tests__/fixtures/default/package.json +38 -0
  138. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  139. package/src/__tests__/fixtures/default/src/app.js +16 -0
  140. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  141. package/src/__tests__/fixtures/default/src/index.html +19 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  144. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  145. package/src/__tests__/info.spec.ts +74 -0
  146. package/src/__tests__/inspect.spec.ts +163 -0
  147. package/src/__tests__/project.spec.ts +304 -0
  148. package/src/__tests__/update.spec.ts +324 -0
  149. package/src/__tests__/utils/index.ts +54 -0
  150. package/src/cli.ts +250 -0
  151. package/src/commands/customCommand.ts +26 -0
  152. package/src/config/index.ts +7 -0
  153. package/src/config/manifest.default.json +35 -0
  154. package/src/config/packagesManagement.ts +20 -0
  155. package/src/config/rn-stylelint.json +5 -0
  156. package/src/config/tsconfig.json +24 -0
  157. package/src/create/constants.ts +1 -0
  158. package/src/create/creator.ts +37 -0
  159. package/src/create/fetchTemplate.ts +152 -0
  160. package/src/create/page.ts +299 -0
  161. package/src/create/plugin.ts +55 -0
  162. package/src/create/project.ts +591 -0
  163. package/src/doctor/index.ts +27 -0
  164. package/src/index.ts +24 -0
  165. package/src/presets/commands/build.ts +227 -0
  166. package/src/presets/commands/config.ts +86 -0
  167. package/src/presets/commands/create.ts +105 -0
  168. package/src/presets/commands/global-config.ts +140 -0
  169. package/src/presets/commands/help.ts +19 -0
  170. package/src/presets/commands/info.ts +77 -0
  171. package/src/presets/commands/init.ts +66 -0
  172. package/src/presets/commands/inspect.ts +138 -0
  173. package/src/presets/commands/update.ts +179 -0
  174. package/src/presets/constant/hooks.ts +14 -0
  175. package/src/presets/constant/index.ts +1 -0
  176. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  177. package/src/presets/files/generateProjectConfig.ts +41 -0
  178. package/src/presets/files/writeFileToDist.ts +17 -0
  179. package/src/presets/hooks/build.ts +22 -0
  180. package/src/presets/hooks/create.ts +11 -0
  181. package/src/presets/index.ts +15 -0
  182. package/src/presets/platforms/plugin.ts +81 -0
  183. package/src/presets/platforms/rn.ts +113 -0
  184. package/src/util/appConfig.ts +29 -0
  185. package/src/util/createPage.ts +132 -0
  186. package/src/util/defineConfig.ts +22 -0
  187. package/src/util/index.ts +127 -0
  188. package/src/util/types.ts +81 -0
  189. package/templates/default/.husky/commit-msg +4 -0
  190. package/templates/default/_editorconfig +12 -0
  191. package/templates/default/_env.development +2 -0
  192. package/templates/default/_env.production +1 -0
  193. package/templates/default/_env.test +1 -0
  194. package/templates/default/_eslintrc +11 -0
  195. package/templates/default/_gitignore +8 -0
  196. package/templates/default/babel.config.js +14 -0
  197. package/templates/default/commitlint.config.mjs +1 -0
  198. package/templates/default/config/dev.js +10 -0
  199. package/templates/default/config/index.js +106 -0
  200. package/templates/default/config/prod.js +46 -0
  201. package/templates/default/package.json.tmpl +158 -0
  202. package/templates/default/project.config.json +15 -0
  203. package/templates/default/src/app.config.js +11 -0
  204. package/templates/default/src/app.css +0 -0
  205. package/templates/default/src/app.js +44 -0
  206. package/templates/default/src/index.html +17 -0
  207. package/templates/default/src/pages/index/index.config.js +3 -0
  208. package/templates/default/src/pages/index/index.css +0 -0
  209. package/templates/default/src/pages/index/index.jsx +15 -0
  210. package/templates/default/src/pages/index/index.vue +23 -0
  211. package/templates/default/stylelint.config.mjs +4 -0
  212. package/templates/default/template_creator.js +77 -0
  213. package/templates/default/tsconfig.json +32 -0
  214. package/templates/default/types/global.d.ts +35 -0
  215. package/templates/default/types/solid.d.ts +12 -0
  216. package/templates/default/types/vue.d.ts +10 -0
  217. package/templates/global-config/index.json +4 -0
  218. package/templates/global-config/package.json +5 -0
  219. package/templates/plugin-compile/README.md +32 -0
  220. package/templates/plugin-compile/index.js +3 -0
  221. package/templates/plugin-compile/package.json.tmpl +43 -0
  222. package/templates/plugin-compile/src/index.ts +153 -0
  223. package/templates/plugin-compile/tsconfig.json +29 -0
@@ -0,0 +1,31 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { chalk } from '@tarojs/helper'
4
+
5
+ import doctor from '../doctor'
6
+
7
+ const validator = doctor.validators[4]
8
+
9
+ describe('eslint validator of doctor', () => {
10
+ let cwd = ''
11
+ beforeEach(() => {
12
+ cwd = process.cwd()
13
+ })
14
+
15
+ afterEach(() => {
16
+ process.chdir(cwd)
17
+ })
18
+
19
+ it('should lint for react', async () => {
20
+ process.chdir(path.join(__dirname, 'fixtures/default'))
21
+ const { isValid, messages } = await validator({
22
+ projectConfig: {
23
+ framework: 'react',
24
+ sourceRoot: 'src'
25
+ },
26
+ chalk
27
+ })
28
+ expect(isValid).toBe(true)
29
+ expect(messages.length).toBe(2)
30
+ })
31
+ })
@@ -0,0 +1,158 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { chalk, fs } from '@tarojs/helper'
4
+
5
+ import doctor from '../doctor'
6
+
7
+ const validator = doctor.validators[3]
8
+
9
+ jest.mock('@tarojs/helper', () => {
10
+ const helper = jest.requireActual('@tarojs/helper')
11
+ const fs = helper.fs
12
+ return {
13
+ __esModule: true,
14
+ ...helper,
15
+ fs: {
16
+ ...fs,
17
+ readdirSync: jest.fn(),
18
+ existsSync: jest.fn(),
19
+ },
20
+ }
21
+ })
22
+
23
+ describe('recommand validator of doctor', () => {
24
+ const existsSyncMocked = fs.existsSync as jest.Mock<any>
25
+ const readdirSyncMocked = fs.readdirSync as jest.Mock<any>
26
+
27
+ beforeEach(() => {
28
+ jest.resetModules()
29
+ existsSyncMocked.mockReset()
30
+ readdirSyncMocked.mockReset()
31
+ existsSyncMocked.mockReturnValue(true)
32
+ })
33
+
34
+ it.skip("should exit because there isn't a Taro project", async () => {
35
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
36
+ const logSpy = jest.spyOn(console, 'log')
37
+
38
+ exitSpy.mockImplementation(() => {
39
+ throw new Error()
40
+ })
41
+ logSpy.mockImplementation(() => {})
42
+ existsSyncMocked.mockReturnValue(false)
43
+
44
+ const MOCK_APP_PATH = 'src/'
45
+ try {
46
+ await validator({ appPath: MOCK_APP_PATH })
47
+ } catch (error) {} // eslint-disable-line no-empty
48
+
49
+ expect(exitSpy).toBeCalledWith(1)
50
+ const PROJECT_PACKAGE_PATH = path.join(MOCK_APP_PATH, 'package.json')
51
+ expect(logSpy).toBeCalledWith(chalk.red(`找不到${PROJECT_PACKAGE_PATH},请确定当前目录是Taro项目根目录!`))
52
+
53
+ exitSpy.mockRestore()
54
+ logSpy.mockRestore()
55
+ })
56
+
57
+ it.skip('should warn when test framework not found', async () => {
58
+ jest.doMock('./fixtures/default/package.json', () => ({
59
+ devDependencies: {
60
+ eslint: 1,
61
+ },
62
+ }))
63
+ readdirSyncMocked.mockReturnValue(['readme', '.gitignore', '.editorconfig'])
64
+
65
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
66
+
67
+ expect(lines.length).toBe(1)
68
+ expect(lines[0].desc).toBe(
69
+ '没有检查到常见的测试依赖(jest/mocha/ava/tape/jesmine/karma), 配置测试可以帮助提升项目质量'
70
+ )
71
+ expect(lines[0].valid).toBe(true)
72
+ expect(lines[0].solution).toBe(
73
+ '可以参考 https://github.com/NervJS/taro-ui-sample 项目, 其中已经包含了完整的测试配置与范例'
74
+ )
75
+
76
+ jest.dontMock('./fixtures/default/package.json')
77
+ })
78
+
79
+ it.skip('should warn when linters not found', async () => {
80
+ jest.doMock('./fixtures/default/package.json', () => ({
81
+ devDependencies: {
82
+ jest: 1,
83
+ },
84
+ }))
85
+ readdirSyncMocked.mockReturnValue(['readme.md', '.gitignore', '.editorconfig'])
86
+
87
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
88
+
89
+ expect(lines.length).toBe(1)
90
+ expect(lines[0].desc).toBe(
91
+ '没有检查到常见的 linter (eslint/jslint/jshint/tslint), 配置 linter 可以帮助提升项目质量'
92
+ )
93
+ expect(lines[0].valid).toBe(true)
94
+ expect(lines[0].solution).toBe(
95
+ 'Taro 还提供了定制的 ESLint 规则, 可以帮助开发者避免一些常见的问题. 使用 taro cli 创建新项目即可体验'
96
+ )
97
+
98
+ jest.dontMock('./fixtures/default/package.json')
99
+ })
100
+
101
+ it.skip('should warn when Readme not found', async () => {
102
+ jest.doMock('./fixtures/default/package.json', () => ({
103
+ devDependencies: {
104
+ mocha: 1,
105
+ jslint: 1,
106
+ },
107
+ }))
108
+ readdirSyncMocked.mockReturnValue(['.gitignore', '.editorconfig'])
109
+
110
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
111
+
112
+ expect(lines.length).toBe(1)
113
+ expect(lines[0].desc).toBe(
114
+ '没有检查到 Readme (readme/readme.md/readme.markdown), 编写 Readme 可以方便其他人了解项目'
115
+ )
116
+ expect(lines[0].valid).toBe(true)
117
+
118
+ jest.dontMock('./fixtures/default/package.json')
119
+ })
120
+
121
+ it.skip('should warn when .gitignore not found', async () => {
122
+ jest.doMock('./fixtures/default/package.json', () => ({
123
+ devDependencies: {
124
+ jesmine: 1,
125
+ tslint: 1,
126
+ },
127
+ }))
128
+ readdirSyncMocked.mockReturnValue(['readme.markdown', '.editorconfig'])
129
+
130
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
131
+
132
+ expect(lines.length).toBe(1)
133
+ expect(lines[0].desc).toBe(
134
+ '没有检查到 .gitignore 配置, 配置 .gitignore 以避免将敏感信息或不必要的内容提交到代码仓库'
135
+ )
136
+ expect(lines[0].valid).toBe(true)
137
+
138
+ jest.dontMock('./fixtures/default/package.json')
139
+ })
140
+
141
+ it.skip('should warn when .editorconfig not found', async () => {
142
+ jest.doMock('./fixtures/default/package.json', () => ({
143
+ devDependencies: {
144
+ karma: 1,
145
+ jshint: 1,
146
+ },
147
+ }))
148
+ readdirSyncMocked.mockReturnValue(['readme', '.gitignore'])
149
+
150
+ const { lines } = await validator({ appPath: path.join(__dirname, './fixtures/default') })
151
+
152
+ expect(lines.length).toBe(1)
153
+ expect(lines[0].desc).toBe('没有检查到 .editconfig 配置, 配置 editconfig 以统一项目成员编辑器的代码风格')
154
+ expect(lines[0].valid).toBe(true)
155
+
156
+ jest.dontMock('./fixtures/default/package.json')
157
+ })
158
+ })
@@ -0,0 +1,77 @@
1
+ import { chalk } from '@tarojs/helper'
2
+
3
+ import { run } from './utils'
4
+
5
+ jest.mock('../doctor', () => {
6
+ return {
7
+ __esModule: true,
8
+ default: {
9
+ validators: [() => ({
10
+ isValid: true,
11
+ messgaes: [{
12
+ kind: 2,
13
+ content: 'Env Success'
14
+ }]
15
+ }), () => Promise.resolve({
16
+ isValid: false,
17
+ messgaes: [{
18
+ kind: 1,
19
+ content: 'Config Error'
20
+ }]
21
+ }), () => ({
22
+ isValid: true,
23
+ messgaes: [{
24
+ kind: 2,
25
+ content: 'Package Success'
26
+ }]
27
+ }), () => ({
28
+ isValid: true,
29
+ messgaes: [{
30
+ kind: 2,
31
+ content: 'recommend Success'
32
+ }]
33
+ }), async () => ({
34
+ isValid: true,
35
+ messgaes: [{
36
+ kind: 2,
37
+ content: 'eslint Success'
38
+ }]
39
+ })]
40
+ }
41
+ }
42
+ })
43
+
44
+ jest.mock('ora', () => {
45
+ const ora = jest.fn()
46
+ ora.mockReturnValue({
47
+ start () {
48
+ return {
49
+ succeed () {}
50
+ }
51
+ }
52
+ })
53
+ return ora
54
+ })
55
+
56
+ const runDoctor = run('doctor', [require.resolve('@tarojs/plugin-doctor')])
57
+
58
+ describe('doctor', () => {
59
+ it('should exit because there isn\'t a Taro project', async () => {
60
+ const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
61
+ const logSpy = jest.spyOn(console, 'log')
62
+
63
+ exitSpy.mockImplementation(() => {
64
+ throw new Error()
65
+ })
66
+ logSpy.mockImplementation(() => {})
67
+ try {
68
+ await runDoctor('', { options: { disableGlobalConfig: true } })
69
+ } catch (error) {} // eslint-disable-line no-empty
70
+
71
+ expect(exitSpy).toBeCalledWith(1)
72
+ expect(logSpy).toBeCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))
73
+
74
+ exitSpy.mockRestore()
75
+ logSpy.mockRestore()
76
+ })
77
+ })
@@ -0,0 +1,105 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { dotenvParse } from '@tarojs/helper'
4
+ import { Kernel } from '@tarojs/service'
5
+
6
+ import CLI from '../cli'
7
+
8
+ jest.mock('@tarojs/service')
9
+ const MockedKernel = Kernel as unknown as jest.Mock<Kernel>
10
+ const APP_PATH = path.join(__dirname, 'fixtures/default')
11
+
12
+ function setProcessArgv (cmd: string) {
13
+ // @ts-ignore
14
+ process.argv = [null, ...cmd.split(' ')]
15
+ }
16
+
17
+ describe('inspect', () => {
18
+ let cli: CLI
19
+
20
+ beforeAll(() => {
21
+ cli = new CLI(APP_PATH)
22
+ })
23
+
24
+ beforeEach(() => {
25
+ MockedKernel.mockClear()
26
+ process.argv = []
27
+ delete process.env.NODE_ENV
28
+ delete process.env.TARO_ENV
29
+ delete process.env.TARO_APP_TEST
30
+ delete process.env.TARO_APP_ID
31
+ delete process.env.JD_APP_TEST
32
+ delete process.env.TARO_APP_DEFAULT
33
+ delete process.env.TARO_APP_FOO
34
+ })
35
+
36
+ afterEach(() => {
37
+ MockedKernel.mockClear()
38
+ process.argv = []
39
+ delete process.env.NODE_ENV
40
+ delete process.env.TARO_ENV
41
+ delete process.env.TARO_APP_TEST
42
+ delete process.env.TARO_APP_ID
43
+ delete process.env.JD_APP_TEST
44
+ delete process.env.TARO_APP_DEFAULT
45
+ delete process.env.TARO_APP_FOO
46
+ })
47
+
48
+ describe('cli mode env', () => {
49
+ it('dotenvParse .env .env.dev should success', async () => {
50
+ expect(process.env.TARO_test).toBeUndefined()
51
+ dotenvParse(path.resolve(__dirname, 'env'), 'TARO_', 'dev')
52
+ expect(process.env.TARO_test).toBe('123')
53
+ expect(process.env._TARO_test).toBeUndefined()
54
+ })
55
+
56
+ it('--watch true => 默认加载 .env.development', async () => {
57
+ setProcessArgv('taro build --watch --type weapp')
58
+ await cli.run()
59
+ expect(process.env.TARO_APP_TEST).toEqual('env-development')
60
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default')
61
+ })
62
+
63
+ it('--watch false => 默认加载 .env.production', async () => {
64
+ setProcessArgv('taro build --type weapp')
65
+ await cli.run()
66
+ expect(process.env.TARO_APP_TEST).toEqual('env-production')
67
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default')
68
+ })
69
+
70
+ it('指定加载 .env.pre', async () => {
71
+ setProcessArgv('taro build --type weapp --mode pre')
72
+ await cli.run()
73
+ expect(process.env.TARO_APP_TEST).toEqual('env-pre')
74
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default')
75
+ })
76
+
77
+ it('env.local 比 .env 优先级更高', async () => {
78
+ setProcessArgv('taro build --type weapp --mode find404')
79
+ await cli.run()
80
+ expect(process.env.TARO_APP_TEST).toEqual('env-local')
81
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default')
82
+ })
83
+
84
+ it('env.uat.local 比 .env.uat 优先级更高', async () => {
85
+ setProcessArgv('taro build --type weapp --mode uat')
86
+ await cli.run()
87
+ expect(process.env.TARO_APP_TEST).toEqual('env-uat-local')
88
+ expect(process.env.TARO_APP_DEFAULT).toEqual('default')
89
+ })
90
+
91
+ it('自定义前缀: JD_APP_', async () => {
92
+ setProcessArgv('taro build --type weapp --mode uat --env-prefix JD_APP_')
93
+ await cli.run()
94
+ expect(process.env.JD_APP_TEST).toEqual('env-uat')
95
+ expect(process.env.TARO_APP_TEST).toEqual(undefined)
96
+ expect(process.env.TARO_APP_ID).toEqual('特殊变量appid')
97
+ })
98
+
99
+ it('环境变量可以相互引用', async () => {
100
+ setProcessArgv('taro build --type weapp --mode pre')
101
+ await cli.run()
102
+ expect(process.env.TARO_APP_FOO).toEqual('env-pre-foo')
103
+ })
104
+ })
105
+ })
@@ -0,0 +1,2 @@
1
+ TARO_test = 123
2
+ _TARO_test = 456
@@ -0,0 +1,3 @@
1
+ TARO_APP_TEST=env
2
+
3
+ TARO_APP_DEFAULT=default
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-development
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-local
@@ -0,0 +1,3 @@
1
+ TARO_APP_TEST=env-pre
2
+
3
+ TARO_APP_FOO=${TARO_APP_TEST}-foo
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-production
@@ -0,0 +1,5 @@
1
+ TARO_APP_TEST=env-uat
2
+
3
+ JD_APP_TEST=env-uat
4
+
5
+ TARO_APP_ID=特殊变量appid
@@ -0,0 +1 @@
1
+ TARO_APP_TEST=env-uat-local
@@ -0,0 +1,10 @@
1
+ // babel-preset-taro 更多选项和默认值:
2
+ // https://github.com/NervJS/taro/blob/next/packages/babel-preset-taro/README.md
3
+ module.exports = {
4
+ presets: [
5
+ ['taro', {
6
+ framework: 'react',
7
+ ts: false
8
+ }]
9
+ ]
10
+ }
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ env: {
3
+ NODE_ENV: '"development"'
4
+ },
5
+ defineConstants: {
6
+ },
7
+ mini: {},
8
+ h5: {}
9
+ }
@@ -0,0 +1,84 @@
1
+ const config = {
2
+ projectName: 't2',
3
+ date: '2020-5-21',
4
+ designWidth: 750,
5
+ deviceRatio: {
6
+ 640: 2.34 / 2,
7
+ 750: 1,
8
+ 828: 1.81 / 2
9
+ },
10
+ sourceRoot: 'src',
11
+ outputRoot: 'dist',
12
+ plugins: [],
13
+ defineConstants: {
14
+ },
15
+ copy: {
16
+ patterns: [
17
+ ],
18
+ options: {
19
+ }
20
+ },
21
+ framework: 'react',
22
+ compiler: {
23
+ type: 'webpack5',
24
+ prebundle: {
25
+ enable: false
26
+ }
27
+ },
28
+ mini: {
29
+ output: {
30
+ clean: {
31
+ keep: ['project.config.json']
32
+ }
33
+ },
34
+ postcss: {
35
+ pxtransform: {
36
+ enable: true,
37
+ config: {
38
+
39
+ }
40
+ },
41
+ url: {
42
+ enable: true,
43
+ config: {
44
+ limit: 1024 // 设定转换尺寸上限
45
+ }
46
+ },
47
+ cssModules: {
48
+ enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
49
+ config: {
50
+ namingPattern: 'module', // 转换模式,取值为 global/module
51
+ generateScopedName: '[name]__[local]___[hash:base64:5]'
52
+ }
53
+ }
54
+ }
55
+ },
56
+ h5: {
57
+ output: {
58
+ clean: false
59
+ },
60
+ publicPath: '/',
61
+ staticDirectory: 'static',
62
+ postcss: {
63
+ autoprefixer: {
64
+ enable: true,
65
+ config: {
66
+ }
67
+ },
68
+ cssModules: {
69
+ enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
70
+ config: {
71
+ namingPattern: 'module', // 转换模式,取值为 global/module
72
+ generateScopedName: '[name]__[local]___[hash:base64:5]'
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ module.exports = function (merge) {
80
+ if (process.env.NODE_ENV === 'development') {
81
+ return merge({}, config, require('./dev'))
82
+ }
83
+ return merge({}, config, require('./prod'))
84
+ }
@@ -0,0 +1,18 @@
1
+ module.exports = {
2
+ env: {
3
+ NODE_ENV: '"production"'
4
+ },
5
+ defineConstants: {
6
+ },
7
+ mini: {},
8
+ h5: {
9
+ /**
10
+ * 如果h5端编译后体积过大,可以使用webpack-bundle-analyzer插件对打包体积进行分析。
11
+ * 参考代码如下:
12
+ * webpackChain (chain) {
13
+ * chain.plugin('analyzer')
14
+ * .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [])
15
+ * }
16
+ */
17
+ }
18
+ }
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "test",
3
+ "dependencies": {
4
+ "@babel/runtime": "^7.7.7",
5
+ "@tarojs/shared": "3.0.0-beta.6",
6
+ "@tarojs/taro": "3.0.0-beta.6",
7
+ "@tarojs/api": "3.0.0-beta.6",
8
+ "@tarojs/cli": "3.0.0-beta.6",
9
+ "@tarojs/components": "3.0.0-beta.6",
10
+ "@tarojs/taro-h5": "3.0.0-beta.6",
11
+ "@tarojs/helper": "3.0.0-beta.6",
12
+ "@tarojs/taro-loader": "3.0.0-beta.6",
13
+ "@tarojs/mini-runner": "3.0.0-beta.6",
14
+ "@tarojs/react": "3.0.0-beta.6",
15
+ "@tarojs/router": "3.0.0-beta.6",
16
+ "@tarojs/runner-utils": "3.0.0-beta.6",
17
+ "@tarojs/runtime": "3.0.0-beta.6",
18
+ "@tarojs/service": "3.0.0-beta.6",
19
+ "@tarojs/webpack-runner": "3.0.0-beta.6",
20
+ "@tarojs/with-weapp": "3.0.0-beta.6",
21
+ "@tarojs/plugin-platform-alipay": "3.0.0-beta.6",
22
+ "@tarojs/plugin-platform-h5": "3.0.0-beta.6",
23
+ "@tarojs/plugin-platform-jd": "3.0.0-beta.6",
24
+ "@tarojs/plugin-platform-qq": "3.0.0-beta.6",
25
+ "@tarojs/plugin-platform-swan": "3.0.0-beta.6",
26
+ "@tarojs/plugin-platform-tt": "3.0.0-beta.6",
27
+ "@tarojs/plugin-platform-weapp": "3.0.0-beta.6",
28
+ "@tarojs/taroize": "1.3.22"
29
+ },
30
+ "devDependencies": {
31
+ "babel-preset-taro": "2.0.0",
32
+ "eslint": "^8.12.0",
33
+ "eslint-config-taro": "3.0.0-beta.6",
34
+ "babel-plugin-transform-taroapi": "3.0.0-beta.6",
35
+ "postcss-plugin-constparse": "3.0.0-beta.6",
36
+ "postcss-pxtransform": "3.0.0-beta.6"
37
+ }
38
+ }
@@ -0,0 +1,11 @@
1
+ export default {
2
+ pages: [
3
+ 'pages/index/index'
4
+ ],
5
+ window: {
6
+ backgroundTextStyle: 'light',
7
+ navigationBarBackgroundColor: '#fff',
8
+ navigationBarTitleText: 'WeChat',
9
+ navigationBarTextStyle: 'black'
10
+ }
11
+ }
@@ -0,0 +1,16 @@
1
+ import { Component } from 'react'
2
+
3
+ class App extends Component {
4
+ componentDidMount () {}
5
+
6
+ componentDidShow () {}
7
+
8
+ componentDidHide () {}
9
+
10
+ // this.props.children 是将要会渲染的页面
11
+ render () {
12
+ return this.props.children
13
+ }
14
+ }
15
+
16
+ export default App
File without changes
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
5
+ <meta content="width=device-width,initial-scale=1,user-scalable=no" name="viewport">
6
+ <meta name="apple-mobile-web-app-capable" content="yes">
7
+ <meta name="apple-touch-fullscreen" content="yes">
8
+ <meta name="format-detection" content="telephone=no,address=no">
9
+ <meta name="apple-mobile-web-app-status-bar-style" content="white">
10
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
11
+ <title></title>
12
+ <script>
13
+ !function(x){function w(){var v,u,t,tes,s=x.document,r=s.documentElement,a=r.getBoundingClientRect().width;if(!v&&!u){var n=!!x.navigator.appVersion.match(/AppleWebKit.*Mobile.*/);v=x.devicePixelRatio;tes=x.devicePixelRatio;v=n?v:1,u=1/v}if(a>=640){r.style.fontSize="40px"}else{if(a<=320){r.style.fontSize="20px"}else{r.style.fontSize=a/320*20+"px"}}}x.addEventListener("resize",function(){w()});w()}(window);
14
+ </script>
15
+ </head>
16
+ <body>
17
+ <div id="app"></div>
18
+ </body>
19
+ </html>
@@ -0,0 +1,3 @@
1
+ export default {
2
+ navigationBarTitleText: '首页'
3
+ }
@@ -0,0 +1,22 @@
1
+ import React, { Component } from 'react'
2
+ import { View, Text } from '@tarojs/components'
3
+
4
+ export default class Index extends Component {
5
+ componentWillMount () { }
6
+
7
+ componentDidMount () { }
8
+
9
+ componentWillUnmount () { }
10
+
11
+ componentDidShow () { }
12
+
13
+ componentDidHide () { }
14
+
15
+ render () {
16
+ return (
17
+ <View className='index'>
18
+ <Text>Hello world!</Text>
19
+ </View>
20
+ )
21
+ }
22
+ }