@modern-js/core 1.0.0-alpha.3
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/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/modern-js.js +18 -0
- package/dist/js/modern/config/defaults.js +102 -0
- package/dist/js/modern/config/index.js +104 -0
- package/dist/js/modern/config/mergeConfig.js +20 -0
- package/dist/js/modern/config/schema/deploy.js +34 -0
- package/dist/js/modern/config/schema/index.js +104 -0
- package/dist/js/modern/config/schema/output.js +147 -0
- package/dist/js/modern/config/schema/server.js +167 -0
- package/dist/js/modern/config/schema/source.js +59 -0
- package/dist/js/modern/config/schema/tools.js +33 -0
- package/dist/js/modern/context.js +25 -0
- package/dist/js/modern/index.js +137 -0
- package/dist/js/modern/initWatcher.js +51 -0
- package/dist/js/modern/loadEnv.js +12 -0
- package/dist/js/modern/loadPlugins.js +66 -0
- package/dist/js/modern/utils/commander.js +19 -0
- package/dist/js/modern/utils/repeatKeyWarning.js +18 -0
- package/dist/js/node/config/defaults.js +109 -0
- package/dist/js/node/config/index.js +142 -0
- package/dist/js/node/config/mergeConfig.js +32 -0
- package/dist/js/node/config/schema/deploy.js +43 -0
- package/dist/js/node/config/schema/index.js +126 -0
- package/dist/js/node/config/schema/output.js +156 -0
- package/dist/js/node/config/schema/server.js +176 -0
- package/dist/js/node/config/schema/source.js +68 -0
- package/dist/js/node/config/schema/tools.js +40 -0
- package/dist/js/node/context.js +52 -0
- package/dist/js/node/index.js +212 -0
- package/dist/js/node/initWatcher.js +72 -0
- package/dist/js/node/loadEnv.js +28 -0
- package/dist/js/node/loadPlugins.js +76 -0
- package/dist/js/node/utils/commander.js +35 -0
- package/dist/js/node/utils/repeatKeyWarning.js +31 -0
- package/dist/types/config/defaults.d.ts +27 -0
- package/dist/types/config/index.d.ts +125 -0
- package/dist/types/config/mergeConfig.d.ts +29 -0
- package/dist/types/config/schema/deploy.d.ts +33 -0
- package/dist/types/config/schema/index.d.ts +474 -0
- package/dist/types/config/schema/output.d.ts +146 -0
- package/dist/types/config/schema/server.d.ts +179 -0
- package/dist/types/config/schema/source.d.ts +58 -0
- package/dist/types/config/schema/tools.d.ts +33 -0
- package/dist/types/context.d.ts +20 -0
- package/dist/types/index.d.ts +86 -0
- package/dist/types/initWatcher.d.ts +4 -0
- package/dist/types/loadEnv.d.ts +1 -0
- package/dist/types/loadPlugins.d.ts +16 -0
- package/dist/types/utils/commander.d.ts +7 -0
- package/dist/types/utils/repeatKeyWarning.d.ts +3 -0
- package/modern.config.js +13 -0
- package/package.json +73 -0
- package/src/config/defaults.ts +104 -0
- package/src/config/index.ts +296 -0
- package/src/config/mergeConfig.ts +68 -0
- package/src/config/schema/deploy.ts +23 -0
- package/src/config/schema/index.ts +111 -0
- package/src/config/schema/output.ts +66 -0
- package/src/config/schema/server.ts +105 -0
- package/src/config/schema/source.ts +34 -0
- package/src/config/schema/tools.ts +15 -0
- package/src/context.ts +46 -0
- package/src/index.ts +240 -0
- package/src/initWatcher.ts +81 -0
- package/src/loadEnv.ts +21 -0
- package/src/loadPlugins.ts +81 -0
- package/src/types.d.ts +0 -0
- package/src/utils/commander.ts +22 -0
- package/src/utils/repeatKeyWarning.ts +29 -0
- package/tests/fixtures/load-plugin/not-found/package.json +3 -0
- package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +1 -0
- package/tests/fixtures/load-plugin/user-plugins/package.json +3 -0
- package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +1 -0
- package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +3 -0
- package/tests/loadEnv.test.ts +100 -0
- package/tests/loadPlugin.test.ts +29 -0
- package/tests/mergeConfig.test.ts +78 -0
- package/tests/repeatKeyWarning.test.ts +68 -0
- package/tests/schema.test.ts +109 -0
- package/tests/tsconfig.json +13 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,167 @@
|
|
|
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'
|
|
159
|
+
},
|
|
160
|
+
measure: {
|
|
161
|
+
type: 'object'
|
|
162
|
+
},
|
|
163
|
+
proxy: {
|
|
164
|
+
type: 'object'
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
@@ -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,33 @@
|
|
|
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
|
+
}
|
|
33
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export const useAppContext = () => AppContext.use();
|
|
8
|
+
export const useConfigContext = () => ConfigContext.use();
|
|
9
|
+
export const useResolvedConfigContext = () => ResolvedConfigContext.use();
|
|
10
|
+
export const initAppContext = (appDirectory, plugins, configFile) => ({
|
|
11
|
+
appDirectory,
|
|
12
|
+
configFile,
|
|
13
|
+
ip: address.ip(),
|
|
14
|
+
port: 0,
|
|
15
|
+
packageName: require(path.resolve(appDirectory, './package.json')).name,
|
|
16
|
+
srcDirectory: path.resolve(appDirectory, './src'),
|
|
17
|
+
distDirectory: '',
|
|
18
|
+
sharedDirectory: path.resolve(appDirectory, './shared'),
|
|
19
|
+
nodeModulesDirectory: path.resolve(appDirectory, './node_modules'),
|
|
20
|
+
internalDirectory: path.resolve(appDirectory, './node_modules/.modern-js'),
|
|
21
|
+
plugins,
|
|
22
|
+
htmlTemplates: {},
|
|
23
|
+
serverRoutes: [],
|
|
24
|
+
entrypoints: []
|
|
25
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { createAsyncManager, createContext, createAsyncWorkflow, createParallelWorkflow } from '@modern-js/plugin';
|
|
9
|
+
import { enable } from '@modern-js/plugin/node';
|
|
10
|
+
import { compatRequire, pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
|
|
11
|
+
import { program } from "./utils/commander";
|
|
12
|
+
import { resolveConfig, defineConfig, loadUserConfig } from "./config";
|
|
13
|
+
import { loadPlugins } from "./loadPlugins";
|
|
14
|
+
import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./context";
|
|
15
|
+
import { initWatcher } from "./initWatcher";
|
|
16
|
+
import { loadEnv } from "./loadEnv";
|
|
17
|
+
export { defaultsConfig } from "./config";
|
|
18
|
+
program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
|
|
19
|
+
const hooksMap = {
|
|
20
|
+
config: createParallelWorkflow(),
|
|
21
|
+
validateSchema: createParallelWorkflow(),
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
23
|
+
prepare: createAsyncWorkflow(),
|
|
24
|
+
commands: createAsyncWorkflow(),
|
|
25
|
+
watchFiles: createParallelWorkflow(),
|
|
26
|
+
fileChange: createAsyncWorkflow(),
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
28
|
+
beforeExit: createAsyncWorkflow()
|
|
29
|
+
};
|
|
30
|
+
export const manager = createAsyncManager(hooksMap);
|
|
31
|
+
export const {
|
|
32
|
+
createPlugin,
|
|
33
|
+
registe: registerHook,
|
|
34
|
+
useRunner: mountHook
|
|
35
|
+
} = manager;
|
|
36
|
+
export const usePlugins = plugins => plugins.forEach(plugin => manager.usePlugin(compatRequire(require.resolve(plugin))));
|
|
37
|
+
export { defineConfig, createContext, AppContext, useAppContext, useConfigContext, useResolvedConfigContext };
|
|
38
|
+
|
|
39
|
+
const initAppDir = async () => {
|
|
40
|
+
const pkg = await pkgUp({
|
|
41
|
+
cwd: process.cwd()
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (!pkg) {
|
|
45
|
+
throw new Error(`no package.json found in current work dir: ${process.cwd()}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return path.dirname(pkg);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const createCli = () => {
|
|
52
|
+
let hooksRunner;
|
|
53
|
+
let isRestart = false;
|
|
54
|
+
|
|
55
|
+
const init = async (argv = []) => {
|
|
56
|
+
manager.clear();
|
|
57
|
+
const appDirectory = await initAppDir();
|
|
58
|
+
loadEnv(appDirectory);
|
|
59
|
+
const loaded = await loadUserConfig(appDirectory);
|
|
60
|
+
const plugins = loadPlugins(appDirectory, loaded.config.plugins || []);
|
|
61
|
+
plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
|
|
62
|
+
const appContext = initAppContext(appDirectory, plugins, loaded.filePath);
|
|
63
|
+
manager.run(() => {
|
|
64
|
+
ConfigContext.set(loaded.config);
|
|
65
|
+
AppContext.set(appContext);
|
|
66
|
+
});
|
|
67
|
+
hooksRunner = await manager.init();
|
|
68
|
+
['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException'].forEach(event => {
|
|
69
|
+
process.on(event, async err => {
|
|
70
|
+
await hooksRunner.beforeExit();
|
|
71
|
+
|
|
72
|
+
if (err instanceof Error) {
|
|
73
|
+
logger.error(err.stack);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
process.nextTick(() => {
|
|
77
|
+
// eslint-disable-next-line no-process-exit
|
|
78
|
+
process.exit(1);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
const extraConfigs = await hooksRunner.config();
|
|
83
|
+
const extraSchemas = await hooksRunner.validateSchema();
|
|
84
|
+
const resolved = await resolveConfig(loaded, extraConfigs, extraSchemas, isRestart, argv); // update context value
|
|
85
|
+
|
|
86
|
+
manager.run(() => {
|
|
87
|
+
ConfigContext.set(loaded.config);
|
|
88
|
+
ResolvedConfigContext.set(resolved);
|
|
89
|
+
AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
90
|
+
port: resolved.server.port,
|
|
91
|
+
distDirectory: ensureAbsolutePath(appDirectory, resolved.output.path)
|
|
92
|
+
}));
|
|
93
|
+
});
|
|
94
|
+
await hooksRunner.prepare();
|
|
95
|
+
return {
|
|
96
|
+
loadedConfig: loaded,
|
|
97
|
+
appContext,
|
|
98
|
+
resolved
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
async function run(argv) {
|
|
103
|
+
enable();
|
|
104
|
+
const {
|
|
105
|
+
loadedConfig,
|
|
106
|
+
appContext,
|
|
107
|
+
resolved
|
|
108
|
+
} = await init(argv);
|
|
109
|
+
initWatcher(loadedConfig, appContext.appDirectory, resolved, hooksRunner, argv);
|
|
110
|
+
await hooksRunner.commands({
|
|
111
|
+
program
|
|
112
|
+
});
|
|
113
|
+
manager.run(() => program.parse(process.argv));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function restart() {
|
|
117
|
+
isRestart = true;
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
logger.info('Restart...\n');
|
|
121
|
+
await init();
|
|
122
|
+
return true;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
logger.error(err);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
init,
|
|
132
|
+
run,
|
|
133
|
+
restart
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const cli = createCli();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import { isDev, createDebugger } from '@modern-js/utils';
|
|
6
|
+
const debug = createDebugger('watch-files');
|
|
7
|
+
|
|
8
|
+
const md5 = data => crypto.createHash('md5').update(data).digest('hex');
|
|
9
|
+
|
|
10
|
+
const hashMap = new Map();
|
|
11
|
+
export const initWatcher = async (loaded, appDirectory, resovledConfig, hooksRunner, argv) => {
|
|
12
|
+
// only add fs watcher on dev mode.
|
|
13
|
+
if (isDev() && argv[0] === 'dev') {
|
|
14
|
+
const {
|
|
15
|
+
source: {
|
|
16
|
+
configDir
|
|
17
|
+
}
|
|
18
|
+
} = resovledConfig;
|
|
19
|
+
const extraFiles = await hooksRunner.watchFiles();
|
|
20
|
+
const configPath = path.join(appDirectory, configDir);
|
|
21
|
+
const watched = [`${configPath}/html`, ...extraFiles, loaded === null || loaded === void 0 ? void 0 : loaded.filePath, ...loaded.dependencies].filter(Boolean);
|
|
22
|
+
debug(`watched: %o`, watched);
|
|
23
|
+
const watcher = chokidar.watch(watched, {
|
|
24
|
+
cwd: appDirectory,
|
|
25
|
+
ignorePermissionErrors: true,
|
|
26
|
+
ignored: [/node_modules/, '**/__test__/**', '**/*.test.(js|jsx|ts|tsx)', '**/*.spec.(js|jsx|ts|tsx)', '**/*.stories.(js|jsx|ts|tsx)']
|
|
27
|
+
});
|
|
28
|
+
watcher.on('change', changed => {
|
|
29
|
+
const lastHash = hashMap.get(changed);
|
|
30
|
+
const currentHash = md5(fs.readFileSync(path.join(appDirectory, changed), 'utf8'));
|
|
31
|
+
|
|
32
|
+
if (currentHash !== lastHash) {
|
|
33
|
+
debug(`file change: %s`, changed);
|
|
34
|
+
hashMap.set(changed, currentHash);
|
|
35
|
+
hooksRunner.fileChange({
|
|
36
|
+
filename: changed
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
watcher.on('unlink', name => {
|
|
41
|
+
debug(`remove file: %s`, name);
|
|
42
|
+
|
|
43
|
+
if (hashMap.has(name)) {
|
|
44
|
+
hashMap.delete(name);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
watcher.on('error', err => {
|
|
48
|
+
throw err;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import dotenv from 'dotenv';
|
|
4
|
+
import dotenvExpand from 'dotenv-expand';
|
|
5
|
+
export const loadEnv = (appDirectory, mode = process.env.NODE_ENV) => {
|
|
6
|
+
[`.env.${mode}`, '.env'].map(name => path.resolve(appDirectory, name)).filter(filePath => fs.existsSync(filePath) && !fs.statSync(filePath).isDirectory()).forEach(filePath => {
|
|
7
|
+
const envConfig = dotenv.config({
|
|
8
|
+
path: filePath
|
|
9
|
+
});
|
|
10
|
+
dotenvExpand(envConfig);
|
|
11
|
+
});
|
|
12
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isDepExists, createDebugger, compatRequire, INTERNAL_PLUGINS } from '@modern-js/utils';
|
|
2
|
+
const debug = createDebugger('load-plugins');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Try to resolve plugin entry file path.
|
|
6
|
+
* @param appDirectory - Application root directory.
|
|
7
|
+
* @param plugin - Plugin name or plugin name with options.
|
|
8
|
+
* @returns Resolved file path.
|
|
9
|
+
*/
|
|
10
|
+
const resolvePlugin = (appDirectory, plugin) => {
|
|
11
|
+
const tryResolve = name => {
|
|
12
|
+
let filePath = '';
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
filePath = require.resolve(name, {
|
|
16
|
+
paths: [appDirectory]
|
|
17
|
+
});
|
|
18
|
+
delete require.cache[filePath];
|
|
19
|
+
} catch (err) {
|
|
20
|
+
if (err.code === 'MODULE_NOT_FOUND') {
|
|
21
|
+
throw new Error(`Can not find plugin ${name}.`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return filePath;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const resolved = {};
|
|
31
|
+
|
|
32
|
+
if (plugin.cli) {
|
|
33
|
+
resolved.cli = tryResolve(plugin.cli);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (plugin.server) {
|
|
37
|
+
resolved.server = tryResolve(plugin.server);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return resolved;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Load internal plugins which in @modern-js scope and user's custom plugins.
|
|
44
|
+
* @param appDirectory - Application root directory.
|
|
45
|
+
* @param pluginsConfig - Plugins declared in the user configuration.
|
|
46
|
+
* @returns Plugin Objects has been required.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
export const loadPlugins = (appDirectory, pluginConfig) => {
|
|
51
|
+
const plugins = [...Object.keys(INTERNAL_PLUGINS).filter(name => isDepExists(appDirectory, name)).map(name => INTERNAL_PLUGINS[name]), ...pluginConfig];
|
|
52
|
+
return plugins.map(plugin => {
|
|
53
|
+
const {
|
|
54
|
+
cli,
|
|
55
|
+
server
|
|
56
|
+
} = resolvePlugin(appDirectory, plugin);
|
|
57
|
+
debug(`resolve plugin %s: %s`, plugin, {
|
|
58
|
+
cli,
|
|
59
|
+
server
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
cli: cli && compatRequire(cli),
|
|
63
|
+
server: server && compatRequire(server)
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { program, Command } from 'commander';
|
|
2
|
+
|
|
3
|
+
if (!program.hasOwnProperty('commandsMap')) {
|
|
4
|
+
Object.defineProperty(program, 'commandsMap', {
|
|
5
|
+
get() {
|
|
6
|
+
const map = new Map();
|
|
7
|
+
|
|
8
|
+
for (const command of program.commands) {
|
|
9
|
+
map.set(command._name, command);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return map;
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
configurable: false
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { program, Command };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { traverseSchema } from "../config/schema";
|
|
2
|
+
export const deepGet = (obj, key) => {
|
|
3
|
+
for (const p of key.split('.')) {
|
|
4
|
+
// eslint-disable-next-line no-param-reassign
|
|
5
|
+
obj = obj ? obj[p] : undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return obj;
|
|
9
|
+
};
|
|
10
|
+
export const repeatKeyWarning = (schema, jsConfig, pkgConfig) => {
|
|
11
|
+
const keys = traverseSchema(schema);
|
|
12
|
+
|
|
13
|
+
for (const key of keys) {
|
|
14
|
+
if (deepGet(jsConfig, key) !== undefined && deepGet(pkgConfig, key) !== undefined) {
|
|
15
|
+
throw new Error(`The same configuration ${key} exists in modern.config.js and package.json.\n Please remove it from package.json.`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.defaults = void 0;
|
|
7
|
+
const sourceDefaults = {
|
|
8
|
+
entries: undefined,
|
|
9
|
+
disableDefaultEntries: false,
|
|
10
|
+
entriesDir: './src',
|
|
11
|
+
configDir: './config',
|
|
12
|
+
apiDir: './api',
|
|
13
|
+
envVars: [],
|
|
14
|
+
globalVars: undefined,
|
|
15
|
+
alias: undefined,
|
|
16
|
+
moduleScopes: undefined,
|
|
17
|
+
include: []
|
|
18
|
+
};
|
|
19
|
+
const outputDefaults = {
|
|
20
|
+
assetPrefix: '/',
|
|
21
|
+
htmlPath: 'html',
|
|
22
|
+
jsPath: 'static/js',
|
|
23
|
+
cssPath: 'static/css',
|
|
24
|
+
mediaPath: 'static/media',
|
|
25
|
+
path: 'dist',
|
|
26
|
+
title: '',
|
|
27
|
+
titleByEntries: undefined,
|
|
28
|
+
meta: {
|
|
29
|
+
charset: {
|
|
30
|
+
charset: 'utf-8'
|
|
31
|
+
},
|
|
32
|
+
viewport: 'width=device-width, initial-scale=1.0, shrink-to-fit=no, viewport-fit=cover, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no',
|
|
33
|
+
'http-equiv': {
|
|
34
|
+
'http-equiv': 'x-ua-compatible',
|
|
35
|
+
content: 'ie=edge'
|
|
36
|
+
},
|
|
37
|
+
renderer: 'webkit',
|
|
38
|
+
layoutmode: 'standard',
|
|
39
|
+
imagemode: 'force',
|
|
40
|
+
'wap-font-scale': 'no',
|
|
41
|
+
'format-detection': 'telephone=no'
|
|
42
|
+
},
|
|
43
|
+
metaByEntries: undefined,
|
|
44
|
+
inject: 'head',
|
|
45
|
+
injectByEntries: undefined,
|
|
46
|
+
mountId: 'root',
|
|
47
|
+
favicon: '',
|
|
48
|
+
faviconByEntries: undefined,
|
|
49
|
+
copy: undefined,
|
|
50
|
+
scriptExt: undefined,
|
|
51
|
+
disableHtmlFolder: false,
|
|
52
|
+
disableCssModuleExtension: false,
|
|
53
|
+
disableCssExtract: false,
|
|
54
|
+
enableCssModuleTSDeclaration: false,
|
|
55
|
+
disableMinimize: false,
|
|
56
|
+
enableInlineStyles: false,
|
|
57
|
+
enableInlineScripts: false,
|
|
58
|
+
disableSourceMap: false,
|
|
59
|
+
disableInlineRuntimeChunk: false,
|
|
60
|
+
disableAssetsCache: false,
|
|
61
|
+
enableLatestDecorators: false,
|
|
62
|
+
polyfill: 'entry',
|
|
63
|
+
dataUriLimit: 10000,
|
|
64
|
+
templateParameters: {},
|
|
65
|
+
templateParametersByEntries: undefined,
|
|
66
|
+
cssModuleLocalIdentName: '[name]__[local]--[hash:base64:5]',
|
|
67
|
+
enableModernMode: false,
|
|
68
|
+
federation: undefined,
|
|
69
|
+
disableNodePolyfill: false,
|
|
70
|
+
enableTsLoader: false
|
|
71
|
+
};
|
|
72
|
+
const serverDefaults = {
|
|
73
|
+
routes: undefined,
|
|
74
|
+
publicRoutes: undefined,
|
|
75
|
+
ssr: undefined,
|
|
76
|
+
ssrByEntries: undefined,
|
|
77
|
+
baseUrl: '/',
|
|
78
|
+
port: 8080
|
|
79
|
+
};
|
|
80
|
+
const devDefaults = {
|
|
81
|
+
assetPrefix: false
|
|
82
|
+
};
|
|
83
|
+
const deployDefaults = {
|
|
84
|
+
microFrontend: {
|
|
85
|
+
enableHtmlEntry: false
|
|
86
|
+
},
|
|
87
|
+
domain: '',
|
|
88
|
+
domainByEntries: undefined
|
|
89
|
+
};
|
|
90
|
+
const toolsDefaults = {
|
|
91
|
+
webpack: undefined,
|
|
92
|
+
babel: undefined,
|
|
93
|
+
postcss: undefined,
|
|
94
|
+
autoprefixer: undefined,
|
|
95
|
+
lodash: undefined,
|
|
96
|
+
devServer: undefined,
|
|
97
|
+
tsLoader: undefined,
|
|
98
|
+
terser: undefined,
|
|
99
|
+
minifyCss: undefined
|
|
100
|
+
};
|
|
101
|
+
const defaults = {
|
|
102
|
+
source: sourceDefaults,
|
|
103
|
+
output: outputDefaults,
|
|
104
|
+
server: serverDefaults,
|
|
105
|
+
dev: devDefaults,
|
|
106
|
+
deploy: deployDefaults,
|
|
107
|
+
tools: toolsDefaults
|
|
108
|
+
};
|
|
109
|
+
exports.defaults = defaults;
|