@modern-js/app-tools 2.0.0-beta.0 → 2.0.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.
- package/CHANGELOG.md +181 -0
- package/dist/js/modern/analyze/generateCode.js +29 -37
- package/dist/js/modern/analyze/getBundleEntry.js +4 -12
- package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +5 -30
- package/dist/js/modern/analyze/getClientRoutes/getRoutesLegacy.js +5 -30
- package/dist/js/modern/analyze/getClientRoutes/utils.js +0 -3
- package/dist/js/modern/analyze/getFileSystemEntry.js +4 -17
- package/dist/js/modern/analyze/getHtmlTemplate.js +2 -9
- package/dist/js/modern/analyze/getServerRoutes.js +15 -32
- package/dist/js/modern/analyze/index.js +79 -22
- package/dist/js/modern/analyze/isDefaultExportFunction.js +0 -4
- package/dist/js/modern/analyze/makeLegalIdentifier.js +0 -2
- package/dist/js/modern/analyze/nestedRoutes.js +8 -33
- package/dist/js/modern/analyze/templates.js +22 -37
- package/dist/js/modern/analyze/utils.js +0 -7
- package/dist/js/modern/builder/builderPlugins/compatModern.js +30 -48
- package/dist/js/modern/builder/createHtmlConfig.js +2 -2
- package/dist/js/modern/builder/createOutputConfig.js +7 -4
- package/dist/js/modern/builder/createSourceConfig.js +0 -11
- package/dist/js/modern/builder/createToolsConfig.js +3 -10
- package/dist/js/modern/builder/index.js +24 -22
- package/dist/js/modern/builder/share.js +0 -4
- package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -8
- package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +2 -13
- package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
- package/dist/js/modern/commands/build.js +6 -46
- package/dist/js/modern/commands/dev.js +4 -16
- package/dist/js/modern/commands/inspect.js +3 -20
- package/dist/js/modern/commands/start.js +0 -2
- package/dist/js/modern/index.js +6 -15
- package/dist/js/modern/utils/config.js +1 -12
- package/dist/js/modern/utils/createServer.js +0 -5
- package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
- package/dist/js/modern/utils/printInstructions.js +2 -1
- package/dist/js/modern/utils/routes.js +0 -2
- package/dist/js/node/analyze/generateCode.js +29 -60
- package/dist/js/node/analyze/getBundleEntry.js +4 -18
- package/dist/js/node/analyze/getClientRoutes/getRoutes.js +5 -45
- package/dist/js/node/analyze/getClientRoutes/getRoutesLegacy.js +5 -45
- package/dist/js/node/analyze/getClientRoutes/index.js +0 -2
- package/dist/js/node/analyze/getClientRoutes/utils.js +0 -14
- package/dist/js/node/analyze/getFileSystemEntry.js +4 -25
- package/dist/js/node/analyze/getHtmlTemplate.js +2 -23
- package/dist/js/node/analyze/getServerRoutes.js +14 -40
- package/dist/js/node/analyze/index.js +79 -32
- package/dist/js/node/analyze/isDefaultExportFunction.js +0 -12
- package/dist/js/node/analyze/makeLegalIdentifier.js +0 -4
- package/dist/js/node/analyze/nestedRoutes.js +8 -41
- package/dist/js/node/analyze/templates.js +22 -47
- package/dist/js/node/analyze/utils.js +0 -20
- package/dist/js/node/builder/builderPlugins/compatModern.js +30 -59
- package/dist/js/node/builder/createHtmlConfig.js +2 -10
- package/dist/js/node/builder/createOutputConfig.js +7 -6
- package/dist/js/node/builder/createSourceConfig.js +0 -17
- package/dist/js/node/builder/createToolsConfig.js +3 -12
- package/dist/js/node/builder/index.js +24 -36
- package/dist/js/node/builder/share.js +0 -12
- package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +0 -10
- package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +2 -15
- package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
- package/dist/js/node/commands/build.js +4 -57
- package/dist/js/node/commands/deploy.js +0 -2
- package/dist/js/node/commands/dev.js +3 -27
- package/dist/js/node/commands/index.js +0 -6
- package/dist/js/node/commands/inspect.js +3 -27
- package/dist/js/node/commands/start.js +0 -11
- package/dist/js/node/exports/server.js +0 -1
- package/dist/js/node/hooks.js +0 -2
- package/dist/js/node/index.js +7 -34
- package/dist/js/node/locale/index.js +0 -4
- package/dist/js/node/utils/commands.js +0 -2
- package/dist/js/node/utils/config.js +1 -25
- package/dist/js/node/utils/createServer.js +0 -15
- package/dist/js/node/utils/getSpecifiedEntries.js +0 -9
- package/dist/js/node/utils/language.js +0 -2
- package/dist/js/node/utils/printInstructions.js +2 -5
- package/dist/js/node/utils/routes.js +0 -5
- package/dist/js/treeshaking/analyze/generateCode.js +57 -100
- package/dist/js/treeshaking/analyze/getBundleEntry.js +11 -19
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +16 -42
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +16 -42
- package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +0 -3
- package/dist/js/treeshaking/analyze/getFileSystemEntry.js +4 -17
- package/dist/js/treeshaking/analyze/getHtmlTemplate.js +2 -26
- package/dist/js/treeshaking/analyze/getServerRoutes.js +24 -37
- package/dist/js/treeshaking/analyze/index.js +185 -65
- package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +0 -4
- package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +0 -2
- package/dist/js/treeshaking/analyze/nestedRoutes.js +9 -51
- package/dist/js/treeshaking/analyze/templates.js +31 -50
- package/dist/js/treeshaking/analyze/utils.js +7 -14
- package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +40 -59
- package/dist/js/treeshaking/builder/createHtmlConfig.js +15 -15
- package/dist/js/treeshaking/builder/createOutputConfig.js +26 -23
- package/dist/js/treeshaking/builder/createSourceConfig.js +7 -19
- package/dist/js/treeshaking/builder/createToolsConfig.js +16 -21
- package/dist/js/treeshaking/builder/index.js +38 -49
- package/dist/js/treeshaking/builder/share.js +0 -4
- package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +1 -13
- package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +2 -14
- package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
- package/dist/js/treeshaking/commands/build.js +26 -111
- package/dist/js/treeshaking/commands/deploy.js +0 -3
- package/dist/js/treeshaking/commands/dev.js +13 -35
- package/dist/js/treeshaking/commands/inspect.js +5 -28
- package/dist/js/treeshaking/commands/start.js +0 -9
- package/dist/js/treeshaking/index.js +2 -39
- package/dist/js/treeshaking/utils/config.js +1 -17
- package/dist/js/treeshaking/utils/createServer.js +0 -10
- package/dist/js/treeshaking/utils/getSpecifiedEntries.js +0 -10
- package/dist/js/treeshaking/utils/printInstructions.js +0 -5
- package/dist/js/treeshaking/utils/routes.js +0 -4
- package/dist/types/analyze/index.d.ts +2 -3
- package/dist/types/analyze/nestedRoutes.d.ts +1 -1
- package/dist/types/analyze/templates.d.ts +3 -1
- package/dist/types/builder/builderPlugins/compatModern.d.ts +0 -1
- package/dist/types/builder/index.d.ts +2 -6
- package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
- package/dist/types/commands/inspect.d.ts +1 -4
- package/dist/types/index.d.ts +0 -2
- package/dist/types/utils/config.d.ts +0 -1
- package/package.json +23 -22
- package/dist/js/modern/utils/createCompiler.js +0 -58
- package/dist/js/node/utils/createCompiler.js +0 -75
- package/dist/js/treeshaking/utils/createCompiler.js +0 -117
- package/dist/types/utils/createCompiler.d.ts +0 -12
package/dist/js/modern/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
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
2
|
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
3
|
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
4
|
import path from 'path';
|
|
8
5
|
import { defineConfig, cli } from '@modern-js/core';
|
|
9
|
-
import LintPlugin from '@modern-js/plugin-
|
|
6
|
+
import LintPlugin from '@modern-js/plugin-lint';
|
|
10
7
|
import { cleanRequireCache, emptyDir, Import } from '@modern-js/utils';
|
|
11
8
|
import AnalyzePlugin from "./analyze";
|
|
12
9
|
import { hooks } from "./hooks";
|
|
@@ -17,7 +14,7 @@ export { defineConfig, hooks };
|
|
|
17
14
|
const upgradeModel = Import.lazy('@modern-js/upgrade', require);
|
|
18
15
|
export default (() => ({
|
|
19
16
|
name: '@modern-js/app-tools',
|
|
20
|
-
post: ['@modern-js/plugin-analyze', '@modern-js/plugin-ssr', '@modern-js/plugin-state', '@modern-js/plugin-router', '@modern-js/plugin-router-legacy', '@modern-js/plugin-polyfill'],
|
|
17
|
+
post: ['@modern-js/plugin-analyze', '@modern-js/plugin-ssr', '@modern-js/plugin-document', '@modern-js/plugin-state', '@modern-js/plugin-router', '@modern-js/plugin-router-legacy', '@modern-js/plugin-polyfill'],
|
|
21
18
|
registerHook: hooks,
|
|
22
19
|
usePlugins: [AnalyzePlugin(), LintPlugin()],
|
|
23
20
|
setup: api => {
|
|
@@ -39,9 +36,9 @@ export default (() => ({
|
|
|
39
36
|
const {
|
|
40
37
|
build
|
|
41
38
|
} = await import("./commands/build");
|
|
42
|
-
await build(api, options);
|
|
39
|
+
await build(api, options);
|
|
40
|
+
// force exit after build.
|
|
43
41
|
// eslint-disable-next-line no-process-exit
|
|
44
|
-
|
|
45
42
|
process.exit(0);
|
|
46
43
|
});
|
|
47
44
|
program.command('start').usage('[options]').description(i18n.t(localeKeys.command.start.describe)).option('--api-only', i18n.t(localeKeys.command.dev.apiOnly)).option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).action(async () => {
|
|
@@ -58,8 +55,8 @@ export default (() => ({
|
|
|
58
55
|
const {
|
|
59
56
|
deploy
|
|
60
57
|
} = await import("./commands/deploy");
|
|
61
|
-
await deploy(api, options);
|
|
62
|
-
|
|
58
|
+
await deploy(api, options);
|
|
59
|
+
// eslint-disable-next-line no-process-exit
|
|
63
60
|
process.exit(0);
|
|
64
61
|
});
|
|
65
62
|
program.command('new').usage('[options]').description(i18n.t(localeKeys.command.new.describe)).option('-d, --debug', i18n.t(localeKeys.command.new.debug), false).option('-c, --config <config>', i18n.t(localeKeys.command.new.config)).option('--dist-tag <tag>', i18n.t(localeKeys.command.new.distTag)).option('--registry', i18n.t(localeKeys.command.new.registry)).action(async options => {
|
|
@@ -78,16 +75,13 @@ export default (() => ({
|
|
|
78
75
|
});
|
|
79
76
|
upgradeModel.defineCommand(program.command('upgrade'));
|
|
80
77
|
},
|
|
81
|
-
|
|
82
78
|
async prepare() {
|
|
83
79
|
const command = getCommand();
|
|
84
|
-
|
|
85
80
|
if (command === 'dev' || command === 'build') {
|
|
86
81
|
const appContext = api.useAppContext();
|
|
87
82
|
await emptyDir(appContext.distDirectory);
|
|
88
83
|
}
|
|
89
84
|
},
|
|
90
|
-
|
|
91
85
|
// 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
|
|
92
86
|
async fileChange(e) {
|
|
93
87
|
const {
|
|
@@ -100,7 +94,6 @@ export default (() => ({
|
|
|
100
94
|
srcDirectory
|
|
101
95
|
} = appContext;
|
|
102
96
|
const absolutePath = path.resolve(appDirectory, filename);
|
|
103
|
-
|
|
104
97
|
if (!absolutePath.includes(srcDirectory) && (eventType === 'change' || eventType === 'unlink')) {
|
|
105
98
|
const {
|
|
106
99
|
closeServer
|
|
@@ -109,11 +102,9 @@ export default (() => ({
|
|
|
109
102
|
await cli.restart();
|
|
110
103
|
}
|
|
111
104
|
},
|
|
112
|
-
|
|
113
105
|
async beforeRestart() {
|
|
114
106
|
cleanRequireCache([require.resolve("./analyze")]);
|
|
115
107
|
}
|
|
116
|
-
|
|
117
108
|
};
|
|
118
109
|
}
|
|
119
110
|
}));
|
|
@@ -1,9 +1,6 @@
|
|
|
1
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
2
|
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
3
|
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
4
|
import * as path from 'path';
|
|
8
5
|
import { bundle } from '@modern-js/node-bundle-require';
|
|
9
6
|
import { CONFIG_FILE_EXTENSIONS, fs, getServerConfig, OUTPUT_CONFIG_FILE } from '@modern-js/utils';
|
|
@@ -15,9 +12,7 @@ export const buildServerConfig = async ({
|
|
|
15
12
|
options
|
|
16
13
|
}) => {
|
|
17
14
|
const configFilePath = await getServerConfig(appDirectory, configFile);
|
|
18
|
-
|
|
19
15
|
const getOutputFile = async filepath => path.resolve(distDirectory, `${filepath.replace(new RegExp(CONFIG_FILE_EXTENSIONS.join('|')), '')}.js`);
|
|
20
|
-
|
|
21
16
|
if (configFilePath) {
|
|
22
17
|
const configHelperFilePath = path.normalize(path.join(distDirectory, './config-helper.js'));
|
|
23
18
|
const helperCode = `
|
|
@@ -29,7 +24,6 @@ export const buildServerConfig = async ({
|
|
|
29
24
|
getOutputFile,
|
|
30
25
|
esbuildPlugins: [{
|
|
31
26
|
name: 'native-build-config',
|
|
32
|
-
|
|
33
27
|
setup(ctx) {
|
|
34
28
|
ctx.onResolve({
|
|
35
29
|
filter: /app-tools\/server/
|
|
@@ -39,37 +33,32 @@ export const buildServerConfig = async ({
|
|
|
39
33
|
};
|
|
40
34
|
});
|
|
41
35
|
}
|
|
42
|
-
|
|
43
36
|
}]
|
|
44
37
|
}));
|
|
45
38
|
}
|
|
46
39
|
};
|
|
40
|
+
|
|
47
41
|
/**
|
|
48
42
|
*
|
|
49
43
|
* 处理循环引用的 replacer
|
|
50
44
|
*/
|
|
51
|
-
|
|
52
45
|
export const safeReplacer = () => {
|
|
53
46
|
const cache = [];
|
|
54
47
|
const keyCache = [];
|
|
55
48
|
return function (key, value) {
|
|
56
49
|
if (typeof value === 'object' && value !== null) {
|
|
57
50
|
const index = cache.indexOf(value);
|
|
58
|
-
|
|
59
51
|
if (index !== -1) {
|
|
60
52
|
return `[Circular ${keyCache[index]}]`;
|
|
61
53
|
}
|
|
62
|
-
|
|
63
54
|
cache.push(value);
|
|
64
55
|
keyCache.push(key || 'root');
|
|
65
56
|
}
|
|
66
|
-
|
|
67
57
|
return value;
|
|
68
58
|
};
|
|
69
59
|
};
|
|
70
60
|
export const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
|
|
71
61
|
var _resolvedConfig$outpu;
|
|
72
|
-
|
|
73
62
|
const outputPath = path.join(appDirectory, (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$outpu = resolvedConfig.output) === null || _resolvedConfig$outpu === void 0 ? void 0 : _resolvedConfig$outpu.path) || './dist', OUTPUT_CONFIG_FILE);
|
|
74
63
|
await fs.writeJSON(outputPath, resolvedConfig, {
|
|
75
64
|
spaces: 2,
|
|
@@ -1,9 +1,6 @@
|
|
|
1
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
2
|
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
3
|
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
4
|
import { Server } from '@modern-js/server';
|
|
8
5
|
let server = null;
|
|
9
6
|
export const getServer = () => server;
|
|
@@ -17,14 +14,12 @@ export const createServer = async options => {
|
|
|
17
14
|
if (server) {
|
|
18
15
|
await server.close();
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
server = new Server(options);
|
|
22
18
|
const app = await server.init();
|
|
23
19
|
return app;
|
|
24
20
|
};
|
|
25
21
|
export const injectDataLoaderPlugin = internalPlugins => {
|
|
26
22
|
const DataLoaderPlugin = require.resolve('@modern-js/plugin-data-loader/server');
|
|
27
|
-
|
|
28
23
|
return _objectSpread(_objectSpread({}, internalPlugins), {}, {
|
|
29
24
|
'@modern-js/plugin-data-loader': {
|
|
30
25
|
path: DataLoaderPlugin,
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { inquirer } from '@modern-js/utils';
|
|
2
2
|
export const getSpecifiedEntries = async (entry, entrypoints) => {
|
|
3
3
|
const entryNames = entrypoints.map(e => e.entryName);
|
|
4
|
-
|
|
5
4
|
if (!entry) {
|
|
6
5
|
return entryNames;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
7
|
if (typeof entry === 'boolean') {
|
|
10
8
|
const {
|
|
11
9
|
selected
|
|
@@ -14,19 +12,15 @@ export const getSpecifiedEntries = async (entry, entrypoints) => {
|
|
|
14
12
|
name: 'selected',
|
|
15
13
|
choices: entryNames,
|
|
16
14
|
message: '请选择需要构建的入口',
|
|
17
|
-
|
|
18
15
|
validate(answer) {
|
|
19
16
|
if (answer.length < 1) {
|
|
20
17
|
return 'You must choose at least one topping.';
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
return true;
|
|
24
20
|
}
|
|
25
|
-
|
|
26
21
|
}]);
|
|
27
22
|
return selected;
|
|
28
23
|
}
|
|
29
|
-
|
|
30
24
|
entry.forEach(name => {
|
|
31
25
|
if (!entryNames.includes(name)) {
|
|
32
26
|
throw new Error(`can not found entry ${name}, compiler entry should in ${entryNames.join(', ')}`);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { prettyInstructions, logger } from '@modern-js/utils';
|
|
2
2
|
export const printInstructions = async (hookRunners, appContext, config) => {
|
|
3
|
-
const message = prettyInstructions(appContext, config);
|
|
3
|
+
const message = prettyInstructions(appContext, config);
|
|
4
4
|
|
|
5
|
+
// call beforePrintInstructions hook.
|
|
5
6
|
const {
|
|
6
7
|
instructions
|
|
7
8
|
} = await hookRunners.beforePrintInstructions({
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { fs, ROUTE_SPEC_FILE } from '@modern-js/utils';
|
|
3
|
-
|
|
4
3
|
const generateRoutes = async appContext => {
|
|
5
4
|
const {
|
|
6
5
|
serverRoutes,
|
|
@@ -11,5 +10,4 @@ const generateRoutes = async appContext => {
|
|
|
11
10
|
}, null, 2);
|
|
12
11
|
await fs.outputFile(path.join(distDirectory, ROUTE_SPEC_FILE), output);
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
export { generateRoutes };
|
|
@@ -4,37 +4,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.generateCode = exports.createImportStatements = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _core = require("@modern-js/core");
|
|
13
|
-
|
|
14
10
|
var _esbuild = _interopRequireDefault(require("esbuild"));
|
|
15
|
-
|
|
16
11
|
var _commands = require("../utils/commands");
|
|
17
|
-
|
|
18
12
|
var templates = _interopRequireWildcard(require("./templates"));
|
|
19
|
-
|
|
20
13
|
var _getClientRoutes = require("./getClientRoutes");
|
|
21
|
-
|
|
22
14
|
var _constants = require("./constants");
|
|
23
|
-
|
|
24
15
|
var _utils2 = require("./utils");
|
|
25
|
-
|
|
26
16
|
var _nestedRoutes = require("./nestedRoutes");
|
|
27
|
-
|
|
28
17
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
29
|
-
|
|
30
18
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
31
|
-
|
|
32
19
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
33
|
-
|
|
34
20
|
const createImportSpecifier = specifiers => {
|
|
35
21
|
let defaults = '';
|
|
36
22
|
const named = [];
|
|
37
|
-
|
|
38
23
|
for (const {
|
|
39
24
|
local,
|
|
40
25
|
imported
|
|
@@ -47,7 +32,6 @@ const createImportSpecifier = specifiers => {
|
|
|
47
32
|
named.push(imported);
|
|
48
33
|
}
|
|
49
34
|
}
|
|
50
|
-
|
|
51
35
|
if (defaults && named.length) {
|
|
52
36
|
return `${defaults}, { ${named.join(', ')} }`;
|
|
53
37
|
} else if (defaults) {
|
|
@@ -56,12 +40,10 @@ const createImportSpecifier = specifiers => {
|
|
|
56
40
|
return `{ ${named.join(', ')} }`;
|
|
57
41
|
}
|
|
58
42
|
};
|
|
59
|
-
|
|
60
43
|
const createImportStatements = statements => {
|
|
61
44
|
// merge import statements with the same value.
|
|
62
45
|
const deDuplicated = [];
|
|
63
46
|
const seen = new Map();
|
|
64
|
-
|
|
65
47
|
for (const {
|
|
66
48
|
value,
|
|
67
49
|
specifiers,
|
|
@@ -76,24 +58,20 @@ const createImportStatements = statements => {
|
|
|
76
58
|
seen.set(value, specifiers);
|
|
77
59
|
} else {
|
|
78
60
|
var _deDuplicated$modifyI, _deDuplicated$modifyI2;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
61
|
+
seen.get(value).push(...specifiers);
|
|
62
|
+
// make "initialize" param can be connected when multiple plugins were imported from same package
|
|
82
63
|
const modifyIndex = deDuplicated.findIndex(v => v.value === value);
|
|
83
64
|
const originInitialize = (_deDuplicated$modifyI = (_deDuplicated$modifyI2 = deDuplicated[modifyIndex]) === null || _deDuplicated$modifyI2 === void 0 ? void 0 : _deDuplicated$modifyI2.initialize) !== null && _deDuplicated$modifyI !== void 0 ? _deDuplicated$modifyI : '';
|
|
84
65
|
deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
|
|
85
66
|
}
|
|
86
67
|
}
|
|
87
|
-
|
|
88
68
|
return deDuplicated.map(({
|
|
89
69
|
value,
|
|
90
70
|
specifiers,
|
|
91
71
|
initialize
|
|
92
72
|
}) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
|
|
93
73
|
};
|
|
94
|
-
|
|
95
74
|
exports.createImportStatements = createImportStatements;
|
|
96
|
-
|
|
97
75
|
const buildLoader = async (entry, outfile) => {
|
|
98
76
|
const loader = {
|
|
99
77
|
'.js': 'jsx',
|
|
@@ -113,32 +91,27 @@ const buildLoader = async (entry, outfile) => {
|
|
|
113
91
|
outfile,
|
|
114
92
|
plugins: [{
|
|
115
93
|
name: 'make-all-packages-external',
|
|
116
|
-
|
|
117
94
|
setup(build) {
|
|
118
95
|
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
119
96
|
build.onResolve({
|
|
120
97
|
filter: EXTERNAL_REGEXP
|
|
121
98
|
}, args => {
|
|
122
|
-
let external = true;
|
|
123
|
-
|
|
99
|
+
let external = true;
|
|
100
|
+
// FIXME: windows external entrypoint
|
|
124
101
|
if (args.kind === 'entry-point') {
|
|
125
102
|
external = false;
|
|
126
103
|
}
|
|
127
|
-
|
|
128
104
|
return {
|
|
129
105
|
path: args.path,
|
|
130
106
|
external
|
|
131
107
|
};
|
|
132
108
|
});
|
|
133
109
|
}
|
|
134
|
-
|
|
135
110
|
}]
|
|
136
111
|
});
|
|
137
112
|
};
|
|
138
|
-
|
|
139
113
|
const generateCode = async (appContext, config, entrypoints, api) => {
|
|
140
114
|
var _config$runtime, _config$runtime$route;
|
|
141
|
-
|
|
142
115
|
const {
|
|
143
116
|
internalDirectory,
|
|
144
117
|
distDirectory,
|
|
@@ -153,7 +126,6 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
153
126
|
} = config.output;
|
|
154
127
|
const getRoutes = islegacy ? _getClientRoutes.getClientRoutesLegacy : _getClientRoutes.getClientRoutes;
|
|
155
128
|
await Promise.all(entrypoints.map(generateEntryCode));
|
|
156
|
-
|
|
157
129
|
async function generateEntryCode(entrypoint) {
|
|
158
130
|
const {
|
|
159
131
|
entryName,
|
|
@@ -161,13 +133,11 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
161
133
|
customBootstrap,
|
|
162
134
|
fileSystemRoutes
|
|
163
135
|
} = entrypoint;
|
|
164
|
-
|
|
165
136
|
if (isAutoMount) {
|
|
166
137
|
// generate routes file for file system routes entrypoint.
|
|
167
138
|
if (fileSystemRoutes) {
|
|
168
139
|
let initialRoutes = [];
|
|
169
140
|
let nestedRoute = null;
|
|
170
|
-
|
|
171
141
|
if (entrypoint.entry) {
|
|
172
142
|
initialRoutes = getRoutes({
|
|
173
143
|
entrypoint,
|
|
@@ -177,25 +147,21 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
177
147
|
internalDirAlias
|
|
178
148
|
});
|
|
179
149
|
}
|
|
180
|
-
|
|
181
150
|
if (entrypoint.nestedRoutesEntry) {
|
|
182
151
|
if (!islegacy) {
|
|
183
152
|
nestedRoute = await (0, _nestedRoutes.walk)(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
|
|
184
153
|
name: internalSrcAlias,
|
|
185
154
|
basename: srcDirectory
|
|
186
|
-
});
|
|
187
|
-
|
|
155
|
+
}, entrypoint.entryName);
|
|
188
156
|
if (nestedRoute) {
|
|
189
157
|
initialRoutes.unshift(nestedRoute);
|
|
190
158
|
}
|
|
191
159
|
} else {
|
|
192
|
-
_utils.logger.error('Nested routes is not supported in legacy mode.');
|
|
193
|
-
|
|
194
|
-
|
|
160
|
+
_utils.logger.error('Nested routes is not supported in legacy mode.');
|
|
161
|
+
// eslint-disable-next-line no-process-exit
|
|
195
162
|
process.exit(1);
|
|
196
163
|
}
|
|
197
164
|
}
|
|
198
|
-
|
|
199
165
|
const {
|
|
200
166
|
routes
|
|
201
167
|
} = await hookRunners.modifyFileSystemRoutes({
|
|
@@ -205,13 +171,19 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
205
171
|
const config = (0, _core.useResolvedConfigContext)();
|
|
206
172
|
const ssr = config === null || config === void 0 ? void 0 : config.server.ssr;
|
|
207
173
|
let mode;
|
|
208
|
-
|
|
209
174
|
if (ssr) {
|
|
210
175
|
mode = typeof ssr === 'object' ? ssr.mode || 'string' : 'string';
|
|
211
176
|
} else {
|
|
212
177
|
mode = false;
|
|
213
178
|
}
|
|
214
|
-
|
|
179
|
+
if (mode === 'stream') {
|
|
180
|
+
const hasPageRoute = routes.some(route => 'type' in route && route.type === 'page');
|
|
181
|
+
if (hasPageRoute) {
|
|
182
|
+
_utils.logger.error('streaming ssr is not supported when pages dir exists');
|
|
183
|
+
// eslint-disable-next-line no-process-exit
|
|
184
|
+
process.exit(1);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
215
187
|
const {
|
|
216
188
|
code
|
|
217
189
|
} = await hookRunners.beforeGenerateRoutes({
|
|
@@ -219,15 +191,15 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
219
191
|
code: templates.fileSystemRoutes({
|
|
220
192
|
routes,
|
|
221
193
|
ssrMode: mode,
|
|
222
|
-
nestedRoutesEntry: entrypoint.nestedRoutesEntry
|
|
194
|
+
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
195
|
+
entryName: entrypoint.entryName
|
|
223
196
|
})
|
|
224
|
-
});
|
|
197
|
+
});
|
|
225
198
|
|
|
199
|
+
// extract nested router loaders
|
|
226
200
|
if (entrypoint.nestedRoutesEntry) {
|
|
227
201
|
const routesServerFile = _path.default.join(internalDirectory, entryName, 'routes.server.js');
|
|
228
|
-
|
|
229
202
|
const outputRoutesServerFile = _path.default.join(distDirectory, 'loader-routes', entryName, 'index.js');
|
|
230
|
-
|
|
231
203
|
const code = templates.routesForServer({
|
|
232
204
|
routes: routes,
|
|
233
205
|
alias: {
|
|
@@ -239,11 +211,10 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
239
211
|
await _utils.fs.writeFile(routesServerFile, code);
|
|
240
212
|
await buildLoader(routesServerFile, outputRoutesServerFile);
|
|
241
213
|
}
|
|
242
|
-
|
|
243
214
|
_utils.fs.outputFileSync(_path.default.resolve(internalDirectory, `./${entryName}/${_constants.FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
|
|
244
|
-
}
|
|
245
|
-
|
|
215
|
+
}
|
|
246
216
|
|
|
217
|
+
// call modifyEntryImports hook
|
|
247
218
|
const {
|
|
248
219
|
imports: importStatements
|
|
249
220
|
} = await hookRunners.modifyEntryImports({
|
|
@@ -255,15 +226,17 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
255
226
|
internalDirAlias,
|
|
256
227
|
internalDirectory
|
|
257
228
|
})
|
|
258
|
-
});
|
|
229
|
+
});
|
|
259
230
|
|
|
231
|
+
// call modifyEntryRuntimePlugins hook
|
|
260
232
|
const {
|
|
261
233
|
plugins
|
|
262
234
|
} = await hookRunners.modifyEntryRuntimePlugins({
|
|
263
235
|
entrypoint,
|
|
264
236
|
plugins: []
|
|
265
|
-
});
|
|
237
|
+
});
|
|
266
238
|
|
|
239
|
+
// call modifyEntryRenderFunction hook
|
|
267
240
|
const {
|
|
268
241
|
code: renderFunction
|
|
269
242
|
} = await hookRunners.modifyEntryRenderFunction({
|
|
@@ -273,8 +246,9 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
273
246
|
customBootstrap,
|
|
274
247
|
fileSystemRoutes
|
|
275
248
|
})
|
|
276
|
-
});
|
|
249
|
+
});
|
|
277
250
|
|
|
251
|
+
// call modifyEntryExport hook
|
|
278
252
|
const {
|
|
279
253
|
exportStatement
|
|
280
254
|
} = await hookRunners.modifyEntryExport({
|
|
@@ -287,11 +261,10 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
287
261
|
renderFunction,
|
|
288
262
|
exportStatement
|
|
289
263
|
});
|
|
290
|
-
|
|
291
264
|
const entryFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_POINT_FILE_NAME}`);
|
|
265
|
+
entrypoint.entry = entryFile;
|
|
292
266
|
|
|
293
|
-
|
|
294
|
-
|
|
267
|
+
// generate entry file.
|
|
295
268
|
if (config.source.enableAsyncEntry) {
|
|
296
269
|
const {
|
|
297
270
|
code: asyncEntryCode
|
|
@@ -299,11 +272,8 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
299
272
|
entrypoint,
|
|
300
273
|
code: `import('./${_constants.ENTRY_BOOTSTRAP_FILE_NAME}');`
|
|
301
274
|
});
|
|
302
|
-
|
|
303
275
|
_utils.fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
|
|
304
|
-
|
|
305
276
|
const bootstrapFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_BOOTSTRAP_FILE_NAME}`);
|
|
306
|
-
|
|
307
277
|
_utils.fs.outputFileSync(bootstrapFile, code, 'utf8');
|
|
308
278
|
} else {
|
|
309
279
|
_utils.fs.outputFileSync(entryFile, code, 'utf8');
|
|
@@ -311,5 +281,4 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
311
281
|
}
|
|
312
282
|
}
|
|
313
283
|
};
|
|
314
|
-
|
|
315
284
|
exports.generateCode = generateCode;
|
|
@@ -4,40 +4,29 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getBundleEntry = void 0;
|
|
7
|
-
|
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _getFileSystemEntry = require("./getFileSystemEntry");
|
|
13
|
-
|
|
14
10
|
var _constants = require("./constants");
|
|
15
|
-
|
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
12
|
const ensureExtensions = file => {
|
|
19
13
|
if (!_path.default.extname(file)) {
|
|
20
14
|
return (0, _utils.findExists)(_constants.JS_EXTENSIONS.map(ext => `${file}${ext}`)) || file;
|
|
21
15
|
}
|
|
22
|
-
|
|
23
16
|
return file;
|
|
24
17
|
};
|
|
25
|
-
|
|
26
18
|
const ifAlreadyExists = (entrypoints, checked) => entrypoints.some(entrypoint => {
|
|
27
19
|
if (ensureExtensions(entrypoint.entry) === ensureExtensions(checked.entry)) {
|
|
28
20
|
// reset entryName
|
|
29
21
|
checked.entryName = entrypoint.entryName;
|
|
30
22
|
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
}
|
|
24
|
+
// filesystem routes entrypoint conflict with normal entrypoint.
|
|
34
25
|
if (entrypoint.entry.startsWith(checked.entry) || checked.entry.startsWith(entrypoint.entry)) {
|
|
35
26
|
throw new Error(`Entry configuration conflicts\n Your configuration: ${checked.entry}.\n Default entrypoint: ${entrypoint.entry}\n Please reset source.entries or set source.disableDefaultEntries to disable the default entry rules.`);
|
|
36
27
|
}
|
|
37
|
-
|
|
38
28
|
return false;
|
|
39
29
|
});
|
|
40
|
-
|
|
41
30
|
const getBundleEntry = (appContext, config) => {
|
|
42
31
|
const {
|
|
43
32
|
appDirectory,
|
|
@@ -50,8 +39,9 @@ const getBundleEntry = (appContext, config) => {
|
|
|
50
39
|
entriesDir
|
|
51
40
|
}
|
|
52
41
|
} = config;
|
|
53
|
-
const defaults = disableDefaultEntries ? [] : (0, _getFileSystemEntry.getFileSystemEntry)(appContext, config);
|
|
42
|
+
const defaults = disableDefaultEntries ? [] : (0, _getFileSystemEntry.getFileSystemEntry)(appContext, config);
|
|
54
43
|
|
|
44
|
+
// merge entrypoints from user config with directory convention.
|
|
55
45
|
if (entries) {
|
|
56
46
|
Object.keys(entries).forEach(name => {
|
|
57
47
|
const value = entries[name];
|
|
@@ -66,13 +56,11 @@ const getBundleEntry = (appContext, config) => {
|
|
|
66
56
|
isAutoMount: !value.disableMount,
|
|
67
57
|
fileSystemRoutes: value.enableFileSystemRoutes ? {} : undefined
|
|
68
58
|
};
|
|
69
|
-
|
|
70
59
|
if (!ifAlreadyExists(defaults, entrypoint)) {
|
|
71
60
|
defaults.push(entrypoint);
|
|
72
61
|
}
|
|
73
62
|
});
|
|
74
63
|
}
|
|
75
|
-
|
|
76
64
|
if (!disableDefaultEntries) {
|
|
77
65
|
// find main entry point which server route is '/'.
|
|
78
66
|
const entriesDirAbs = (0, _utils.ensureAbsolutePath)(appDirectory, entriesDir);
|
|
@@ -83,8 +71,6 @@ const getBundleEntry = (appContext, config) => {
|
|
|
83
71
|
}) => entryName === packageName || _path.default.dirname(entry) === entriesDirAbs || _path.default.dirname(_nestedRoutesEntry) === entriesDirAbs);
|
|
84
72
|
found && (found.entryName = _utils.MAIN_ENTRY_NAME);
|
|
85
73
|
}
|
|
86
|
-
|
|
87
74
|
return defaults;
|
|
88
75
|
};
|
|
89
|
-
|
|
90
76
|
exports.getBundleEntry = getBundleEntry;
|