@modern-js/core 1.8.0 → 1.9.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 (196) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +22 -0
  3. package/bin/modern-js.js +1 -1
  4. package/compiled/ajv/codegen.js +1 -0
  5. package/compiled/ajv/dist/ajv.d.ts +16 -0
  6. package/compiled/ajv/dist/compile/codegen/code.d.ts +40 -0
  7. package/compiled/ajv/dist/compile/codegen/index.d.ts +79 -0
  8. package/compiled/ajv/dist/compile/codegen/scope.d.ts +79 -0
  9. package/compiled/ajv/dist/compile/errors.d.ts +13 -0
  10. package/compiled/ajv/dist/compile/index.d.ts +80 -0
  11. package/compiled/ajv/dist/compile/ref_error.d.ts +6 -0
  12. package/compiled/ajv/dist/compile/resolve.d.ts +12 -0
  13. package/compiled/ajv/dist/compile/rules.d.ts +28 -0
  14. package/compiled/ajv/dist/compile/util.d.ts +40 -0
  15. package/compiled/ajv/dist/compile/validate/index.d.ts +42 -0
  16. package/compiled/ajv/dist/compile/validate/subschema.d.ts +47 -0
  17. package/compiled/ajv/dist/core.d.ts +173 -0
  18. package/compiled/ajv/dist/runtime/validation_error.d.ts +7 -0
  19. package/compiled/ajv/dist/types/index.d.ts +183 -0
  20. package/compiled/ajv/dist/types/json-schema.d.ts +124 -0
  21. package/compiled/ajv/dist/types/jtd-schema.d.ts +169 -0
  22. package/compiled/ajv/index.js +9 -0
  23. package/compiled/ajv/license +22 -0
  24. package/compiled/ajv/package.json +1 -0
  25. package/compiled/ajv/uri-js/dist/es5/uri.all.d.ts +59 -0
  26. package/compiled/ajv-keywords/ajv/dist/ajv.d.ts +16 -0
  27. package/compiled/ajv-keywords/ajv/dist/compile/codegen/code.d.ts +40 -0
  28. package/compiled/ajv-keywords/ajv/dist/compile/codegen/index.d.ts +79 -0
  29. package/compiled/ajv-keywords/ajv/dist/compile/codegen/scope.d.ts +79 -0
  30. package/compiled/ajv-keywords/ajv/dist/compile/errors.d.ts +13 -0
  31. package/compiled/ajv-keywords/ajv/dist/compile/index.d.ts +80 -0
  32. package/compiled/ajv-keywords/ajv/dist/compile/ref_error.d.ts +6 -0
  33. package/compiled/ajv-keywords/ajv/dist/compile/resolve.d.ts +12 -0
  34. package/compiled/ajv-keywords/ajv/dist/compile/rules.d.ts +28 -0
  35. package/compiled/ajv-keywords/ajv/dist/compile/util.d.ts +40 -0
  36. package/compiled/ajv-keywords/ajv/dist/compile/validate/index.d.ts +42 -0
  37. package/compiled/ajv-keywords/ajv/dist/compile/validate/subschema.d.ts +47 -0
  38. package/compiled/ajv-keywords/ajv/dist/core.d.ts +173 -0
  39. package/compiled/ajv-keywords/ajv/dist/runtime/validation_error.d.ts +7 -0
  40. package/compiled/ajv-keywords/ajv/dist/types/index.d.ts +183 -0
  41. package/compiled/ajv-keywords/ajv/dist/types/json-schema.d.ts +124 -0
  42. package/compiled/ajv-keywords/ajv/dist/types/jtd-schema.d.ts +169 -0
  43. package/compiled/ajv-keywords/dist/index.d.ts +4 -0
  44. package/compiled/ajv-keywords/index.js +1 -0
  45. package/compiled/ajv-keywords/license +21 -0
  46. package/compiled/ajv-keywords/package.json +1 -0
  47. package/compiled/ajv-keywords/uri-js/dist/es5/uri.all.d.ts +59 -0
  48. package/compiled/better-ajv-errors/ajv/dist/ajv.d.ts +16 -0
  49. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/code.d.ts +40 -0
  50. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/index.d.ts +79 -0
  51. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/scope.d.ts +79 -0
  52. package/compiled/better-ajv-errors/ajv/dist/compile/errors.d.ts +13 -0
  53. package/compiled/better-ajv-errors/ajv/dist/compile/index.d.ts +80 -0
  54. package/compiled/better-ajv-errors/ajv/dist/compile/ref_error.d.ts +6 -0
  55. package/compiled/better-ajv-errors/ajv/dist/compile/resolve.d.ts +12 -0
  56. package/compiled/better-ajv-errors/ajv/dist/compile/rules.d.ts +28 -0
  57. package/compiled/better-ajv-errors/ajv/dist/compile/util.d.ts +40 -0
  58. package/compiled/better-ajv-errors/ajv/dist/compile/validate/index.d.ts +42 -0
  59. package/compiled/better-ajv-errors/ajv/dist/compile/validate/subschema.d.ts +47 -0
  60. package/compiled/better-ajv-errors/ajv/dist/core.d.ts +173 -0
  61. package/compiled/better-ajv-errors/ajv/dist/runtime/validation_error.d.ts +7 -0
  62. package/compiled/better-ajv-errors/ajv/dist/types/index.d.ts +183 -0
  63. package/compiled/better-ajv-errors/ajv/dist/types/json-schema.d.ts +124 -0
  64. package/compiled/better-ajv-errors/ajv/dist/types/jtd-schema.d.ts +169 -0
  65. package/compiled/better-ajv-errors/index.js +1 -0
  66. package/compiled/better-ajv-errors/license +13 -0
  67. package/compiled/better-ajv-errors/package.json +1 -0
  68. package/compiled/better-ajv-errors/typings.d.ts +24 -0
  69. package/compiled/better-ajv-errors/uri-js/dist/es5/uri.all.d.ts +59 -0
  70. package/compiled/v8-compile-cache/index.js +1 -0
  71. package/compiled/v8-compile-cache/license +21 -0
  72. package/compiled/v8-compile-cache/package.json +1 -0
  73. package/dist/cli.d.ts +1 -0
  74. package/dist/cli.js +33 -0
  75. package/dist/config/defaults.d.ts +28 -0
  76. package/dist/config/defaults.js +98 -0
  77. package/dist/{types/config → config}/index.d.ts +2 -2
  78. package/dist/config/index.js +120 -0
  79. package/dist/config/mergeConfig.d.ts +30 -0
  80. package/dist/config/mergeConfig.js +26 -0
  81. package/dist/config/schema/deploy.d.ts +16 -0
  82. package/dist/config/schema/deploy.js +16 -0
  83. package/dist/config/schema/index.d.ts +466 -0
  84. package/dist/config/schema/index.js +95 -0
  85. package/dist/config/schema/output.d.ts +146 -0
  86. package/dist/config/schema/output.js +68 -0
  87. package/dist/config/schema/server.d.ts +182 -0
  88. package/dist/config/schema/server.js +107 -0
  89. package/dist/config/schema/source.d.ts +58 -0
  90. package/dist/config/schema/source.js +36 -0
  91. package/dist/config/schema/tools.d.ts +36 -0
  92. package/dist/config/schema/tools.js +19 -0
  93. package/dist/config/types/electron.d.ts +13 -0
  94. package/dist/config/types/electron.js +2 -0
  95. package/dist/config/types/index.d.ts +240 -0
  96. package/dist/config/types/index.js +2 -0
  97. package/dist/config/types/less.d.ts +10 -0
  98. package/dist/config/types/less.js +2 -0
  99. package/dist/config/types/sass.d.ts +8 -0
  100. package/dist/config/types/sass.js +2 -0
  101. package/dist/{types/config → config}/types/ssg.d.ts +8 -8
  102. package/dist/config/types/ssg.js +2 -0
  103. package/dist/config/types/test.d.ts +14 -0
  104. package/dist/config/types/test.js +2 -0
  105. package/dist/config/types/unbundle.d.ts +25 -0
  106. package/dist/config/types/unbundle.js +2 -0
  107. package/dist/{types/context.d.ts → context.d.ts} +12 -22
  108. package/dist/context.js +59 -0
  109. package/dist/index.d.ts +64 -0
  110. package/dist/index.js +177 -0
  111. package/dist/{types/initWatcher.d.ts → initWatcher.d.ts} +1 -1
  112. package/dist/initWatcher.js +75 -0
  113. package/dist/{types/loadEnv.d.ts → loadEnv.d.ts} +1 -1
  114. package/dist/loadEnv.js +21 -0
  115. package/dist/{types/loadPlugins.d.ts → loadPlugins.d.ts} +13 -16
  116. package/dist/loadPlugins.js +109 -0
  117. package/dist/manager.d.ts +72 -0
  118. package/dist/manager.js +24 -0
  119. package/dist/{types/pluginAPI.d.ts → pluginAPI.d.ts} +5 -6
  120. package/dist/pluginAPI.js +16 -0
  121. package/dist/{types/utils → utils}/commander.d.ts +1 -1
  122. package/dist/utils/commander.js +20 -0
  123. package/dist/{types/utils → utils}/repeatKeyWarning.d.ts +1 -1
  124. package/dist/utils/repeatKeyWarning.js +22 -0
  125. package/package.json +12 -26
  126. package/tsconfig.json +3 -4
  127. package/dist/js/modern/cli.js +0 -29
  128. package/dist/js/modern/config/defaults.js +0 -103
  129. package/dist/js/modern/config/index.js +0 -115
  130. package/dist/js/modern/config/mergeConfig.js +0 -22
  131. package/dist/js/modern/config/schema/deploy.js +0 -17
  132. package/dist/js/modern/config/schema/index.js +0 -107
  133. package/dist/js/modern/config/schema/output.js +0 -147
  134. package/dist/js/modern/config/schema/server.js +0 -170
  135. package/dist/js/modern/config/schema/source.js +0 -59
  136. package/dist/js/modern/config/schema/tools.js +0 -36
  137. package/dist/js/modern/config/types/electron.js +0 -1
  138. package/dist/js/modern/config/types/index.js +0 -1
  139. package/dist/js/modern/config/types/less.js +0 -0
  140. package/dist/js/modern/config/types/sass.js +0 -0
  141. package/dist/js/modern/config/types/ssg.js +0 -0
  142. package/dist/js/modern/config/types/test.js +0 -0
  143. package/dist/js/modern/config/types/unbundle.js +0 -0
  144. package/dist/js/modern/context.js +0 -63
  145. package/dist/js/modern/index.js +0 -169
  146. package/dist/js/modern/initWatcher.js +0 -62
  147. package/dist/js/modern/loadEnv.js +0 -14
  148. package/dist/js/modern/loadPlugins.js +0 -122
  149. package/dist/js/modern/manager.js +0 -28
  150. package/dist/js/modern/pluginAPI.js +0 -11
  151. package/dist/js/modern/utils/commander.js +0 -19
  152. package/dist/js/modern/utils/repeatKeyWarning.js +0 -18
  153. package/dist/js/node/cli.js +0 -35
  154. package/dist/js/node/config/defaults.js +0 -110
  155. package/dist/js/node/config/index.js +0 -182
  156. package/dist/js/node/config/mergeConfig.js +0 -32
  157. package/dist/js/node/config/schema/deploy.js +0 -26
  158. package/dist/js/node/config/schema/index.js +0 -127
  159. package/dist/js/node/config/schema/output.js +0 -156
  160. package/dist/js/node/config/schema/server.js +0 -179
  161. package/dist/js/node/config/schema/source.js +0 -68
  162. package/dist/js/node/config/schema/tools.js +0 -43
  163. package/dist/js/node/config/types/electron.js +0 -5
  164. package/dist/js/node/config/types/index.js +0 -5
  165. package/dist/js/node/config/types/less.js +0 -0
  166. package/dist/js/node/config/types/sass.js +0 -0
  167. package/dist/js/node/config/types/ssg.js +0 -0
  168. package/dist/js/node/config/types/test.js +0 -0
  169. package/dist/js/node/config/types/unbundle.js +0 -0
  170. package/dist/js/node/context.js +0 -93
  171. package/dist/js/node/index.js +0 -329
  172. package/dist/js/node/initWatcher.js +0 -82
  173. package/dist/js/node/loadEnv.js +0 -30
  174. package/dist/js/node/loadPlugins.js +0 -134
  175. package/dist/js/node/manager.js +0 -45
  176. package/dist/js/node/pluginAPI.js +0 -54
  177. package/dist/js/node/utils/commander.js +0 -32
  178. package/dist/js/node/utils/repeatKeyWarning.js +0 -31
  179. package/dist/types/cli.d.ts +0 -1
  180. package/dist/types/config/defaults.d.ts +0 -28
  181. package/dist/types/config/mergeConfig.d.ts +0 -31
  182. package/dist/types/config/schema/deploy.d.ts +0 -16
  183. package/dist/types/config/schema/index.d.ts +0 -466
  184. package/dist/types/config/schema/output.d.ts +0 -146
  185. package/dist/types/config/schema/server.d.ts +0 -182
  186. package/dist/types/config/schema/source.d.ts +0 -58
  187. package/dist/types/config/schema/tools.d.ts +0 -36
  188. package/dist/types/config/types/electron.d.ts +0 -13
  189. package/dist/types/config/types/index.d.ts +0 -252
  190. package/dist/types/config/types/less.d.ts +0 -10
  191. package/dist/types/config/types/sass.d.ts +0 -8
  192. package/dist/types/config/types/test.d.ts +0 -15
  193. package/dist/types/config/types/unbundle.d.ts +0 -28
  194. package/dist/types/index.d.ts +0 -64
  195. package/dist/types/manager.d.ts +0 -75
  196. package/modern.config.js +0 -6
