@modern-js/app-tools 1.21.5 → 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 +319 -51
- package/dist/js/modern/analyze/constants.js +1 -0
- package/dist/js/modern/analyze/generateCode.js +126 -32
- package/dist/js/modern/analyze/getBundleEntry.js +7 -14
- package/dist/js/modern/analyze/getClientRoutes/getRoutes.js +173 -0
- package/dist/js/modern/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +13 -58
- package/dist/js/modern/analyze/getClientRoutes/index.js +2 -0
- package/dist/js/modern/analyze/getClientRoutes/utils.js +18 -0
- package/dist/js/modern/analyze/getFileSystemEntry.js +27 -21
- 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 +81 -23
- 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 +102 -0
- package/dist/js/modern/analyze/templates.js +151 -12
- package/dist/js/modern/analyze/utils.js +2 -8
- package/dist/js/modern/builder/builderPlugins/compatModern.js +208 -0
- package/dist/js/modern/builder/createHtmlConfig.js +58 -0
- package/dist/js/modern/builder/createOutputConfig.js +70 -0
- package/dist/js/modern/builder/createSourceConfig.js +74 -0
- package/dist/js/modern/builder/createToolsConfig.js +87 -0
- package/dist/js/modern/builder/index.js +108 -0
- package/dist/js/modern/builder/share.js +44 -0
- package/dist/js/modern/builder/webpackPlugins/htmlAsyncChunkPlugin.js +29 -0
- package/dist/js/modern/builder/webpackPlugins/htmlBottomTemplate.js +33 -0
- package/dist/js/modern/builder/webpackPlugins/routerPlugin.js +97 -0
- package/dist/js/modern/commands/build.js +16 -110
- package/dist/js/modern/commands/dev.js +12 -28
- package/dist/js/modern/commands/inspect.js +8 -66
- package/dist/js/modern/commands/start.js +2 -3
- package/dist/js/modern/index.js +17 -16
- package/dist/js/modern/locale/en.js +0 -1
- package/dist/js/modern/locale/zh.js +0 -1
- package/dist/js/modern/utils/commands.js +5 -0
- package/dist/js/modern/utils/config.js +1 -12
- package/dist/js/modern/utils/createServer.js +12 -1
- package/dist/js/modern/utils/getSpecifiedEntries.js +0 -6
- package/dist/js/modern/utils/printInstructions.js +3 -2
- package/dist/js/modern/utils/routes.js +0 -2
- package/dist/js/node/analyze/constants.js +3 -1
- package/dist/js/node/analyze/generateCode.js +123 -47
- package/dist/js/node/analyze/getBundleEntry.js +7 -20
- package/dist/js/node/analyze/getClientRoutes/getRoutes.js +181 -0
- package/dist/js/node/analyze/{getClientRoutes.js → getClientRoutes/getRoutesLegacy.js} +19 -78
- package/dist/js/node/analyze/getClientRoutes/index.js +19 -0
- package/dist/js/node/analyze/getClientRoutes/utils.js +28 -0
- package/dist/js/node/analyze/getFileSystemEntry.js +26 -28
- 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 +81 -33
- 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 +111 -0
- package/dist/js/node/analyze/templates.js +153 -21
- package/dist/js/node/analyze/utils.js +4 -20
- package/dist/js/node/builder/builderPlugins/compatModern.js +216 -0
- package/dist/js/node/builder/createHtmlConfig.js +68 -0
- package/dist/js/node/builder/createOutputConfig.js +76 -0
- package/dist/js/node/builder/createSourceConfig.js +82 -0
- package/dist/js/node/builder/createToolsConfig.js +94 -0
- package/dist/js/node/builder/index.js +117 -0
- package/dist/js/node/builder/share.js +51 -0
- package/dist/js/node/builder/webpackPlugins/htmlAsyncChunkPlugin.js +36 -0
- package/dist/js/node/builder/webpackPlugins/htmlBottomTemplate.js +40 -0
- package/dist/js/node/builder/webpackPlugins/routerPlugin.js +105 -0
- package/dist/js/node/commands/build.js +14 -121
- package/dist/js/node/commands/deploy.js +0 -2
- package/dist/js/node/commands/dev.js +10 -44
- package/dist/js/node/commands/index.js +0 -6
- package/dist/js/node/commands/inspect.js +10 -90
- package/dist/js/node/commands/start.js +2 -11
- package/dist/js/node/exports/server.js +0 -1
- package/dist/js/node/hooks.js +0 -2
- package/dist/js/node/index.js +22 -32
- package/dist/js/node/locale/en.js +0 -1
- package/dist/js/node/locale/index.js +0 -4
- package/dist/js/node/locale/zh.js +0 -1
- package/dist/js/node/utils/commands.js +12 -0
- package/dist/js/node/utils/config.js +1 -25
- package/dist/js/node/utils/createServer.js +15 -11
- 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 +3 -6
- package/dist/js/node/utils/routes.js +0 -5
- package/dist/js/treeshaking/analyze/constants.js +16 -0
- package/dist/js/treeshaking/analyze/generateCode.js +369 -0
- package/dist/js/treeshaking/analyze/getBundleEntry.js +69 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutes.js +184 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/getRoutesLegacy.js +185 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/index.js +2 -0
- package/dist/js/treeshaking/analyze/getClientRoutes/utils.js +24 -0
- package/dist/js/treeshaking/analyze/getFileSystemEntry.js +96 -0
- package/dist/js/treeshaking/analyze/getHtmlTemplate.js +129 -0
- package/dist/js/treeshaking/analyze/getServerRoutes.js +157 -0
- package/dist/js/treeshaking/analyze/index.js +334 -0
- package/dist/js/treeshaking/analyze/isDefaultExportFunction.js +28 -0
- package/dist/js/treeshaking/analyze/makeLegalIdentifier.js +16 -0
- package/dist/js/treeshaking/analyze/nestedRoutes.js +165 -0
- package/dist/js/treeshaking/analyze/templates.js +170 -0
- package/dist/js/treeshaking/analyze/utils.js +88 -0
- package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +212 -0
- package/dist/js/treeshaking/builder/createHtmlConfig.js +59 -0
- package/dist/js/treeshaking/builder/createOutputConfig.js +70 -0
- package/dist/js/treeshaking/builder/createSourceConfig.js +88 -0
- package/dist/js/treeshaking/builder/createToolsConfig.js +85 -0
- package/dist/js/treeshaking/builder/index.js +161 -0
- package/dist/js/treeshaking/builder/share.js +46 -0
- package/dist/js/treeshaking/builder/webpackPlugins/htmlAsyncChunkPlugin.js +46 -0
- package/dist/js/treeshaking/builder/webpackPlugins/htmlBottomTemplate.js +41 -0
- package/dist/js/treeshaking/builder/webpackPlugins/routerPlugin.js +121 -0
- package/dist/js/treeshaking/commands/build.js +83 -0
- package/dist/js/treeshaking/commands/deploy.js +26 -0
- package/dist/js/treeshaking/commands/dev.js +124 -0
- package/dist/js/treeshaking/commands/index.js +3 -0
- package/dist/js/treeshaking/commands/inspect.js +29 -0
- package/dist/js/treeshaking/commands/start.js +69 -0
- package/dist/js/treeshaking/exports/server.js +1 -0
- package/dist/js/treeshaking/hooks.js +21 -0
- package/dist/js/treeshaking/index.js +267 -0
- package/dist/js/treeshaking/locale/en.js +34 -0
- package/dist/js/treeshaking/locale/index.js +9 -0
- package/dist/js/treeshaking/locale/zh.js +34 -0
- package/dist/js/treeshaking/utils/commands.js +5 -0
- package/dist/js/treeshaking/utils/config.js +124 -0
- package/dist/js/treeshaking/utils/createServer.js +73 -0
- package/dist/js/treeshaking/utils/getSpecifiedEntries.js +58 -0
- package/dist/js/treeshaking/utils/language.js +5 -0
- package/dist/js/treeshaking/utils/printInstructions.js +30 -0
- package/dist/js/treeshaking/utils/routes.js +29 -0
- package/dist/js/treeshaking/utils/types.js +0 -0
- package/dist/types/analyze/constants.d.ts +1 -0
- package/dist/types/analyze/generateCode.d.ts +1 -1
- package/dist/types/analyze/{getClientRoutes.d.ts → getClientRoutes/getRoutes.d.ts} +2 -7
- package/dist/types/analyze/getClientRoutes/getRoutesLegacy.d.ts +15 -0
- package/dist/types/analyze/getClientRoutes/index.d.ts +2 -0
- package/dist/types/analyze/getClientRoutes/utils.d.ts +5 -0
- package/dist/types/analyze/index.d.ts +4 -5
- package/dist/types/analyze/nestedRoutes.d.ts +5 -0
- package/dist/types/analyze/templates.d.ts +19 -3
- package/dist/types/analyze/utils.d.ts +2 -1
- package/dist/types/builder/builderPlugins/compatModern.d.ts +13 -0
- package/dist/types/builder/createHtmlConfig.d.ts +6 -0
- package/dist/types/builder/createOutputConfig.d.ts +3 -0
- package/dist/types/builder/createSourceConfig.d.ts +5 -0
- package/dist/types/builder/createToolsConfig.d.ts +13 -0
- package/dist/types/builder/index.d.ts +15 -0
- package/dist/types/builder/share.d.ts +26 -0
- package/dist/types/builder/webpackPlugins/htmlAsyncChunkPlugin.d.ts +8 -0
- package/dist/types/builder/webpackPlugins/htmlBottomTemplate.d.ts +10 -0
- package/dist/types/builder/webpackPlugins/routerPlugin.d.ts +10 -0
- package/dist/types/commands/build.d.ts +2 -1
- package/dist/types/commands/deploy.d.ts +2 -1
- package/dist/types/commands/dev.d.ts +2 -1
- package/dist/types/commands/inspect.d.ts +2 -6
- package/dist/types/commands/start.d.ts +2 -1
- package/dist/types/hooks.d.ts +15 -10
- package/dist/types/index.d.ts +4 -4
- package/dist/types/locale/en.d.ts +0 -1
- package/dist/types/locale/index.d.ts +0 -2
- package/dist/types/locale/zh.d.ts +0 -1
- package/dist/types/utils/commands.d.ts +1 -0
- package/dist/types/utils/config.d.ts +0 -1
- package/dist/types/utils/createServer.d.ts +8 -1
- package/dist/types/utils/printInstructions.d.ts +3 -2
- package/dist/types/utils/types.d.ts +2 -3
- package/package.json +25 -42
- package/dist/js/modern/utils/createCompiler.js +0 -61
- package/dist/js/node/utils/createCompiler.js +0 -81
- package/dist/types/utils/createCompiler.d.ts +0 -13
|
@@ -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,3 +1,6 @@
|
|
|
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
|
+
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; }
|
|
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; }
|
|
1
4
|
import { Server } from '@modern-js/server';
|
|
2
5
|
let server = null;
|
|
3
6
|
export const getServer = () => server;
|
|
@@ -11,8 +14,16 @@ export const createServer = async options => {
|
|
|
11
14
|
if (server) {
|
|
12
15
|
await server.close();
|
|
13
16
|
}
|
|
14
|
-
|
|
15
17
|
server = new Server(options);
|
|
16
18
|
const app = await server.init();
|
|
17
19
|
return app;
|
|
20
|
+
};
|
|
21
|
+
export const injectDataLoaderPlugin = internalPlugins => {
|
|
22
|
+
const DataLoaderPlugin = require.resolve('@modern-js/plugin-data-loader/server');
|
|
23
|
+
return _objectSpread(_objectSpread({}, internalPlugins), {}, {
|
|
24
|
+
'@modern-js/plugin-data-loader': {
|
|
25
|
+
path: DataLoaderPlugin,
|
|
26
|
+
forced: true
|
|
27
|
+
}
|
|
28
|
+
});
|
|
18
29
|
};
|
|
@@ -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,11 +1,12 @@
|
|
|
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({
|
|
8
9
|
instructions: message
|
|
9
10
|
});
|
|
10
|
-
logger.
|
|
11
|
+
logger.info(instructions);
|
|
11
12
|
};
|
|
@@ -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 };
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.PAGES_DIR_NAME = exports.JS_EXTENSIONS = exports.INDEX_FILE_NAME = exports.HTML_PARTIALS_FOLDER = exports.HTML_PARTIALS_EXTENSIONS = exports.FILE_SYSTEM_ROUTES_LAYOUT = exports.FILE_SYSTEM_ROUTES_INDEX = exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = exports.FILE_SYSTEM_ROUTES_FILE_NAME = exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = exports.ENTRY_POINT_FILE_NAME = exports.ENTRY_BOOTSTRAP_FILE_NAME = exports.APP_FILE_NAME = void 0;
|
|
6
|
+
exports.PAGES_DIR_NAME = exports.NESTED_ROUTES_DIR = exports.JS_EXTENSIONS = exports.INDEX_FILE_NAME = exports.HTML_PARTIALS_FOLDER = exports.HTML_PARTIALS_EXTENSIONS = exports.FILE_SYSTEM_ROUTES_LAYOUT = exports.FILE_SYSTEM_ROUTES_INDEX = exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = exports.FILE_SYSTEM_ROUTES_FILE_NAME = exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = exports.ENTRY_POINT_FILE_NAME = exports.ENTRY_BOOTSTRAP_FILE_NAME = exports.APP_FILE_NAME = void 0;
|
|
7
7
|
const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
|
|
8
8
|
exports.JS_EXTENSIONS = JS_EXTENSIONS;
|
|
9
9
|
const INDEX_FILE_NAME = 'index';
|
|
@@ -12,6 +12,8 @@ const APP_FILE_NAME = 'App';
|
|
|
12
12
|
exports.APP_FILE_NAME = APP_FILE_NAME;
|
|
13
13
|
const PAGES_DIR_NAME = 'pages';
|
|
14
14
|
exports.PAGES_DIR_NAME = PAGES_DIR_NAME;
|
|
15
|
+
const NESTED_ROUTES_DIR = 'routes';
|
|
16
|
+
exports.NESTED_ROUTES_DIR = NESTED_ROUTES_DIR;
|
|
15
17
|
const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
|
|
16
18
|
exports.FILE_SYSTEM_ROUTES_FILE_NAME = FILE_SYSTEM_ROUTES_FILE_NAME;
|
|
17
19
|
const ENTRY_POINT_FILE_NAME = 'index.js';
|
|
@@ -4,29 +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
|
-
|
|
9
|
+
var _core = require("@modern-js/core");
|
|
10
|
+
var _esbuild = _interopRequireDefault(require("esbuild"));
|
|
11
|
+
var _commands = require("../utils/commands");
|
|
12
12
|
var templates = _interopRequireWildcard(require("./templates"));
|
|
13
|
-
|
|
14
13
|
var _getClientRoutes = require("./getClientRoutes");
|
|
15
|
-
|
|
16
14
|
var _constants = require("./constants");
|
|
17
|
-
|
|
18
15
|
var _utils2 = require("./utils");
|
|
19
|
-
|
|
16
|
+
var _nestedRoutes = require("./nestedRoutes");
|
|
20
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); }
|
|
21
|
-
|
|
22
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; }
|
|
23
|
-
|
|
24
19
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
-
|
|
26
20
|
const createImportSpecifier = specifiers => {
|
|
27
21
|
let defaults = '';
|
|
28
22
|
const named = [];
|
|
29
|
-
|
|
30
23
|
for (const {
|
|
31
24
|
local,
|
|
32
25
|
imported
|
|
@@ -39,7 +32,6 @@ const createImportSpecifier = specifiers => {
|
|
|
39
32
|
named.push(imported);
|
|
40
33
|
}
|
|
41
34
|
}
|
|
42
|
-
|
|
43
35
|
if (defaults && named.length) {
|
|
44
36
|
return `${defaults}, { ${named.join(', ')} }`;
|
|
45
37
|
} else if (defaults) {
|
|
@@ -48,12 +40,10 @@ const createImportSpecifier = specifiers => {
|
|
|
48
40
|
return `{ ${named.join(', ')} }`;
|
|
49
41
|
}
|
|
50
42
|
};
|
|
51
|
-
|
|
52
43
|
const createImportStatements = statements => {
|
|
53
44
|
// merge import statements with the same value.
|
|
54
45
|
const deDuplicated = [];
|
|
55
46
|
const seen = new Map();
|
|
56
|
-
|
|
57
47
|
for (const {
|
|
58
48
|
value,
|
|
59
49
|
specifiers,
|
|
@@ -68,75 +58,163 @@ const createImportStatements = statements => {
|
|
|
68
58
|
seen.set(value, specifiers);
|
|
69
59
|
} else {
|
|
70
60
|
var _deDuplicated$modifyI, _deDuplicated$modifyI2;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
61
|
+
seen.get(value).push(...specifiers);
|
|
62
|
+
// make "initialize" param can be connected when multiple plugins were imported from same package
|
|
74
63
|
const modifyIndex = deDuplicated.findIndex(v => v.value === value);
|
|
75
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 : '';
|
|
76
65
|
deDuplicated[modifyIndex].initialize = originInitialize.concat(`\n${initialize || ''}`);
|
|
77
66
|
}
|
|
78
67
|
}
|
|
79
|
-
|
|
80
68
|
return deDuplicated.map(({
|
|
81
69
|
value,
|
|
82
70
|
specifiers,
|
|
83
71
|
initialize
|
|
84
72
|
}) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
|
|
85
73
|
};
|
|
86
|
-
|
|
87
74
|
exports.createImportStatements = createImportStatements;
|
|
88
|
-
|
|
75
|
+
const buildLoader = async (entry, outfile) => {
|
|
76
|
+
const loader = {
|
|
77
|
+
'.js': 'jsx',
|
|
78
|
+
'.ts': 'tsx'
|
|
79
|
+
};
|
|
80
|
+
const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
|
|
81
|
+
const command = (0, _commands.getCommand)();
|
|
82
|
+
await _esbuild.default.build({
|
|
83
|
+
format: 'cjs',
|
|
84
|
+
platform: 'node',
|
|
85
|
+
target: 'esnext',
|
|
86
|
+
loader,
|
|
87
|
+
watch: command === 'dev' && {},
|
|
88
|
+
bundle: true,
|
|
89
|
+
logLevel: 'error',
|
|
90
|
+
entryPoints: [entry],
|
|
91
|
+
outfile,
|
|
92
|
+
plugins: [{
|
|
93
|
+
name: 'make-all-packages-external',
|
|
94
|
+
setup(build) {
|
|
95
|
+
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
|
96
|
+
build.onResolve({
|
|
97
|
+
filter: EXTERNAL_REGEXP
|
|
98
|
+
}, args => {
|
|
99
|
+
let external = true;
|
|
100
|
+
// FIXME: windows external entrypoint
|
|
101
|
+
if (args.kind === 'entry-point') {
|
|
102
|
+
external = false;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
path: args.path,
|
|
106
|
+
external
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}]
|
|
111
|
+
});
|
|
112
|
+
};
|
|
89
113
|
const generateCode = async (appContext, config, entrypoints, api) => {
|
|
114
|
+
var _config$runtime, _config$runtime$route;
|
|
90
115
|
const {
|
|
91
116
|
internalDirectory,
|
|
117
|
+
distDirectory,
|
|
92
118
|
srcDirectory,
|
|
93
119
|
internalDirAlias,
|
|
94
120
|
internalSrcAlias
|
|
95
121
|
} = appContext;
|
|
96
122
|
const hookRunners = api.useHookRunners();
|
|
123
|
+
const islegacy = Boolean(config === null || config === void 0 ? void 0 : (_config$runtime = config.runtime) === null || _config$runtime === void 0 ? void 0 : (_config$runtime$route = _config$runtime.router) === null || _config$runtime$route === void 0 ? void 0 : _config$runtime$route.legacy);
|
|
97
124
|
const {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
for (const entrypoint of entrypoints) {
|
|
125
|
+
mountId
|
|
126
|
+
} = config.output;
|
|
127
|
+
const getRoutes = islegacy ? _getClientRoutes.getClientRoutesLegacy : _getClientRoutes.getClientRoutes;
|
|
128
|
+
await Promise.all(entrypoints.map(generateEntryCode));
|
|
129
|
+
async function generateEntryCode(entrypoint) {
|
|
104
130
|
const {
|
|
105
131
|
entryName,
|
|
106
132
|
isAutoMount,
|
|
107
133
|
customBootstrap,
|
|
108
134
|
fileSystemRoutes
|
|
109
135
|
} = entrypoint;
|
|
110
|
-
|
|
111
136
|
if (isAutoMount) {
|
|
112
137
|
// generate routes file for file system routes entrypoint.
|
|
113
138
|
if (fileSystemRoutes) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
139
|
+
let initialRoutes = [];
|
|
140
|
+
let nestedRoute = null;
|
|
141
|
+
if (entrypoint.entry) {
|
|
142
|
+
initialRoutes = getRoutes({
|
|
143
|
+
entrypoint,
|
|
144
|
+
srcDirectory,
|
|
145
|
+
srcAlias: internalSrcAlias,
|
|
146
|
+
internalDirectory,
|
|
147
|
+
internalDirAlias
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (entrypoint.nestedRoutesEntry) {
|
|
151
|
+
if (!islegacy) {
|
|
152
|
+
nestedRoute = await (0, _nestedRoutes.walk)(entrypoint.nestedRoutesEntry, entrypoint.nestedRoutesEntry, {
|
|
153
|
+
name: internalSrcAlias,
|
|
154
|
+
basename: srcDirectory
|
|
155
|
+
}, entrypoint.entryName);
|
|
156
|
+
if (nestedRoute) {
|
|
157
|
+
initialRoutes.unshift(nestedRoute);
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
_utils.logger.error('Nested routes is not supported in legacy mode.');
|
|
161
|
+
// eslint-disable-next-line no-process-exit
|
|
162
|
+
process.exit(1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
121
165
|
const {
|
|
122
166
|
routes
|
|
123
167
|
} = await hookRunners.modifyFileSystemRoutes({
|
|
124
168
|
entrypoint,
|
|
125
169
|
routes: initialRoutes
|
|
126
170
|
});
|
|
171
|
+
const config = (0, _core.useResolvedConfigContext)();
|
|
172
|
+
const ssr = config === null || config === void 0 ? void 0 : config.server.ssr;
|
|
173
|
+
let mode;
|
|
174
|
+
if (ssr) {
|
|
175
|
+
mode = typeof ssr === 'object' ? ssr.mode || 'string' : 'string';
|
|
176
|
+
} else {
|
|
177
|
+
mode = false;
|
|
178
|
+
}
|
|
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
|
+
}
|
|
127
187
|
const {
|
|
128
188
|
code
|
|
129
189
|
} = await hookRunners.beforeGenerateRoutes({
|
|
130
190
|
entrypoint,
|
|
131
191
|
code: templates.fileSystemRoutes({
|
|
132
|
-
routes
|
|
192
|
+
routes,
|
|
193
|
+
ssrMode: mode,
|
|
194
|
+
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
195
|
+
entryName: entrypoint.entryName
|
|
133
196
|
})
|
|
134
197
|
});
|
|
135
198
|
|
|
199
|
+
// extract nested router loaders
|
|
200
|
+
if (entrypoint.nestedRoutesEntry) {
|
|
201
|
+
const routesServerFile = _path.default.join(internalDirectory, entryName, 'routes.server.js');
|
|
202
|
+
const outputRoutesServerFile = _path.default.join(distDirectory, 'loader-routes', entryName, 'index.js');
|
|
203
|
+
const code = templates.routesForServer({
|
|
204
|
+
routes: routes,
|
|
205
|
+
alias: {
|
|
206
|
+
name: internalSrcAlias,
|
|
207
|
+
basename: srcDirectory
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
await _utils.fs.ensureFile(routesServerFile);
|
|
211
|
+
await _utils.fs.writeFile(routesServerFile, code);
|
|
212
|
+
await buildLoader(routesServerFile, outputRoutesServerFile);
|
|
213
|
+
}
|
|
136
214
|
_utils.fs.outputFileSync(_path.default.resolve(internalDirectory, `./${entryName}/${_constants.FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
|
|
137
|
-
}
|
|
138
|
-
|
|
215
|
+
}
|
|
139
216
|
|
|
217
|
+
// call modifyEntryImports hook
|
|
140
218
|
const {
|
|
141
219
|
imports: importStatements
|
|
142
220
|
} = await hookRunners.modifyEntryImports({
|
|
@@ -148,15 +226,17 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
148
226
|
internalDirAlias,
|
|
149
227
|
internalDirectory
|
|
150
228
|
})
|
|
151
|
-
});
|
|
229
|
+
});
|
|
152
230
|
|
|
231
|
+
// call modifyEntryRuntimePlugins hook
|
|
153
232
|
const {
|
|
154
233
|
plugins
|
|
155
234
|
} = await hookRunners.modifyEntryRuntimePlugins({
|
|
156
235
|
entrypoint,
|
|
157
236
|
plugins: []
|
|
158
|
-
});
|
|
237
|
+
});
|
|
159
238
|
|
|
239
|
+
// call modifyEntryRenderFunction hook
|
|
160
240
|
const {
|
|
161
241
|
code: renderFunction
|
|
162
242
|
} = await hookRunners.modifyEntryRenderFunction({
|
|
@@ -166,8 +246,9 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
166
246
|
customBootstrap,
|
|
167
247
|
fileSystemRoutes
|
|
168
248
|
})
|
|
169
|
-
});
|
|
249
|
+
});
|
|
170
250
|
|
|
251
|
+
// call modifyEntryExport hook
|
|
171
252
|
const {
|
|
172
253
|
exportStatement
|
|
173
254
|
} = await hookRunners.modifyEntryExport({
|
|
@@ -180,11 +261,10 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
180
261
|
renderFunction,
|
|
181
262
|
exportStatement
|
|
182
263
|
});
|
|
183
|
-
|
|
184
264
|
const entryFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_POINT_FILE_NAME}`);
|
|
265
|
+
entrypoint.entry = entryFile;
|
|
185
266
|
|
|
186
|
-
|
|
187
|
-
|
|
267
|
+
// generate entry file.
|
|
188
268
|
if (config.source.enableAsyncEntry) {
|
|
189
269
|
const {
|
|
190
270
|
code: asyncEntryCode
|
|
@@ -192,11 +272,8 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
192
272
|
entrypoint,
|
|
193
273
|
code: `import('./${_constants.ENTRY_BOOTSTRAP_FILE_NAME}');`
|
|
194
274
|
});
|
|
195
|
-
|
|
196
275
|
_utils.fs.outputFileSync(entryFile, asyncEntryCode, 'utf8');
|
|
197
|
-
|
|
198
276
|
const bootstrapFile = _path.default.resolve(internalDirectory, `./${entryName}/${_constants.ENTRY_BOOTSTRAP_FILE_NAME}`);
|
|
199
|
-
|
|
200
277
|
_utils.fs.outputFileSync(bootstrapFile, code, 'utf8');
|
|
201
278
|
} else {
|
|
202
279
|
_utils.fs.outputFileSync(entryFile, code, 'utf8');
|
|
@@ -204,5 +281,4 @@ const generateCode = async (appContext, config, entrypoints, api) => {
|
|
|
204
281
|
}
|
|
205
282
|
}
|
|
206
283
|
};
|
|
207
|
-
|
|
208
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,24 +56,21 @@ 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);
|
|
79
67
|
const found = defaults.find(({
|
|
80
68
|
entryName,
|
|
81
|
-
entry
|
|
82
|
-
|
|
69
|
+
entry,
|
|
70
|
+
nestedRoutesEntry: _nestedRoutesEntry = ''
|
|
71
|
+
}) => entryName === packageName || _path.default.dirname(entry) === entriesDirAbs || _path.default.dirname(_nestedRoutesEntry) === entriesDirAbs);
|
|
83
72
|
found && (found.entryName = _utils.MAIN_ENTRY_NAME);
|
|
84
73
|
}
|
|
85
|
-
|
|
86
74
|
return defaults;
|
|
87
75
|
};
|
|
88
|
-
|
|
89
76
|
exports.getBundleEntry = getBundleEntry;
|