@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
|
@@ -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 { fs, getRouteId } from '@modern-js/utils';
|
|
9
6
|
import { JS_EXTENSIONS } from "./constants";
|
|
@@ -14,37 +11,30 @@ const LOADING_FILE = 'loading';
|
|
|
14
11
|
const ERROR_FILE = 'error';
|
|
15
12
|
const LOADER_FILE = 'loader';
|
|
16
13
|
const conventionNames = [LAYOUT_FILE, PAGE_FILE, LOADING_FILE, ERROR_FILE, LOADER_FILE];
|
|
17
|
-
|
|
18
14
|
const replaceDynamicPath = routePath => {
|
|
19
15
|
return routePath.replace(/\[(.*?)\]/g, ':$1');
|
|
20
16
|
};
|
|
21
|
-
|
|
22
|
-
const createIndexRoute = (routeInfo, rootDir, filename) => {
|
|
17
|
+
const createIndexRoute = (routeInfo, rootDir, filename, entryName) => {
|
|
23
18
|
return createRoute(_objectSpread(_objectSpread({}, routeInfo), {}, {
|
|
24
19
|
index: true,
|
|
25
20
|
children: undefined
|
|
26
|
-
}), rootDir, filename);
|
|
21
|
+
}), rootDir, filename, entryName);
|
|
27
22
|
};
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const id = getRouteId(filename, rootDir);
|
|
23
|
+
const createRoute = (routeInfo, rootDir, filename, entryName) => {
|
|
24
|
+
const id = getRouteId(filename, rootDir, entryName);
|
|
31
25
|
return _objectSpread(_objectSpread({}, routeInfo), {}, {
|
|
32
26
|
id,
|
|
33
27
|
type: 'nested'
|
|
34
28
|
});
|
|
35
29
|
};
|
|
36
|
-
|
|
37
|
-
export const walk = async (dirname, rootDir, alias) => {
|
|
30
|
+
export const walk = async (dirname, rootDir, alias, entryName) => {
|
|
38
31
|
if (!(await fs.pathExists(dirname))) {
|
|
39
32
|
return null;
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
const isDirectory = (await fs.stat(dirname)).isDirectory();
|
|
43
|
-
|
|
44
35
|
if (!isDirectory) {
|
|
45
36
|
return null;
|
|
46
37
|
}
|
|
47
|
-
|
|
48
38
|
const relativeDir = path.relative(rootDir, dirname);
|
|
49
39
|
const pathSegments = relativeDir.split(path.sep);
|
|
50
40
|
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
@@ -52,65 +42,52 @@ export const walk = async (dirname, rootDir, alias) => {
|
|
|
52
42
|
const isPathlessLayout = lastSegment.startsWith('__');
|
|
53
43
|
const isWithoutLayoutPath = lastSegment.includes('.');
|
|
54
44
|
let routePath = isRoot || isPathlessLayout ? '/' : `${lastSegment}`;
|
|
55
|
-
|
|
56
45
|
if (isWithoutLayoutPath) {
|
|
57
46
|
routePath = lastSegment.split('.').join('/');
|
|
58
47
|
}
|
|
59
|
-
|
|
60
48
|
routePath = replaceDynamicPath(routePath);
|
|
61
49
|
const route = {
|
|
62
50
|
path: routePath,
|
|
63
51
|
children: []
|
|
64
52
|
};
|
|
65
53
|
const items = await fs.readdir(dirname);
|
|
66
|
-
|
|
67
54
|
for (const item of items) {
|
|
68
55
|
const itemPath = path.join(dirname, item);
|
|
69
56
|
const extname = path.extname(item);
|
|
70
57
|
const itemWithoutExt = item.slice(0, -extname.length);
|
|
71
58
|
const isDirectory = (await fs.stat(itemPath)).isDirectory();
|
|
72
|
-
|
|
73
59
|
if (isDirectory) {
|
|
74
|
-
const childRoute = await walk(itemPath, rootDir, alias);
|
|
75
|
-
|
|
60
|
+
const childRoute = await walk(itemPath, rootDir, alias, entryName);
|
|
76
61
|
if (childRoute) {
|
|
77
62
|
var _route$children;
|
|
78
|
-
|
|
79
63
|
(_route$children = route.children) === null || _route$children === void 0 ? void 0 : _route$children.push(childRoute);
|
|
80
64
|
}
|
|
81
65
|
}
|
|
82
|
-
|
|
83
66
|
if (extname && (!JS_EXTENSIONS.includes(extname) || !conventionNames.includes(itemWithoutExt))) {
|
|
84
67
|
continue;
|
|
85
68
|
}
|
|
86
|
-
|
|
87
69
|
if (itemWithoutExt === LAYOUT_FILE) {
|
|
88
70
|
route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
89
71
|
}
|
|
90
|
-
|
|
91
72
|
if (itemWithoutExt === PAGE_FILE) {
|
|
92
73
|
var _route$children2;
|
|
93
|
-
|
|
94
74
|
const childRoute = createIndexRoute({
|
|
95
75
|
_component: replaceWithAlias(alias.basename, itemPath, alias.name)
|
|
96
|
-
}, rootDir, itemPath);
|
|
76
|
+
}, rootDir, itemPath, entryName);
|
|
97
77
|
(_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.push(childRoute);
|
|
98
78
|
}
|
|
99
|
-
|
|
100
79
|
if (itemWithoutExt === LOADER_FILE) {
|
|
101
80
|
route.loader = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
102
81
|
}
|
|
103
|
-
|
|
104
82
|
if (itemWithoutExt === LOADING_FILE) {
|
|
105
83
|
route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
106
84
|
}
|
|
107
|
-
|
|
108
85
|
if (itemWithoutExt === ERROR_FILE) {
|
|
109
86
|
route.error = replaceWithAlias(alias.basename, itemPath, alias.name);
|
|
110
87
|
}
|
|
111
88
|
}
|
|
89
|
+
const finalRoute = createRoute(route, rootDir, path.join(dirname, `${LAYOUT_FILE}.ts`), entryName);
|
|
112
90
|
|
|
113
|
-
const finalRoute = createRoute(route, rootDir, path.join(dirname, `${LAYOUT_FILE}.ts`));
|
|
114
91
|
/**
|
|
115
92
|
* when the url is /, the __auth/layout.tsx component should not be rendered
|
|
116
93
|
* - routes
|
|
@@ -118,10 +95,8 @@ export const walk = async (dirname, rootDir, alias) => {
|
|
|
118
95
|
* - layout.tsx
|
|
119
96
|
* - layout.tsx
|
|
120
97
|
*/
|
|
121
|
-
|
|
122
98
|
if (isPathlessLayout) {
|
|
123
99
|
delete finalRoute.path;
|
|
124
100
|
}
|
|
125
|
-
|
|
126
101
|
return finalRoute;
|
|
127
102
|
};
|
|
@@ -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
|
export const index = ({
|
|
8
5
|
mountId,
|
|
9
6
|
imports,
|
|
@@ -88,37 +85,28 @@ export const routesForServer = ({
|
|
|
88
85
|
basename
|
|
89
86
|
} = alias;
|
|
90
87
|
const loaders = [];
|
|
91
|
-
|
|
92
88
|
const traverseRouteTree = route => {
|
|
93
89
|
let children;
|
|
94
|
-
|
|
95
90
|
if ('children' in route && route.children) {
|
|
96
91
|
var _route$children;
|
|
97
|
-
|
|
98
92
|
children = route === null || route === void 0 ? void 0 : (_route$children = route.children) === null || _route$children === void 0 ? void 0 : _route$children.map(traverseRouteTree);
|
|
99
93
|
}
|
|
100
|
-
|
|
101
94
|
let loader;
|
|
102
|
-
|
|
103
95
|
if (route.type === 'nested') {
|
|
104
96
|
if (route.loader) {
|
|
105
97
|
loaders.push(route.loader);
|
|
106
98
|
loader = `loader_${loaders.length - 1}`;
|
|
107
99
|
}
|
|
108
100
|
}
|
|
109
|
-
|
|
110
101
|
const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
|
|
111
102
|
loader,
|
|
112
103
|
children
|
|
113
104
|
});
|
|
114
|
-
|
|
115
105
|
return finalRoute;
|
|
116
106
|
};
|
|
117
|
-
|
|
118
107
|
let routesCode = `
|
|
119
108
|
export const routes = [
|
|
120
109
|
`;
|
|
121
|
-
|
|
122
110
|
for (const route of routes) {
|
|
123
111
|
if ('type' in route) {
|
|
124
112
|
const newRoute = traverseRouteTree(route);
|
|
@@ -127,7 +115,6 @@ export const routesForServer = ({
|
|
|
127
115
|
routesCode += `${JSON.stringify(route, null, 2)}`;
|
|
128
116
|
}
|
|
129
117
|
}
|
|
130
|
-
|
|
131
118
|
routesCode += `\n];`;
|
|
132
119
|
const importLoadersCode = loaders.map((loader, index) => {
|
|
133
120
|
const realLoaderPath = loader.replace(name, basename);
|
|
@@ -141,87 +128,85 @@ export const routesForServer = ({
|
|
|
141
128
|
export const fileSystemRoutes = ({
|
|
142
129
|
routes,
|
|
143
130
|
ssrMode,
|
|
144
|
-
nestedRoutesEntry
|
|
131
|
+
nestedRoutesEntry,
|
|
132
|
+
entryName
|
|
145
133
|
}) => {
|
|
146
|
-
|
|
134
|
+
// The legacy mode and pages dir routes should use loadable
|
|
135
|
+
// nested routes + renderTostring should use loadable.lazy
|
|
136
|
+
// nested routes + renderToStream should use react.lazy
|
|
137
|
+
const importLazyCode = `
|
|
138
|
+
import { lazy } from "react";
|
|
139
|
+
import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"
|
|
140
|
+
`;
|
|
147
141
|
let dataLoaderPath = '';
|
|
148
|
-
|
|
149
142
|
if (ssrMode) {
|
|
150
143
|
dataLoaderPath = require.resolve(`@modern-js/plugin-data-loader/loader`);
|
|
151
|
-
|
|
152
144
|
if (nestedRoutesEntry) {
|
|
153
|
-
dataLoaderPath = `${dataLoaderPath}?routesDir=${nestedRoutesEntry}!`;
|
|
145
|
+
dataLoaderPath = `${dataLoaderPath}?routesDir=${nestedRoutesEntry}&entryName=${entryName}!`;
|
|
154
146
|
}
|
|
155
147
|
}
|
|
156
|
-
|
|
157
148
|
const loadings = [];
|
|
158
149
|
const errors = [];
|
|
159
150
|
const loaders = [];
|
|
160
|
-
|
|
161
151
|
const traverseRouteTree = route => {
|
|
162
152
|
let children;
|
|
163
|
-
|
|
164
153
|
if ('children' in route && route.children) {
|
|
165
154
|
var _route$children2;
|
|
166
|
-
|
|
167
155
|
children = route === null || route === void 0 ? void 0 : (_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.map(traverseRouteTree);
|
|
168
156
|
}
|
|
169
|
-
|
|
170
157
|
let loading;
|
|
171
158
|
let error;
|
|
172
159
|
let loader;
|
|
173
|
-
|
|
160
|
+
let component = '';
|
|
174
161
|
if (route.type === 'nested') {
|
|
175
162
|
if (route.loading) {
|
|
176
163
|
loadings.push(route.loading);
|
|
177
164
|
loading = `loading_${loadings.length - 1}`;
|
|
178
165
|
}
|
|
179
|
-
|
|
180
166
|
if (route.error) {
|
|
181
167
|
errors.push(route.error);
|
|
182
168
|
error = `error_${errors.length - 1}`;
|
|
183
169
|
}
|
|
184
|
-
|
|
185
170
|
if (route.loader) {
|
|
186
171
|
loaders.push(route.loader);
|
|
187
172
|
loader = `loader_${loaders.length - 1}`;
|
|
188
173
|
}
|
|
174
|
+
if (route._component) {
|
|
175
|
+
if (ssrMode === 'stream') {
|
|
176
|
+
component = `lazy(() => import(/* webpackChunkName: "${route.id}" */ /* webpackMode: "lazy-once" */ '${route._component}'))`;
|
|
177
|
+
} else {
|
|
178
|
+
component = `loadable(() => import(/* webpackChunkName: "${route.id}" */ /* webpackMode: "lazy-once" */ '${route._component}'))`;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} else if (route._component) {
|
|
182
|
+
component = `loadable(() => import('${route._component}'))`;
|
|
189
183
|
}
|
|
190
|
-
|
|
191
184
|
const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
|
|
192
185
|
loading,
|
|
193
186
|
loader,
|
|
194
187
|
error,
|
|
195
188
|
children
|
|
196
189
|
});
|
|
197
|
-
|
|
198
190
|
if (route._component) {
|
|
199
|
-
const component = ssrMode === 'stream' ? `lazy(() => import('${route._component}'))` : `loadable(() => import('${route._component}'))`;
|
|
200
191
|
finalRoute.component = component;
|
|
201
192
|
}
|
|
202
|
-
|
|
203
193
|
return finalRoute;
|
|
204
194
|
};
|
|
205
|
-
|
|
206
195
|
let routeComponentsCode = `
|
|
207
196
|
export const routes = [
|
|
208
197
|
`;
|
|
209
|
-
|
|
210
198
|
for (const route of routes) {
|
|
211
199
|
if ('type' in route) {
|
|
212
200
|
const newRoute = traverseRouteTree(route);
|
|
213
|
-
routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable
|
|
201
|
+
routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, '$1').replace(/"(loadableLazy.*\))"/g, '$1').replace(/"(lazy.*\))"/g, '$1').replace(/"(loading_[^"])"/g, '$1').replace(/"(loader_[^"])"/g, '$1').replace(/"(error_[^"])"/g, '$1').replace(/\\"/g, '"')},`;
|
|
214
202
|
} else {
|
|
215
|
-
const component =
|
|
216
|
-
|
|
203
|
+
const component = `loadable(() => import('${route._component}'))`;
|
|
217
204
|
const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
|
|
218
205
|
component
|
|
219
206
|
});
|
|
220
|
-
|
|
221
207
|
routeComponentsCode += `${JSON.stringify(finalRoute, null, 2).replace(/"(loadable[^"]*)"/g, '$1').replace(/"(lazy[^"]*)"/g, '$1')},`;
|
|
222
208
|
}
|
|
223
209
|
}
|
|
224
|
-
|
|
225
210
|
routeComponentsCode += `\n];`;
|
|
226
211
|
const importLoadingCode = loadings.map((loading, index) => {
|
|
227
212
|
return `import loading_${index} from '${loading}';\n`;
|
|
@@ -4,7 +4,6 @@ import { isReact18, normalizeToPosixPath } from '@modern-js/utils';
|
|
|
4
4
|
import { FILE_SYSTEM_ROUTES_FILE_NAME } from "./constants";
|
|
5
5
|
export const walkDirectory = dir => fs.readdirSync(dir).reduce((previous, filename) => {
|
|
6
6
|
const filePath = path.join(dir, filename);
|
|
7
|
-
|
|
8
7
|
if (fs.statSync(filePath).isDirectory()) {
|
|
9
8
|
return [...previous, ...walkDirectory(filePath)];
|
|
10
9
|
} else {
|
|
@@ -47,7 +46,6 @@ export const getDefaultImports = ({
|
|
|
47
46
|
}],
|
|
48
47
|
value: normalizeToPosixPath(customBootstrap.replace(srcDirectory, internalSrcAlias))
|
|
49
48
|
}].filter(Boolean);
|
|
50
|
-
|
|
51
49
|
if (fileSystemRoutes) {
|
|
52
50
|
const route = {
|
|
53
51
|
specifiers: [{
|
|
@@ -55,7 +53,6 @@ export const getDefaultImports = ({
|
|
|
55
53
|
}],
|
|
56
54
|
value: normalizeToPosixPath(`${internalDirAlias}/${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`)
|
|
57
55
|
};
|
|
58
|
-
|
|
59
56
|
if (fileSystemRoutes.globalApp) {
|
|
60
57
|
imports.push({
|
|
61
58
|
specifiers: [{
|
|
@@ -66,7 +63,6 @@ export const getDefaultImports = ({
|
|
|
66
63
|
} else {
|
|
67
64
|
route.initialize = 'const App = false;';
|
|
68
65
|
}
|
|
69
|
-
|
|
70
66
|
imports.push(route);
|
|
71
67
|
} else {
|
|
72
68
|
imports.push({
|
|
@@ -76,18 +72,15 @@ export const getDefaultImports = ({
|
|
|
76
72
|
value: normalizeToPosixPath(entry.replace(srcDirectory, internalSrcAlias))
|
|
77
73
|
});
|
|
78
74
|
}
|
|
79
|
-
|
|
80
75
|
return imports;
|
|
81
76
|
};
|
|
82
77
|
export const isRouteComponentFile = filePath => {
|
|
83
78
|
if (/\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/.test(filePath)) {
|
|
84
79
|
return false;
|
|
85
80
|
}
|
|
86
|
-
|
|
87
81
|
if (['.js', '.jsx', '.ts', '.tsx'].includes(path.extname(filePath))) {
|
|
88
82
|
return true;
|
|
89
83
|
}
|
|
90
|
-
|
|
91
84
|
return false;
|
|
92
85
|
};
|
|
93
86
|
export const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
|
|
@@ -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 { join } from 'path';
|
|
8
5
|
import { mergeBuilderConfig } from '@modern-js/builder-shared';
|
|
9
6
|
import { template as lodashTemplate } from '@modern-js/utils/lodash';
|
|
@@ -12,13 +9,12 @@ import { getEntryOptions } from '@modern-js/utils';
|
|
|
12
9
|
import { BottomTemplatePlugin } from "../webpackPlugins/htmlBottomTemplate";
|
|
13
10
|
import { HtmlAsyncChunkPlugin } from "../webpackPlugins/htmlAsyncChunkPlugin";
|
|
14
11
|
import { createCopyPattern } from "../share";
|
|
15
|
-
|
|
12
|
+
import RouterPlugin from "../webpackPlugins/routerPlugin";
|
|
16
13
|
/**
|
|
17
14
|
* Provides default configuration consistent with `@modern-js/webpack`
|
|
18
15
|
*/
|
|
19
16
|
export const PluginCompatModern = (appContext, modernConfig, options) => ({
|
|
20
17
|
name: 'builder-plugin-compat-modern',
|
|
21
|
-
|
|
22
18
|
setup(api) {
|
|
23
19
|
api.modifyBuilderConfig(config => {
|
|
24
20
|
if (isStreamingSSR(modernConfig)) {
|
|
@@ -28,7 +24,6 @@ export const PluginCompatModern = (appContext, modernConfig, options) => ({
|
|
|
28
24
|
}
|
|
29
25
|
});
|
|
30
26
|
}
|
|
31
|
-
|
|
32
27
|
return config;
|
|
33
28
|
});
|
|
34
29
|
api.modifyWebpackChain((chain, {
|
|
@@ -36,8 +31,8 @@ export const PluginCompatModern = (appContext, modernConfig, options) => ({
|
|
|
36
31
|
CHAIN_ID,
|
|
37
32
|
isProd
|
|
38
33
|
}) => {
|
|
39
|
-
const builderNormalizedConfig = api.getNormalizedConfig();
|
|
40
|
-
|
|
34
|
+
const builderNormalizedConfig = api.getNormalizedConfig();
|
|
35
|
+
// set webpack config name
|
|
41
36
|
if (target === 'node') {
|
|
42
37
|
chain.name('server');
|
|
43
38
|
} else if (target === 'modern-web') {
|
|
@@ -45,13 +40,12 @@ export const PluginCompatModern = (appContext, modernConfig, options) => ({
|
|
|
45
40
|
} else {
|
|
46
41
|
chain.name('client');
|
|
47
42
|
}
|
|
43
|
+
chain.resolve.modules.add('node_modules').add(join(api.context.rootPath, 'node_modules'));
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
// apply node compat
|
|
51
46
|
if (target === 'node') {
|
|
52
47
|
applyNodeCompat(chain, modernConfig, isProd);
|
|
53
48
|
}
|
|
54
|
-
|
|
55
49
|
if (isHtmlEnabled(builderNormalizedConfig, target)) {
|
|
56
50
|
applyBottomHtmlWebpackPlugin({
|
|
57
51
|
api,
|
|
@@ -65,36 +59,38 @@ export const PluginCompatModern = (appContext, modernConfig, options) => ({
|
|
|
65
59
|
CHAIN_ID,
|
|
66
60
|
modernConfig
|
|
67
61
|
});
|
|
68
|
-
}
|
|
69
|
-
// const copyPatterns = createCopyPatterns(chain, appContext, modernConfig);
|
|
70
|
-
|
|
62
|
+
}
|
|
71
63
|
|
|
64
|
+
// apply copy plugin
|
|
65
|
+
// const copyPatterns = createCopyPatterns(chain, appContext, modernConfig);
|
|
72
66
|
const defaultCopyPattern = createCopyPattern(appContext, modernConfig, 'public', chain);
|
|
73
67
|
chain.plugin(CHAIN_ID.PLUGIN.COPY).tap(args => {
|
|
74
68
|
var _args$;
|
|
75
|
-
|
|
76
69
|
return [{
|
|
77
70
|
patterns: [...(((_args$ = args[0]) === null || _args$ === void 0 ? void 0 : _args$.patterns) || []), defaultCopyPattern]
|
|
78
71
|
}];
|
|
79
72
|
});
|
|
80
|
-
|
|
73
|
+
const {
|
|
74
|
+
entrypoints
|
|
75
|
+
} = appContext;
|
|
76
|
+
const existNestedRoutes = entrypoints.some(entrypoint => entrypoint.nestedRoutesEntry);
|
|
77
|
+
chain.plugin('route-plugin').use(RouterPlugin, [{
|
|
78
|
+
existNestedRoutes
|
|
79
|
+
}]);
|
|
81
80
|
function isHtmlEnabled(config, target) {
|
|
82
81
|
var _config$tools;
|
|
83
|
-
|
|
84
82
|
return ((_config$tools = config.tools) === null || _config$tools === void 0 ? void 0 : _config$tools.htmlPlugin) !== false && target !== 'node' && target !== 'web-worker';
|
|
85
83
|
}
|
|
86
84
|
});
|
|
87
|
-
|
|
88
85
|
if (options) {
|
|
89
86
|
applyCallbacks(api, options);
|
|
90
87
|
}
|
|
91
88
|
}
|
|
92
|
-
|
|
93
89
|
});
|
|
90
|
+
|
|
94
91
|
/**
|
|
95
92
|
* register builder hooks callback
|
|
96
93
|
*/
|
|
97
|
-
|
|
98
94
|
function applyCallbacks(api, options) {
|
|
99
95
|
options.onAfterBuild && api.onAfterBuild(options.onAfterBuild);
|
|
100
96
|
options.onAfterCreateCompiler && api.onAfterCreateCompiler(options.onAfterCreateCompiler);
|
|
@@ -105,40 +101,35 @@ function applyCallbacks(api, options) {
|
|
|
105
101
|
options.onDevCompileDone && api.onDevCompileDone(options.onDevCompileDone);
|
|
106
102
|
options.onExit && api.onExit(options.onExit);
|
|
107
103
|
}
|
|
104
|
+
|
|
108
105
|
/**
|
|
109
106
|
* compat some config, if target is `node`
|
|
110
107
|
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
108
|
function applyNodeCompat(chain, modernConfig, isProd) {
|
|
114
109
|
// apply node resolve extensions
|
|
115
110
|
for (const ext of ['.node.js', '.node.jsx', '.node.ts', '.node.tsx']) {
|
|
116
111
|
chain.resolve.extensions.prepend(ext);
|
|
117
|
-
}
|
|
118
|
-
|
|
112
|
+
}
|
|
119
113
|
|
|
114
|
+
// apply filterEntriesBySSRConfig
|
|
120
115
|
filterEntriesBySSRConfig(isProd, chain, modernConfig.server, modernConfig.output);
|
|
121
|
-
|
|
122
116
|
function filterEntriesBySSRConfig(isProd, chain, serverConfig, outputConfig) {
|
|
123
117
|
var _outputConfig$ssg;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
118
|
+
const entries = chain.entryPoints.entries();
|
|
119
|
+
// if prod and ssg config is true or function
|
|
127
120
|
if (isProd && ((outputConfig === null || outputConfig === void 0 ? void 0 : outputConfig.ssg) === true || typeof (outputConfig === null || outputConfig === void 0 ? void 0 : (_outputConfig$ssg = outputConfig.ssg) === null || _outputConfig$ssg === void 0 ? void 0 : _outputConfig$ssg[0]) === 'function')) {
|
|
128
121
|
return;
|
|
129
|
-
}
|
|
130
|
-
// `ssg: {}` is not allowed if multi entry
|
|
131
|
-
|
|
122
|
+
}
|
|
132
123
|
|
|
124
|
+
// if single entry has ssg config
|
|
125
|
+
// `ssg: {}` is not allowed if multi entry
|
|
133
126
|
const entryNames = Object.keys(entries);
|
|
134
|
-
|
|
135
127
|
if (isProd && entryNames.length === 1 && outputConfig !== null && outputConfig !== void 0 && outputConfig.ssg) {
|
|
136
128
|
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
129
|
+
}
|
|
139
130
|
|
|
131
|
+
// collect all ssg entries
|
|
140
132
|
const ssgEntries = [];
|
|
141
|
-
|
|
142
133
|
if (isProd && outputConfig !== null && outputConfig !== void 0 && outputConfig.ssg) {
|
|
143
134
|
const {
|
|
144
135
|
ssg
|
|
@@ -149,7 +140,6 @@ function applyNodeCompat(chain, modernConfig, isProd) {
|
|
|
149
140
|
}
|
|
150
141
|
});
|
|
151
142
|
}
|
|
152
|
-
|
|
153
143
|
const {
|
|
154
144
|
ssr,
|
|
155
145
|
ssrByEntries
|
|
@@ -161,11 +151,10 @@ function applyNodeCompat(chain, modernConfig, isProd) {
|
|
|
161
151
|
});
|
|
162
152
|
}
|
|
163
153
|
}
|
|
154
|
+
|
|
164
155
|
/**
|
|
165
156
|
* inject bottom template
|
|
166
157
|
*/
|
|
167
|
-
|
|
168
|
-
|
|
169
158
|
function applyBottomHtmlWebpackPlugin({
|
|
170
159
|
api,
|
|
171
160
|
chain,
|
|
@@ -181,29 +170,24 @@ function applyBottomHtmlWebpackPlugin({
|
|
|
181
170
|
title: getEntryOptions(entryName, modernConfig.output.title, modernConfig.output.titleByEntries, appContext.packageName),
|
|
182
171
|
mountId: modernConfig.output.mountId
|
|
183
172
|
}, getEntryOptions(entryName, modernConfig.output.templateParameters, modernConfig.output.templateParametersByEntries, appContext.packageName));
|
|
184
|
-
|
|
185
173
|
chain.plugin(`${CHAIN_ID.PLUGIN.HTML}-${entryName}`).tap(args => [_objectSpread(_objectSpread({}, args[0] || {}), {}, {
|
|
186
174
|
__internal__: true,
|
|
187
175
|
bottomTemplate: appContext.htmlTemplates[`__${entryName}-bottom__`] && lodashTemplate(appContext.htmlTemplates[`__${entryName}-bottom__`])(baseTemplateParams)
|
|
188
176
|
})]);
|
|
189
177
|
}
|
|
190
|
-
|
|
191
178
|
chain.plugin(CHAIN_ID.PLUGIN.BOTTOM_TEMPLATE).use(BottomTemplatePlugin, [HtmlWebpackPlugin]);
|
|
192
179
|
}
|
|
193
|
-
|
|
194
180
|
const isStreamingSSR = userConfig => {
|
|
195
181
|
const isStreaming = ssr => ssr && typeof ssr === 'object' && ssr.mode === 'stream';
|
|
196
|
-
|
|
197
182
|
const {
|
|
198
183
|
server
|
|
199
184
|
} = userConfig;
|
|
200
|
-
|
|
201
185
|
if (isStreaming(server.ssr)) {
|
|
202
186
|
return true;
|
|
203
|
-
}
|
|
204
|
-
// we regard the whole app as streaming ssr only if one entry meets the requirement.
|
|
205
|
-
|
|
187
|
+
}
|
|
206
188
|
|
|
189
|
+
// Since we cannot apply different plugins for different entries,
|
|
190
|
+
// we regard the whole app as streaming ssr only if one entry meets the requirement.
|
|
207
191
|
if (server !== null && server !== void 0 && server.ssrByEntries && typeof server.ssrByEntries === 'object') {
|
|
208
192
|
for (const name of Object.keys(server.ssrByEntries)) {
|
|
209
193
|
if (isStreaming(server.ssrByEntries[name])) {
|
|
@@ -211,10 +195,8 @@ const isStreamingSSR = userConfig => {
|
|
|
211
195
|
}
|
|
212
196
|
}
|
|
213
197
|
}
|
|
214
|
-
|
|
215
198
|
return false;
|
|
216
199
|
};
|
|
217
|
-
|
|
218
200
|
function applyAsyncChunkHtmlPlugin({
|
|
219
201
|
chain,
|
|
220
202
|
modernConfig,
|
|
@@ -18,8 +18,9 @@ export function createHtmlConfig(normalizedConfig, appContext) {
|
|
|
18
18
|
} = normalizedConfig.output;
|
|
19
19
|
const {
|
|
20
20
|
configDir
|
|
21
|
-
} = normalizedConfig.source;
|
|
21
|
+
} = normalizedConfig.source;
|
|
22
22
|
|
|
23
|
+
// transform Modernjs `output.scriptExt` to Builder `html.crossorigin` configuration
|
|
23
24
|
const builderCrossorigin = createBuilderCrossorigin(scriptExt);
|
|
24
25
|
const builderAppIcon = createBuilderAppIcon(configDir, appContext);
|
|
25
26
|
const builderFavicon = createBuilderFavicon(favicon, configDir, appContext);
|
|
@@ -48,7 +49,6 @@ export function createBuilderAppIcon(configDir, appContext) {
|
|
|
48
49
|
}
|
|
49
50
|
export function createBuilderCrossorigin(scriptExt) {
|
|
50
51
|
var _scriptExtCustomConfi;
|
|
51
|
-
|
|
52
52
|
const scriptExtCustomConfig = scriptExt === null || scriptExt === void 0 ? void 0 : scriptExt.custom;
|
|
53
53
|
return scriptExtCustomConfig !== null && scriptExtCustomConfig !== void 0 && (_scriptExtCustomConfi = scriptExtCustomConfig.test) !== null && _scriptExtCustomConfi !== void 0 && _scriptExtCustomConfi.test('.js') && (scriptExtCustomConfig === null || scriptExtCustomConfig === void 0 ? void 0 : scriptExtCustomConfig.attribute) === 'crossorigin' ? scriptExtCustomConfig.value : undefined;
|
|
54
54
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createCopyPattern } from "./share";
|
|
2
2
|
export function createOutputConfig(normalizedConfig, appContext) {
|
|
3
3
|
// TODO: add `externals` options in Modern.
|
|
4
|
+
|
|
4
5
|
const {
|
|
5
6
|
assetPrefix,
|
|
6
7
|
copy,
|
|
@@ -13,6 +14,7 @@ export function createOutputConfig(normalizedConfig, appContext) {
|
|
|
13
14
|
disableInlineRuntimeChunk,
|
|
14
15
|
disableMinimize,
|
|
15
16
|
disableSourceMap,
|
|
17
|
+
disableTsChecker,
|
|
16
18
|
enableCssModuleTSDeclaration,
|
|
17
19
|
enableInlineScripts,
|
|
18
20
|
enableInlineStyles,
|
|
@@ -33,10 +35,10 @@ export function createOutputConfig(normalizedConfig, appContext) {
|
|
|
33
35
|
js: jsPath,
|
|
34
36
|
html: htmlPath,
|
|
35
37
|
// `@modern-js/webpack` output all media files to `dist/media` by default
|
|
36
|
-
svg: mediaPath || '
|
|
37
|
-
image: mediaPath || '
|
|
38
|
-
font: mediaPath || '
|
|
39
|
-
media: mediaPath || '
|
|
38
|
+
svg: mediaPath || 'media',
|
|
39
|
+
image: mediaPath || 'media',
|
|
40
|
+
font: mediaPath || 'media',
|
|
41
|
+
media: mediaPath || 'media'
|
|
40
42
|
},
|
|
41
43
|
dataUriLimit: {
|
|
42
44
|
svg: dataUriLimit,
|
|
@@ -48,6 +50,7 @@ export function createOutputConfig(normalizedConfig, appContext) {
|
|
|
48
50
|
disableInlineRuntimeChunk,
|
|
49
51
|
disableMinimize,
|
|
50
52
|
disableSourceMap,
|
|
53
|
+
disableTsChecker,
|
|
51
54
|
enableCssModuleTSDeclaration,
|
|
52
55
|
enableInlineScripts,
|
|
53
56
|
enableInlineStyles,
|