@@ -1,170 +0,0 @@
1
- import { ENTRY_NAME_PATTERN } from '@modern-js/utils';
2
- const SERVER_ROUTE_OBJECT = {
3
- type: 'object',
4
- properties: {
5
- path: {
6
- type: 'string'
7
- },
8
- headers: {
9
- type: 'object'
10
- }
11
- },
12
- additionalProperties: false
13
- };
14
- export const server = {
15
- type: 'object',
16
- additionalProperties: false,
17
- properties: {
18
- port: {
19
- type: 'number'
20
- },
21
- ssr: {
22
- if: {
23
- type: 'object'
24
- },
25
- then: {
26
- properties: {
27
- disableLoadable: {
28
- type: 'boolean'
29
- },
30
- disableHelmet: {
31
- type: 'boolean'
32
- },
33
- disableRedirect: {
34
- type: 'boolean'
35
- },
36
- enableAsyncData: {
37
- type: 'boolean'
38
- },
39
- enableProductWarning: {
40
- type: 'boolean'
41
- },
42
- timeout: {
43
- type: 'number'
44
- },
45
- asyncDataTimeout: {
46
- type: 'number'
47
- }
48
- }
49
- },
50
- else: {
51
- type: 'boolean'
52
- }
53
- },
54
- ssrByEntries: {
55
- type: 'object',
56
- patternProperties: {
57
- [ENTRY_NAME_PATTERN]: {
58
- if: {
59
- type: 'object'
60
- },
61
- then: {
62
- properties: {
63
- disableLoadable: {
64
- type: 'boolean'
65
- },
66
- disableHelmet: {
67
- type: 'boolean'
68
- },
69
- disableRedirect: {
70
- type: 'boolean'
71
- },
72
- enableProductWarning: {
73
- type: 'boolean'
74
- },
75
- enableAsyncData: {
76
- type: 'boolean'
77
- },
78
- timeout: {
79
- type: 'number'
80
- },
81
- asyncDataTimeout: {
82
- type: 'number'
83
- }
84
- },
85
- additionalProperties: false
86
- },
87
- else: {
88
- type: 'boolean'
89
- }
90
- }
91
- }
92
- },
93
- routes: {
94
- type: 'object',
95
- patternProperties: {
96
- [ENTRY_NAME_PATTERN]: {
97
- if: {
98
- type: 'object'
99
- },
100
- then: {
101
- properties: {
102
- route: {
103
- oneOf: [{
104
- type: 'string'
105
- }, {
106
- type: 'array',
107
- items: {
108
- oneOf: [{
109
- type: 'string'
110
- }, SERVER_ROUTE_OBJECT]
111
- }
112
- }, SERVER_ROUTE_OBJECT]
113
- },
114
- disableSpa: {
115
- type: 'boolean'
116
- }
117
- },
118
- additionalProperties: false
119
- },
120
- else: {
121
- oneOf: [{
122
- type: 'string'
123
- }, {
124
- type: 'array',
125
- items: {
126
- type: 'string'
127
- }
128
- }]
129
- }
130
- }
131
- }
132
- },
133
- publicRoutes: {
134
- type: 'object',
135
- patternProperties: {
136
- [ENTRY_NAME_PATTERN]: {
137
- type: ['string']
138
- }
139
- }
140
- },
141
- baseUrl: {
142
- oneOf: [{
143
- type: 'string'
144
- }, {
145
- type: 'array',
146
- items: [{
147
- type: 'string'
148
- }]
149
- }]
150
- },
151
- middleware: {
152
- instanceof: ['Array', 'Function']
153
- },
154
- renderHook: {
155
- instanceof: 'Function'
156
- },
157
- logger: {
158
- type: ['object', 'boolean']
159
- },
160
- metrics: {
161
- type: ['object', 'boolean']
162
- },
163
- proxy: {
164
- type: 'object'
165
- },
166
- enableMicroFrontendDebug: {
167
- type: 'boolean'
168
- }
169
- }
170
- };
@@ -1,59 +0,0 @@
1
- import { ENTRY_NAME_PATTERN } from '@modern-js/utils';
2
- export const source = {
3
- type: 'object',
4
- additionalProperties: false,
5
- properties: {
6
- entries: {
7
- type: 'object',
8
- patternProperties: {
9
- [ENTRY_NAME_PATTERN]: {
10
- if: {
11
- type: 'object'
12
- },
13
- then: {
14
- required: ['entry'],
15
- properties: {
16
- entry: {
17
- type: ['string', 'array']
18
- },
19
- disableMount: {
20
- type: 'boolean'
21
- },
22
- enableFileSystemRoutes: {
23
- type: 'boolean'
24
- }
25
- },
26
- additionalProperties: false
27
- },
28
- else: {
29
- type: ['string', 'array']
30
- }
31
- }
32
- }
33
- },
34
- alias: {
35
- typeof: ['object', 'function']
36
- },
37
- disableDefaultEntries: {
38
- type: 'boolean'
39
- },
40
- envVars: {
41
- type: 'array'
42
- },
43
- globalVars: {
44
- type: 'object'
45
- },
46
- moduleScopes: {
47
- instanceof: ['Array', 'Function']
48
- },
49
- entriesDir: {
50
- type: 'string'
51
- },
52
- configDir: {
53
- type: 'string'
54
- },
55
- include: {
56
- type: ['array']
57
- }
58
- }
59
- };
@@ -1,36 +0,0 @@
1
- export const tools = {
2
- type: 'object',
3
- additionalProperties: false,
4
- properties: {
5
- webpack: {
6
- typeof: ['object', 'function']
7
- },
8
- babel: {
9
- typeof: ['object', 'function']
10
- },
11
- postcss: {
12
- typeof: ['object', 'function']
13
- },
14
- lodash: {
15
- typeof: ['object', 'function']
16
- },
17
- devServer: {
18
- type: 'object'
19
- },
20
- tsLoader: {
21
- typeof: ['object', 'function']
22
- },
23
- autoprefixer: {
24
- typeof: ['object', 'function']
25
- },
26
- terser: {
27
- typeof: ['object', 'function']
28
- },
29
- minifyCss: {
30
- typeof: ['object', 'function']
31
- },
32
- styledComponents: {
33
- typeof: ['object', 'function']
34
- }
35
- }
36
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,63 +0,0 @@
1
- import path from 'path';
2
- import { createContext } from '@modern-js/plugin';
3
- import address from 'address';
4
- export const AppContext = createContext({});
5
- export const ConfigContext = createContext({});
6
- export const ResolvedConfigContext = createContext({});
7
- /**
8
- * Set app context.
9
- * @param value new app context. It will override previous app context.
10
- */
11
-
12
- export const setAppContext = value => AppContext.set(value);
13
- /**
14
- * Get app context, including directories, plugins and some static infos.
15
- */
16
-
17
- export const useAppContext = () => AppContext.use().value;
18
- /**
19
- * Get original content of user config.
20
- */
21
-
22
- export const useConfigContext = () => ConfigContext.use().value;
23
- /**
24
- * Get normalized content of user config.
25
- */
26
-
27
- export const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
28
- export const initAppContext = ({
29
- appDirectory,
30
- plugins,
31
- configFile,
32
- options,
33
- serverConfigFile
34
- }) => {
35
- const {
36
- metaName = 'modern-js',
37
- srcDir = 'src',
38
- distDir = '',
39
- sharedDir = 'shared'
40
- } = options || {};
41
- return {
42
- metaName,
43
- appDirectory,
44
- configFile,
45
- serverConfigFile,
46
- ip: address.ip(),
47
- port: 0,
48
- packageName: require(path.resolve(appDirectory, './package.json')).name,
49
- srcDirectory: path.resolve(appDirectory, srcDir),
50
- distDirectory: distDir,
51
- sharedDirectory: path.resolve(appDirectory, sharedDir),
52
- nodeModulesDirectory: path.resolve(appDirectory, './node_modules'),
53
- internalDirectory: path.resolve(appDirectory, `./node_modules/.${metaName}`),
54
- plugins,
55
- htmlTemplates: {},
56
- serverRoutes: [],
57
- entrypoints: [],
58
- checkedEntries: [],
59
- apiOnly: false,
60
- internalDirAlias: `@_${metaName.replace(/-/g, '_')}_internal`,
61
- internalSrcAlias: `@_${metaName.replace(/-/g, '_')}_src`
62
- };
63
- };
@@ -1,169 +0,0 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
-
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
-
5
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
-
7
- import path from 'path';
8
- import { pkgUp, program, ensureAbsolutePath, logger, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
9
- import { enable } from '@modern-js/plugin/node';
10
- import { initCommandsMap } from "./utils/commander";
11
- import { resolveConfig, loadUserConfig, addServerConfigToDeps } from "./config";
12
- import { loadPlugins } from "./loadPlugins";
13
- import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext } from "./context";
14
- import { initWatcher } from "./initWatcher";
15
- import { loadEnv } from "./loadEnv";
16
- import { manager } from "./manager";
17
- export * from "./config";
18
- export * from '@modern-js/plugin';
19
- export * from '@modern-js/plugin/node'; // TODO: remove export after refactor all plugins
20
-
21
- export { manager, mountHook, usePlugins, createPlugin, registerHook } from "./manager";
22
- // TODO: remove export after refactor all plugins
23
- export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./pluginAPI";
24
- program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
25
-
26
- const initAppDir = async cwd => {
27
- if (!cwd) {
28
- // eslint-disable-next-line no-param-reassign
29
- cwd = process.cwd();
30
- }
31
-
32
- const pkg = await pkgUp({
33
- cwd
34
- });
35
-
36
- if (!pkg) {
37
- throw new Error(`no package.json found in current work dir: ${cwd}`);
38
- }
39
-
40
- return path.dirname(pkg);
41
- };
42
-
43
- export const mergeOptions = options => {
44
- const defaultOptions = {
45
- serverConfigFile: DEFAULT_SERVER_CONFIG
46
- };
47
- return _objectSpread(_objectSpread({}, defaultOptions), options);
48
- };
49
-
50
- const createCli = () => {
51
- let hooksRunner;
52
- let isRestart = false;
53
- let restartWithExistingPort = 0;
54
- let restartOptions;
55
-
56
- const init = async (argv = [], options) => {
57
- var _mergedOptions$option, _mergedOptions$option2;
58
-
59
- enable();
60
- manager.clear();
61
- const mergedOptions = mergeOptions(options);
62
- restartOptions = mergedOptions;
63
- const appDirectory = await initAppDir();
64
- initCommandsMap();
65
- const metaName = (_mergedOptions$option = mergedOptions === null || mergedOptions === void 0 ? void 0 : (_mergedOptions$option2 = mergedOptions.options) === null || _mergedOptions$option2 === void 0 ? void 0 : _mergedOptions$option2.metaName) !== null && _mergedOptions$option !== void 0 ? _mergedOptions$option : 'MODERN';
66
- loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
67
- const loaded = await loadUserConfig(appDirectory, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.configFile, mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.packageJsonConfig);
68
- const plugins = loadPlugins(appDirectory, loaded.config, {
69
- internalPlugins: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.plugins,
70
- transformPlugin: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.transformPlugin
71
- });
72
- plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
73
- const appContext = initAppContext({
74
- appDirectory,
75
- plugins,
76
- configFile: loaded.filePath,
77
- options: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.options,
78
- serverConfigFile: mergedOptions === null || mergedOptions === void 0 ? void 0 : mergedOptions.serverConfigFile
79
- }); // 将 server.config 加入到 loaded.dependencies,以便对文件监听做热更新
80
-
81
- addServerConfigToDeps(loaded.dependencies, appDirectory, mergedOptions.serverConfigFile);
82
- manager.run(() => {
83
- ConfigContext.set(loaded.config);
84
- AppContext.set(appContext);
85
- });
86
- hooksRunner = await manager.init();
87
- ['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException'].forEach(event => {
88
- process.on(event, async err => {
89
- await hooksRunner.beforeExit();
90
-
91
- if (err instanceof Error) {
92
- logger.error(err.stack);
93
- }
94
-
95
- process.nextTick(() => {
96
- // eslint-disable-next-line no-process-exit
97
- process.exit(1);
98
- });
99
- });
100
- });
101
- const extraConfigs = await hooksRunner.config();
102
- const extraSchemas = await hooksRunner.validateSchema();
103
- const config = await resolveConfig(loaded, extraConfigs, extraSchemas, restartWithExistingPort, argv, options === null || options === void 0 ? void 0 : options.onSchemaError);
104
- const {
105
- resolved
106
- } = await hooksRunner.resolvedConfig({
107
- resolved: config
108
- }); // update context value
109
-
110
- manager.run(() => {
111
- ConfigContext.set(loaded.config);
112
- ResolvedConfigContext.set(resolved);
113
- AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
114
- port: resolved.server.port,
115
- distDirectory: ensureAbsolutePath(appDirectory, resolved.output.path)
116
- }));
117
- });
118
- await hooksRunner.prepare();
119
- return {
120
- loadedConfig: loaded,
121
- appContext,
122
- resolved
123
- };
124
- };
125
-
126
- async function run(argv, options) {
127
- const {
128
- loadedConfig,
129
- appContext,
130
- resolved
131
- } = await init(argv, options);
132
- await hooksRunner.commands({
133
- program
134
- });
135
- initWatcher(loadedConfig, appContext.appDirectory, resolved.source.configDir, hooksRunner, argv);
136
- manager.run(() => program.parse(process.argv));
137
- }
138
-
139
- async function restart() {
140
- var _AppContext$use$value, _AppContext$use$value2;
141
-
142
- isRestart = true;
143
- restartWithExistingPort = isRestart ? (_AppContext$use$value = (_AppContext$use$value2 = AppContext.use().value) === null || _AppContext$use$value2 === void 0 ? void 0 : _AppContext$use$value2.port) !== null && _AppContext$use$value !== void 0 ? _AppContext$use$value : 0 : 0;
144
- logger.info('Restart...\n');
145
- let hasGetError = false;
146
- const runner = manager.useRunner();
147
- await runner.beforeRestart();
148
-
149
- try {
150
- await init(process.argv.slice(2), restartOptions);
151
- } catch (err) {
152
- console.error(err);
153
- hasGetError = true;
154
- } finally {
155
- if (!hasGetError) {
156
- manager.run(() => program.parse(process.argv));
157
- }
158
- }
159
- }
160
-
161
- return {
162
- init,
163
- run,
164
- restart
165
- };
166
- };
167
-
168
- export const cli = createCli();
169
- export { initAppDir, initAppContext };
@@ -1,62 +0,0 @@
1
- import crypto from 'crypto';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { isDev, chokidar, createDebugger, isTest } from '@modern-js/utils';
5
- const debug = createDebugger('watch-files');
6
-
7
- const md5 = data => crypto.createHash('md5').update(data).digest('hex');
8
-
9
- const hashMap = new Map();
10
- export const initWatcher = async (loaded, appDirectory, configDir, hooksRunner, argv) => {
11
- // only add fs watcher on dev mode.
12
- if ((isDev() || isTest()) && argv[0] === 'dev') {
13
- const extraFiles = await hooksRunner.watchFiles();
14
- const configPath = path.join(appDirectory, configDir);
15
- const watched = [`${configPath}/html`, ...extraFiles, loaded === null || loaded === void 0 ? void 0 : loaded.filePath, ...loaded.dependencies].filter(Boolean);
16
- debug(`watched: %o`, watched);
17
- const watcher = chokidar.watch(watched, {
18
- cwd: appDirectory,
19
- ignoreInitial: true,
20
- ignorePermissionErrors: true,
21
- ignored: [/node_modules/, '**/__test__/**', '**/*.test.(js|jsx|ts|tsx)', '**/*.spec.(js|jsx|ts|tsx)', '**/*.stories.(js|jsx|ts|tsx)']
22
- });
23
- watcher.on('change', changed => {
24
- const lastHash = hashMap.get(changed);
25
- const currentHash = md5(fs.readFileSync(path.join(appDirectory, changed), 'utf8'));
26
-
27
- if (currentHash !== lastHash) {
28
- debug(`file change: %s`, changed);
29
- hashMap.set(changed, currentHash);
30
- hooksRunner.fileChange({
31
- filename: changed,
32
- eventType: 'change'
33
- });
34
- }
35
- });
36
- watcher.on('add', name => {
37
- debug(`add file: %s`, name);
38
- const currentHash = md5(fs.readFileSync(path.join(appDirectory, name), 'utf8'));
39
- hashMap.set(name, currentHash);
40
- hooksRunner.fileChange({
41
- filename: name,
42
- eventType: 'add'
43
- });
44
- });
45
- watcher.on('unlink', name => {
46
- debug(`remove file: %s`, name);
47
-
48
- if (hashMap.has(name)) {
49
- hashMap.delete(name);
50
- }
51
-
52
- hooksRunner.fileChange({
53
- filename: name,
54
- eventType: 'unlink'
55
- });
56
- });
57
- watcher.on('error', err => {
58
- throw err;
59
- });
60
- return watcher;
61
- }
62
- };
@@ -1,14 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import dotenv from 'dotenv';
4
- import dotenvExpand from 'dotenv-expand';
5
- export const loadEnv = (appDirectory, mode = process.env.NODE_ENV) => {
6
- // Don't change the order of the filenames, since they are ordered by the priority.
7
- // Files on the left have more priority than files on the right.
8
- [`.env.${mode}.local`, '.env.local', `.env.${mode}`, '.env'].map(name => path.resolve(appDirectory, name)).filter(filePath => fs.existsSync(filePath) && !fs.statSync(filePath).isDirectory()).forEach(filePath => {
9
- const envConfig = dotenv.config({
10
- path: filePath
11
- });
12
- dotenvExpand(envConfig);
13
- });
14
- };