@modern-js/core 1.9.1 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/index.js +5 -1
- package/dist/config/schema/index.js +2 -3
- package/dist/index.js +5 -1
- package/dist/js/modern/cli.js +29 -0
- package/dist/js/modern/config/defaults.js +103 -0
- package/dist/js/modern/config/index.js +115 -0
- package/dist/js/modern/config/mergeConfig.js +22 -0
- package/dist/js/modern/config/schema/deploy.js +17 -0
- package/dist/js/modern/config/schema/index.js +107 -0
- package/dist/js/modern/config/schema/output.js +147 -0
- package/dist/js/modern/config/schema/server.js +170 -0
- package/dist/js/modern/config/schema/source.js +59 -0
- package/dist/js/modern/config/schema/tools.js +36 -0
- package/dist/js/modern/config/types/electron.js +1 -0
- package/dist/js/modern/config/types/index.js +1 -0
- package/dist/js/modern/config/types/less.js +0 -0
- package/dist/js/modern/config/types/sass.js +0 -0
- package/dist/js/modern/config/types/ssg.js +0 -0
- package/dist/js/modern/config/types/test.js +0 -0
- package/dist/js/modern/config/types/unbundle.js +0 -0
- package/dist/js/modern/context.js +63 -0
- package/dist/js/modern/index.js +169 -0
- package/dist/js/modern/initWatcher.js +62 -0
- package/dist/js/modern/loadEnv.js +14 -0
- package/dist/js/modern/loadPlugins.js +122 -0
- package/dist/js/modern/manager.js +28 -0
- package/dist/js/modern/pluginAPI.js +11 -0
- package/dist/js/modern/utils/commander.js +19 -0
- package/dist/js/modern/utils/repeatKeyWarning.js +18 -0
- package/dist/js/node/cli.js +35 -0
- package/dist/js/node/config/defaults.js +110 -0
- package/dist/js/node/config/index.js +182 -0
- package/dist/js/node/config/mergeConfig.js +32 -0
- package/dist/js/node/config/schema/deploy.js +26 -0
- package/dist/js/node/config/schema/index.js +127 -0
- package/dist/js/node/config/schema/output.js +156 -0
- package/dist/js/node/config/schema/server.js +179 -0
- package/dist/js/node/config/schema/source.js +68 -0
- package/dist/js/node/config/schema/tools.js +43 -0
- package/dist/js/node/config/types/electron.js +5 -0
- package/dist/js/node/config/types/index.js +5 -0
- package/dist/js/node/config/types/less.js +0 -0
- package/dist/js/node/config/types/sass.js +0 -0
- package/dist/js/node/config/types/ssg.js +0 -0
- package/dist/js/node/config/types/test.js +0 -0
- package/dist/js/node/config/types/unbundle.js +0 -0
- package/dist/js/node/context.js +93 -0
- package/dist/js/node/index.js +329 -0
- package/dist/js/node/initWatcher.js +82 -0
- package/dist/js/node/loadEnv.js +30 -0
- package/dist/js/node/loadPlugins.js +134 -0
- package/dist/js/node/manager.js +45 -0
- package/dist/js/node/pluginAPI.js +54 -0
- package/dist/js/node/utils/commander.js +32 -0
- package/dist/js/node/utils/repeatKeyWarning.js +31 -0
- package/dist/types/cli.d.ts +1 -0
- package/dist/types/config/defaults.d.ts +28 -0
- package/dist/types/config/index.d.ts +12 -0
- package/dist/types/config/mergeConfig.d.ts +31 -0
- package/dist/types/config/schema/deploy.d.ts +16 -0
- package/dist/types/config/schema/index.d.ts +466 -0
- package/dist/types/config/schema/output.d.ts +146 -0
- package/dist/types/config/schema/server.d.ts +182 -0
- package/dist/types/config/schema/source.d.ts +58 -0
- package/dist/types/config/schema/tools.d.ts +36 -0
- package/dist/types/config/types/electron.d.ts +13 -0
- package/dist/types/config/types/index.d.ts +252 -0
- package/dist/types/config/types/less.d.ts +10 -0
- package/dist/types/config/types/sass.d.ts +8 -0
- package/dist/types/config/types/ssg.d.ts +13 -0
- package/dist/types/config/types/test.d.ts +15 -0
- package/dist/types/config/types/unbundle.d.ts +28 -0
- package/dist/types/context.d.ts +47 -0
- package/dist/types/index.d.ts +64 -0
- package/dist/types/initWatcher.d.ts +3 -0
- package/dist/types/loadEnv.d.ts +1 -0
- package/dist/types/loadPlugins.d.ts +44 -0
- package/dist/types/manager.d.ts +75 -0
- package/dist/types/pluginAPI.d.ts +13 -0
- package/dist/types/utils/commander.d.ts +4 -0
- package/dist/types/utils/repeatKeyWarning.d.ts +3 -0
- package/package.json +4 -4
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
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 };
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
};
|