@modern-js/app-tools 2.54.2 → 2.54.3-alpha.4
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/plugins/deploy/dependencies/index.js +26 -17
- package/dist/cjs/plugins/deploy/dependencies/utils.js +14 -21
- package/dist/cjs/plugins/deploy/exports.js +28 -0
- package/dist/cjs/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/cjs/plugins/deploy/platforms/node.js +8 -3
- package/dist/cjs/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/esm/plugins/deploy/dependencies/index.js +94 -69
- package/dist/esm/plugins/deploy/dependencies/utils.js +19 -40
- package/dist/esm/plugins/deploy/exports.js +4 -0
- package/dist/esm/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/esm/plugins/deploy/platforms/node.js +8 -3
- package/dist/esm/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/esm-node/plugins/deploy/dependencies/index.js +27 -18
- package/dist/esm-node/plugins/deploy/dependencies/utils.js +15 -22
- package/dist/esm-node/plugins/deploy/exports.js +4 -0
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +7 -3
- package/dist/esm-node/plugins/deploy/platforms/node.js +8 -3
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +7 -3
- package/dist/js/modern/analyze/constants.js +15 -0
- package/dist/js/modern/analyze/generateCode.js +179 -0
- package/dist/js/modern/analyze/getBundleEntry.js +75 -0
- package/dist/js/modern/analyze/getClientRoutes.js +219 -0
- package/dist/js/modern/analyze/getFileSystemEntry.js +74 -0
- package/dist/js/modern/analyze/getHtmlTemplate.js +82 -0
- package/dist/js/modern/analyze/getServerRoutes.js +192 -0
- package/dist/js/modern/analyze/index.js +148 -0
- package/dist/js/modern/analyze/isDefaultExportFunction.js +32 -0
- package/dist/js/modern/analyze/makeLegalIdentifier.js +16 -0
- package/dist/js/modern/analyze/templates.js +88 -0
- package/dist/js/modern/analyze/utils.js +92 -0
- package/dist/js/modern/commands/build.js +154 -0
- package/dist/js/modern/commands/deploy.js +5 -0
- package/dist/js/modern/commands/dev.js +95 -0
- package/dist/js/modern/commands/index.js +3 -0
- package/dist/js/modern/commands/inspect.js +69 -0
- package/dist/js/modern/commands/start.js +31 -0
- package/dist/js/modern/exports/server.js +1 -0
- package/dist/js/modern/hooks.js +21 -0
- package/dist/js/modern/index.js +109 -0
- package/dist/js/modern/locale/en.js +35 -0
- package/dist/js/modern/locale/index.js +9 -0
- package/dist/js/modern/locale/zh.js +35 -0
- package/dist/js/modern/utils/config.js +78 -0
- package/dist/js/modern/utils/createCompiler.js +61 -0
- package/dist/js/modern/utils/createServer.js +18 -0
- package/dist/js/modern/utils/getSpecifiedEntries.js +36 -0
- package/dist/js/modern/utils/language.js +5 -0
- package/dist/js/modern/utils/printInstructions.js +11 -0
- package/dist/js/modern/utils/routes.js +15 -0
- package/dist/js/modern/utils/types.js +0 -0
- package/dist/js/node/analyze/constants.js +36 -0
- package/dist/js/node/analyze/generateCode.js +208 -0
- package/dist/js/node/analyze/getBundleEntry.js +89 -0
- package/dist/js/node/analyze/getClientRoutes.js +241 -0
- package/dist/js/node/analyze/getFileSystemEntry.js +90 -0
- package/dist/js/node/analyze/getHtmlTemplate.js +106 -0
- package/dist/js/node/analyze/getServerRoutes.js +208 -0
- package/dist/js/node/analyze/index.js +178 -0
- package/dist/js/node/analyze/isDefaultExportFunction.js +50 -0
- package/dist/js/node/analyze/makeLegalIdentifier.js +24 -0
- package/dist/js/node/analyze/templates.js +106 -0
- package/dist/js/node/analyze/utils.js +113 -0
- package/dist/js/node/commands/build.js +174 -0
- package/dist/js/node/commands/deploy.js +14 -0
- package/dist/js/node/commands/dev.js +120 -0
- package/dist/js/node/commands/index.js +44 -0
- package/dist/js/node/commands/inspect.js +98 -0
- package/dist/js/node/commands/start.js +47 -0
- package/dist/js/node/exports/server.js +13 -0
- package/dist/js/node/hooks.js +39 -0
- package/dist/js/node/index.js +141 -0
- package/dist/js/node/locale/en.js +42 -0
- package/dist/js/node/locale/index.js +20 -0
- package/dist/js/node/locale/zh.js +42 -0
- package/dist/js/node/utils/config.js +103 -0
- package/dist/js/node/utils/createCompiler.js +81 -0
- package/dist/js/node/utils/createServer.js +35 -0
- package/dist/js/node/utils/getSpecifiedEntries.js +46 -0
- package/dist/js/node/utils/language.js +13 -0
- package/dist/js/node/utils/printInstructions.js +22 -0
- package/dist/js/node/utils/routes.js +25 -0
- package/dist/js/node/utils/types.js +0 -0
- package/dist/types/plugins/deploy/dependencies/index.d.ts +11 -1
- package/dist/types/plugins/deploy/dependencies/utils.d.ts +7 -1
- package/dist/types/plugins/deploy/exports.d.ts +1 -0
- package/package.json +14 -6
@@ -0,0 +1,106 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.getHtmlTemplate = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _utils = require("@modern-js/utils");
|
11
|
+
|
12
|
+
var _constants = require("./constants");
|
13
|
+
|
14
|
+
var templates = _interopRequireWildcard(require("./templates"));
|
15
|
+
|
16
|
+
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); }
|
17
|
+
|
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; }
|
19
|
+
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
+
|
22
|
+
var PartialPosition;
|
23
|
+
|
24
|
+
(function (PartialPosition) {
|
25
|
+
PartialPosition["TOP"] = "top";
|
26
|
+
PartialPosition["HEAD"] = "head";
|
27
|
+
PartialPosition["BODY"] = "body";
|
28
|
+
PartialPosition["BOTTOM"] = "bottom";
|
29
|
+
PartialPosition["INDEX"] = "index";
|
30
|
+
})(PartialPosition || (PartialPosition = {}));
|
31
|
+
|
32
|
+
const findPartials = (dir, entryName, position) => {
|
33
|
+
if (_utils.fs.existsSync(dir)) {
|
34
|
+
const base = (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, `${position}${ext}`)));
|
35
|
+
const file = entryName ? (0, _utils.findExists)(_constants.HTML_PARTIALS_EXTENSIONS.map(ext => _path.default.resolve(dir, entryName, `${position}${ext}`))) || base : base;
|
36
|
+
return file ? {
|
37
|
+
file,
|
38
|
+
content: _utils.fs.readFileSync(file, 'utf8')
|
39
|
+
} : null;
|
40
|
+
}
|
41
|
+
|
42
|
+
return null;
|
43
|
+
}; // generate html template for
|
44
|
+
|
45
|
+
|
46
|
+
const getHtmlTemplate = async (entrypoints, api, {
|
47
|
+
appContext,
|
48
|
+
config
|
49
|
+
}) => {
|
50
|
+
const {
|
51
|
+
appDirectory,
|
52
|
+
internalDirectory
|
53
|
+
} = appContext;
|
54
|
+
const {
|
55
|
+
source: {
|
56
|
+
configDir
|
57
|
+
}
|
58
|
+
} = config;
|
59
|
+
|
60
|
+
const htmlDir = _path.default.resolve(appDirectory, configDir, _constants.HTML_PARTIALS_FOLDER);
|
61
|
+
|
62
|
+
const htmlTemplates = {};
|
63
|
+
|
64
|
+
for (const entrypoint of entrypoints) {
|
65
|
+
const {
|
66
|
+
entryName
|
67
|
+
} = entrypoint;
|
68
|
+
const name = entrypoints.length === 1 && entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName;
|
69
|
+
const customIndexTemplate = findPartials(htmlDir, name, PartialPosition.INDEX);
|
70
|
+
|
71
|
+
if (customIndexTemplate) {
|
72
|
+
htmlTemplates[entryName] = customIndexTemplate.file;
|
73
|
+
} else {
|
74
|
+
const hookRunners = api.useHookRunners();
|
75
|
+
const {
|
76
|
+
partials
|
77
|
+
} = await hookRunners.htmlPartials({
|
78
|
+
entrypoint,
|
79
|
+
partials: [PartialPosition.TOP, PartialPosition.HEAD, PartialPosition.BODY].reduce((previous, position) => {
|
80
|
+
const found = findPartials(htmlDir, name, position);
|
81
|
+
previous[position] = found ? [found.content] : [];
|
82
|
+
return previous;
|
83
|
+
}, {
|
84
|
+
top: [],
|
85
|
+
head: [],
|
86
|
+
body: []
|
87
|
+
})
|
88
|
+
});
|
89
|
+
|
90
|
+
const templatePath = _path.default.resolve(internalDirectory, entryName, 'index.html');
|
91
|
+
|
92
|
+
_utils.fs.outputFileSync(templatePath, templates.html(partials), 'utf8');
|
93
|
+
|
94
|
+
htmlTemplates[entryName] = templatePath;
|
95
|
+
const bottomTemplate = findPartials(htmlDir, name, PartialPosition.BOTTOM);
|
96
|
+
|
97
|
+
if (bottomTemplate) {
|
98
|
+
htmlTemplates[`__${entryName}-bottom__`] = bottomTemplate.content;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
return htmlTemplates;
|
104
|
+
};
|
105
|
+
|
106
|
+
exports.getHtmlTemplate = getHtmlTemplate;
|
@@ -0,0 +1,208 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.getServerRoutes = void 0;
|
7
|
+
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
9
|
+
|
10
|
+
var _fs = _interopRequireDefault(require("fs"));
|
11
|
+
|
12
|
+
var _utils = require("@modern-js/utils");
|
13
|
+
|
14
|
+
var _utils2 = require("./utils");
|
15
|
+
|
16
|
+
const _excluded = ["path"],
|
17
|
+
_excluded2 = ["path"];
|
18
|
+
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20
|
+
|
21
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
22
|
+
|
23
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
24
|
+
|
25
|
+
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; }
|
26
|
+
|
27
|
+
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; }
|
28
|
+
|
29
|
+
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; }
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Add base url for each server route.
|
33
|
+
* @param baseUrl - Base url from server.baseUrl
|
34
|
+
* @param routes - Server routes.
|
35
|
+
* @returns Server routes with baseUrl prefixed.
|
36
|
+
*/
|
37
|
+
const applyBaseUrl = (baseUrl, routes) => {
|
38
|
+
if (baseUrl) {
|
39
|
+
if (Array.isArray(baseUrl)) {
|
40
|
+
return baseUrl.reduce((previous, current) => [...previous, ...applyBaseUrl(current, routes)], []);
|
41
|
+
} else {
|
42
|
+
return routes.map(route => {
|
43
|
+
const urlPath = (0, _utils.urlJoin)(baseUrl, route.urlPath);
|
44
|
+
return _objectSpread(_objectSpread({}, route), {}, {
|
45
|
+
urlPath: urlPath === '/' ? urlPath : (0, _utils.removeTailSlash)(urlPath)
|
46
|
+
});
|
47
|
+
});
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
return routes;
|
52
|
+
};
|
53
|
+
/**
|
54
|
+
*
|
55
|
+
* @param original - Original entrypoint route info.
|
56
|
+
* @param routeOptions - Custom entrypoint route config from server.routes.
|
57
|
+
* @returns
|
58
|
+
*/
|
59
|
+
|
60
|
+
|
61
|
+
const applyRouteOptions = (original, routeOptions) => {
|
62
|
+
const {
|
63
|
+
route,
|
64
|
+
disableSpa
|
65
|
+
} = routeOptions;
|
66
|
+
original.isSPA = !disableSpa; // set entryPath as dir
|
67
|
+
|
68
|
+
!original.isSPA && (original.entryPath = _path.default.dirname(original.entryPath));
|
69
|
+
let routes;
|
70
|
+
|
71
|
+
if (route) {
|
72
|
+
if (Array.isArray(route)) {
|
73
|
+
routes = route.map(url => {
|
74
|
+
if ((0, _utils.isPlainObject)(url)) {
|
75
|
+
const _ref = url,
|
76
|
+
{
|
77
|
+
path: urlPath
|
78
|
+
} = _ref,
|
79
|
+
other = _objectWithoutProperties(_ref, _excluded);
|
80
|
+
|
81
|
+
return _objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
82
|
+
urlPath
|
83
|
+
});
|
84
|
+
} else {
|
85
|
+
return _objectSpread(_objectSpread({}, original), {}, {
|
86
|
+
urlPath: url
|
87
|
+
});
|
88
|
+
}
|
89
|
+
});
|
90
|
+
} else if ((0, _utils.isPlainObject)(route)) {
|
91
|
+
const _ref2 = route,
|
92
|
+
{
|
93
|
+
path: urlPath
|
94
|
+
} = _ref2,
|
95
|
+
other = _objectWithoutProperties(_ref2, _excluded2);
|
96
|
+
|
97
|
+
routes = [_objectSpread(_objectSpread(_objectSpread({}, original), other), {}, {
|
98
|
+
urlPath
|
99
|
+
})];
|
100
|
+
} else {
|
101
|
+
routes = [_objectSpread(_objectSpread({}, original), {}, {
|
102
|
+
urlPath: route
|
103
|
+
})];
|
104
|
+
}
|
105
|
+
} else {
|
106
|
+
routes = [original];
|
107
|
+
}
|
108
|
+
|
109
|
+
return routes;
|
110
|
+
};
|
111
|
+
/**
|
112
|
+
* Collect routes from entrypoints.
|
113
|
+
* @param entrypoints - Bundle entrypoints.
|
114
|
+
* @param config - Normalized user config.
|
115
|
+
* @returns entrypoint Routes
|
116
|
+
*/
|
117
|
+
|
118
|
+
|
119
|
+
const collectHtmlRoutes = (entrypoints, appContext, config) => {
|
120
|
+
const {
|
121
|
+
output: {
|
122
|
+
htmlPath,
|
123
|
+
disableHtmlFolder,
|
124
|
+
enableModernMode
|
125
|
+
},
|
126
|
+
server: {
|
127
|
+
baseUrl,
|
128
|
+
routes,
|
129
|
+
ssr,
|
130
|
+
ssrByEntries
|
131
|
+
}
|
132
|
+
} = config;
|
133
|
+
const {
|
134
|
+
packageName
|
135
|
+
} = appContext;
|
136
|
+
let htmlRoutes = entrypoints.reduce((previous, {
|
137
|
+
entryName
|
138
|
+
}) => {
|
139
|
+
const entryOptions = (0, _utils.getEntryOptions)(entryName, ssr, ssrByEntries, packageName);
|
140
|
+
const isSSR = Boolean(entryOptions);
|
141
|
+
const {
|
142
|
+
resHeaders
|
143
|
+
} = (routes === null || routes === void 0 ? void 0 : routes[entryName]) || {};
|
144
|
+
let route = {
|
145
|
+
urlPath: `/${entryName === _utils.MAIN_ENTRY_NAME ? '' : entryName}`,
|
146
|
+
entryName,
|
147
|
+
entryPath: (0, _utils.removeLeadingSlash)(_path.default.posix.normalize(`${htmlPath}/${entryName}${disableHtmlFolder ? '.html' : '/index.html'}`)),
|
148
|
+
isSPA: true,
|
149
|
+
isSSR,
|
150
|
+
responseHeaders: resHeaders,
|
151
|
+
enableModernMode: Boolean(enableModernMode),
|
152
|
+
bundle: isSSR ? `${_utils.SERVER_BUNDLE_DIRECTORY}/${entryName}.js` : undefined
|
153
|
+
};
|
154
|
+
|
155
|
+
if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
|
156
|
+
const routeOptions = (0, _utils.isPlainObject)(routes[entryName]) ? routes[entryName] : {
|
157
|
+
route: routes[entryName]
|
158
|
+
};
|
159
|
+
route = applyRouteOptions(route, routeOptions);
|
160
|
+
}
|
161
|
+
|
162
|
+
return Array.isArray(route) ? [...previous, ...route] : [...previous, route];
|
163
|
+
}, []);
|
164
|
+
htmlRoutes = applyBaseUrl(baseUrl, htmlRoutes);
|
165
|
+
return htmlRoutes;
|
166
|
+
};
|
167
|
+
/**
|
168
|
+
* Collect static public file routes from config/public folder.
|
169
|
+
* @param appContext - App context info.
|
170
|
+
* @param config - normalized user config.
|
171
|
+
* @returns Static public file routes.
|
172
|
+
*/
|
173
|
+
|
174
|
+
|
175
|
+
const collectStaticRoutes = (appContext, config) => {
|
176
|
+
const {
|
177
|
+
appDirectory
|
178
|
+
} = appContext;
|
179
|
+
const {
|
180
|
+
source: {
|
181
|
+
configDir
|
182
|
+
},
|
183
|
+
server: {
|
184
|
+
publicRoutes = {}
|
185
|
+
}
|
186
|
+
} = config;
|
187
|
+
|
188
|
+
const publicFolder = _path.default.resolve(appDirectory, configDir, 'public');
|
189
|
+
|
190
|
+
return _fs.default.existsSync(publicFolder) ? (0, _utils2.walkDirectory)(publicFolder).map(filePath => {
|
191
|
+
const urlPath = `${(0, _utils.urlJoin)(toPosix(filePath).slice(toPosix(publicFolder).length))}`;
|
192
|
+
return {
|
193
|
+
urlPath: publicRoutes[(0, _utils.removeLeadingSlash)(urlPath)] || urlPath,
|
194
|
+
isSPA: true,
|
195
|
+
isSSR: false,
|
196
|
+
entryPath: toPosix(_path.default.relative(_path.default.resolve(appDirectory, configDir), filePath))
|
197
|
+
};
|
198
|
+
}) : [];
|
199
|
+
};
|
200
|
+
|
201
|
+
const getServerRoutes = (entrypoints, {
|
202
|
+
appContext,
|
203
|
+
config
|
204
|
+
}) => [...collectHtmlRoutes(entrypoints, appContext, config), ...collectStaticRoutes(appContext, config)];
|
205
|
+
|
206
|
+
exports.getServerRoutes = getServerRoutes;
|
207
|
+
|
208
|
+
const toPosix = pathStr => pathStr.split(_path.default.sep).join(_path.default.posix.sep);
|
@@ -0,0 +1,178 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.modifyServerRoutes = exports.modifyFileSystemRoutes = exports.modifyEntryRuntimePlugins = exports.modifyEntryRenderFunction = exports.modifyEntryImports = exports.modifyEntryExport = exports.modifyAsyncEntry = exports.htmlPartials = exports.default = exports.beforeGenerateRoutes = exports.addRuntimeExports = exports.addDefineTypes = void 0;
|
7
|
+
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
9
|
+
|
10
|
+
var _plugin = require("@modern-js/plugin");
|
11
|
+
|
12
|
+
var _utils = require("@modern-js/utils");
|
13
|
+
|
14
|
+
var _lodash = require("@modern-js/utils/lodash");
|
15
|
+
|
16
|
+
var _utils2 = require("./utils");
|
17
|
+
|
18
|
+
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); }
|
19
|
+
|
20
|
+
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; }
|
21
|
+
|
22
|
+
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; }
|
23
|
+
|
24
|
+
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; }
|
25
|
+
|
26
|
+
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; }
|
27
|
+
|
28
|
+
const debug = (0, _utils.createDebugger)('plugin-analyze');
|
29
|
+
const modifyEntryImports = (0, _plugin.createAsyncWaterfall)();
|
30
|
+
exports.modifyEntryImports = modifyEntryImports;
|
31
|
+
const modifyEntryExport = (0, _plugin.createAsyncWaterfall)();
|
32
|
+
exports.modifyEntryExport = modifyEntryExport;
|
33
|
+
const addRuntimeExports = (0, _plugin.createAsyncWaterfall)();
|
34
|
+
exports.addRuntimeExports = addRuntimeExports;
|
35
|
+
const modifyEntryRuntimePlugins = (0, _plugin.createAsyncWaterfall)();
|
36
|
+
exports.modifyEntryRuntimePlugins = modifyEntryRuntimePlugins;
|
37
|
+
const modifyEntryRenderFunction = (0, _plugin.createAsyncWaterfall)();
|
38
|
+
exports.modifyEntryRenderFunction = modifyEntryRenderFunction;
|
39
|
+
const modifyAsyncEntry = (0, _plugin.createAsyncWaterfall)();
|
40
|
+
exports.modifyAsyncEntry = modifyAsyncEntry;
|
41
|
+
const modifyFileSystemRoutes = (0, _plugin.createAsyncWaterfall)();
|
42
|
+
exports.modifyFileSystemRoutes = modifyFileSystemRoutes;
|
43
|
+
const modifyServerRoutes = (0, _plugin.createAsyncWaterfall)();
|
44
|
+
exports.modifyServerRoutes = modifyServerRoutes;
|
45
|
+
const htmlPartials = (0, _plugin.createAsyncWaterfall)();
|
46
|
+
exports.htmlPartials = htmlPartials;
|
47
|
+
const beforeGenerateRoutes = (0, _plugin.createAsyncWaterfall)();
|
48
|
+
exports.beforeGenerateRoutes = beforeGenerateRoutes;
|
49
|
+
const addDefineTypes = (0, _plugin.createAsyncWaterfall)();
|
50
|
+
exports.addDefineTypes = addDefineTypes;
|
51
|
+
|
52
|
+
var _default = () => ({
|
53
|
+
name: '@modern-js/plugin-analyze',
|
54
|
+
registerHook: {
|
55
|
+
modifyAsyncEntry,
|
56
|
+
modifyEntryImports,
|
57
|
+
modifyEntryExport,
|
58
|
+
modifyEntryRuntimePlugins,
|
59
|
+
modifyEntryRenderFunction,
|
60
|
+
modifyFileSystemRoutes,
|
61
|
+
modifyServerRoutes,
|
62
|
+
htmlPartials,
|
63
|
+
addRuntimeExports,
|
64
|
+
beforeGenerateRoutes,
|
65
|
+
addDefineTypes
|
66
|
+
},
|
67
|
+
setup: api => {
|
68
|
+
let pagesDir = [];
|
69
|
+
let originEntrypoints = [];
|
70
|
+
return {
|
71
|
+
async prepare() {
|
72
|
+
var _resolvedConfig$sourc;
|
73
|
+
|
74
|
+
const appContext = api.useAppContext();
|
75
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
76
|
+
const hookRunners = api.useHookRunners();
|
77
|
+
|
78
|
+
try {
|
79
|
+
_utils.fs.emptydirSync(appContext.internalDirectory);
|
80
|
+
} catch (_unused) {// FIXME:
|
81
|
+
}
|
82
|
+
|
83
|
+
const apiOnly = await (0, _utils.isApiOnly)(appContext.appDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$sourc = resolvedConfig.source) === null || _resolvedConfig$sourc === void 0 ? void 0 : _resolvedConfig$sourc.entriesDir);
|
84
|
+
await hookRunners.addRuntimeExports();
|
85
|
+
|
86
|
+
if (apiOnly) {
|
87
|
+
const {
|
88
|
+
routes
|
89
|
+
} = await hookRunners.modifyServerRoutes({
|
90
|
+
routes: []
|
91
|
+
});
|
92
|
+
debug(`server routes: %o`, routes);
|
93
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
94
|
+
apiOnly,
|
95
|
+
serverRoutes: routes
|
96
|
+
}));
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
|
100
|
+
const [{
|
101
|
+
getBundleEntry
|
102
|
+
}, {
|
103
|
+
getServerRoutes
|
104
|
+
}, {
|
105
|
+
generateCode
|
106
|
+
}, {
|
107
|
+
getHtmlTemplate
|
108
|
+
}] = await Promise.all([Promise.resolve().then(() => _interopRequireWildcard(require("./getBundleEntry"))), Promise.resolve().then(() => _interopRequireWildcard(require("./getServerRoutes"))), Promise.resolve().then(() => _interopRequireWildcard(require("./generateCode"))), Promise.resolve().then(() => _interopRequireWildcard(require("./getHtmlTemplate")))]);
|
109
|
+
const entrypoints = getBundleEntry(appContext, resolvedConfig);
|
110
|
+
const defaultChecked = entrypoints.map(point => point.entryName);
|
111
|
+
debug(`entrypoints: %o`, entrypoints);
|
112
|
+
const initialRoutes = getServerRoutes(entrypoints, {
|
113
|
+
appContext,
|
114
|
+
config: resolvedConfig
|
115
|
+
});
|
116
|
+
const {
|
117
|
+
routes
|
118
|
+
} = await hookRunners.modifyServerRoutes({
|
119
|
+
routes: initialRoutes
|
120
|
+
});
|
121
|
+
debug(`server routes: %o`, routes);
|
122
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
123
|
+
entrypoints,
|
124
|
+
serverRoutes: routes
|
125
|
+
}));
|
126
|
+
pagesDir = entrypoints.map(point => point.entry);
|
127
|
+
originEntrypoints = (0, _lodash.cloneDeep)(entrypoints);
|
128
|
+
await generateCode(appContext, resolvedConfig, entrypoints, api);
|
129
|
+
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
130
|
+
appContext,
|
131
|
+
config: resolvedConfig
|
132
|
+
});
|
133
|
+
debug(`html templates: %o`, htmlTemplates);
|
134
|
+
await hookRunners.addDefineTypes();
|
135
|
+
debug(`add Define Types`);
|
136
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
137
|
+
entrypoints,
|
138
|
+
checkedEntries: defaultChecked,
|
139
|
+
apiOnly,
|
140
|
+
serverRoutes: routes,
|
141
|
+
htmlTemplates
|
142
|
+
}));
|
143
|
+
},
|
144
|
+
|
145
|
+
watchFiles() {
|
146
|
+
return pagesDir;
|
147
|
+
},
|
148
|
+
|
149
|
+
async fileChange(e) {
|
150
|
+
const appContext = api.useAppContext();
|
151
|
+
const {
|
152
|
+
appDirectory
|
153
|
+
} = appContext;
|
154
|
+
const {
|
155
|
+
filename,
|
156
|
+
eventType
|
157
|
+
} = e;
|
158
|
+
|
159
|
+
const isPageFile = name => pagesDir.some(pageDir => name.includes(pageDir));
|
160
|
+
|
161
|
+
const absoluteFilePath = path.resolve(appDirectory, filename);
|
162
|
+
const isRouteComponent = isPageFile(absoluteFilePath) && (0, _utils2.isRouteComponentFile)(absoluteFilePath);
|
163
|
+
|
164
|
+
if (isRouteComponent && (eventType === 'add' || eventType === 'unlink')) {
|
165
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
166
|
+
const {
|
167
|
+
generateCode
|
168
|
+
} = await Promise.resolve().then(() => _interopRequireWildcard(require("./generateCode")));
|
169
|
+
const entrypoints = (0, _lodash.cloneDeep)(originEntrypoints);
|
170
|
+
generateCode(appContext, resolvedConfig, entrypoints, api);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
};
|
175
|
+
}
|
176
|
+
});
|
177
|
+
|
178
|
+
exports.default = _default;
|
@@ -0,0 +1,50 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.isDefaultExportFunction = void 0;
|
7
|
+
|
8
|
+
var _fs = _interopRequireDefault(require("fs"));
|
9
|
+
|
10
|
+
var _parser = require("@babel/parser");
|
11
|
+
|
12
|
+
var _traverse = _interopRequireDefault(require("@babel/traverse"));
|
13
|
+
|
14
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
15
|
+
|
16
|
+
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); }
|
17
|
+
|
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; }
|
19
|
+
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
+
|
22
|
+
const isFunction = node => t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
|
23
|
+
|
24
|
+
const isDefaultExportFunction = file => {
|
25
|
+
if (!file || !_fs.default.existsSync(file)) {
|
26
|
+
return false;
|
27
|
+
}
|
28
|
+
|
29
|
+
const ast = (0, _parser.parse)(_fs.default.readFileSync(file, 'utf8'), {
|
30
|
+
sourceType: 'unambiguous',
|
31
|
+
plugins: ['jsx', 'typescript', 'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'decorators-legacy', 'functionBind', 'classPrivateMethods', ['pipelineOperator', {
|
32
|
+
proposal: 'minimal'
|
33
|
+
}], 'optionalChaining', 'optionalCatchBinding', 'objectRestSpread', 'numericSeparator']
|
34
|
+
});
|
35
|
+
let isExportFunction = false;
|
36
|
+
(0, _traverse.default)(ast, {
|
37
|
+
ExportDefaultDeclaration: path => {
|
38
|
+
const {
|
39
|
+
declaration
|
40
|
+
} = path.node;
|
41
|
+
|
42
|
+
if (isFunction(declaration)) {
|
43
|
+
isExportFunction = true;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
});
|
47
|
+
return isExportFunction;
|
48
|
+
};
|
49
|
+
|
50
|
+
exports.isDefaultExportFunction = isDefaultExportFunction;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.makeLegalIdentifier = makeLegalIdentifier;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* modified from https://github.com/rollup/plugins/blob/master/packages/pluginutils
|
10
|
+
* license at https://github.com/rollup/plugins/blob/master/LICENSE
|
11
|
+
*/
|
12
|
+
const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
|
13
|
+
const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
|
14
|
+
const forbidList = new Set(`${reservedWords} ${builtins}`.split(' '));
|
15
|
+
|
16
|
+
function makeLegalIdentifier(str) {
|
17
|
+
const identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');
|
18
|
+
|
19
|
+
if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
|
20
|
+
return `_${identifier}`;
|
21
|
+
}
|
22
|
+
|
23
|
+
return identifier || '_';
|
24
|
+
}
|
@@ -0,0 +1,106 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.renderFunction = exports.index = exports.html = exports.fileSystemRoutes = void 0;
|
7
|
+
|
8
|
+
const index = ({
|
9
|
+
mountId,
|
10
|
+
imports,
|
11
|
+
renderFunction,
|
12
|
+
exportStatement
|
13
|
+
}) => `
|
14
|
+
const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
|
15
|
+
const IS_REACT18 = process.env.IS_REACT18 === 'true';
|
16
|
+
const MOUNT_ID = '${mountId}';
|
17
|
+
|
18
|
+
${imports}
|
19
|
+
|
20
|
+
let AppWrapper = null;
|
21
|
+
|
22
|
+
let root = null;
|
23
|
+
|
24
|
+
function render() {
|
25
|
+
${renderFunction}
|
26
|
+
}
|
27
|
+
|
28
|
+
AppWrapper = render();
|
29
|
+
|
30
|
+
${exportStatement};
|
31
|
+
`;
|
32
|
+
|
33
|
+
exports.index = index;
|
34
|
+
|
35
|
+
const renderFunction = ({
|
36
|
+
plugins,
|
37
|
+
customBootstrap,
|
38
|
+
fileSystemRoutes
|
39
|
+
}) => `
|
40
|
+
AppWrapper = createApp({
|
41
|
+
plugins: [
|
42
|
+
${plugins.map(({
|
43
|
+
name,
|
44
|
+
options,
|
45
|
+
args
|
46
|
+
}) => `${name}({...${options}, ...App?.config?.${args || name}}),`).join('\n')}
|
47
|
+
]
|
48
|
+
})(${fileSystemRoutes ? '' : `App`})
|
49
|
+
|
50
|
+
if (IS_BROWSER) {
|
51
|
+
${customBootstrap ? `customBootstrap(AppWrapper);` : `bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM);`}
|
52
|
+
}
|
53
|
+
|
54
|
+
return AppWrapper
|
55
|
+
`;
|
56
|
+
|
57
|
+
exports.renderFunction = renderFunction;
|
58
|
+
|
59
|
+
const html = partials => `
|
60
|
+
<!DOCTYPE html>
|
61
|
+
<html>
|
62
|
+
<head>
|
63
|
+
<%= meta %>
|
64
|
+
<title><%= title %></title>
|
65
|
+
|
66
|
+
${partials.top.join('\n')}
|
67
|
+
|
68
|
+
<script>
|
69
|
+
window.__assetPrefix__ = '<%= assetPrefix %>';
|
70
|
+
</script>
|
71
|
+
${partials.head.join('\n')}
|
72
|
+
|
73
|
+
<!--<?- chunksMap.css ?>-->
|
74
|
+
</head>
|
75
|
+
|
76
|
+
<body>
|
77
|
+
<noscript>
|
78
|
+
We're sorry but react app doesn't work properly without JavaScript enabled. Please enable it to continue.
|
79
|
+
</noscript>
|
80
|
+
<div id="<%= mountId %>"><!--<?- html ?>--></div>
|
81
|
+
${partials.body.join('\n')}
|
82
|
+
<!--<?- chunksMap.js ?>-->
|
83
|
+
<!--<?- SSRDataScript ?>-->
|
84
|
+
<!--<?- bottomTemplate ?>-->
|
85
|
+
</body>
|
86
|
+
|
87
|
+
</html>
|
88
|
+
`;
|
89
|
+
|
90
|
+
exports.html = html;
|
91
|
+
|
92
|
+
const fileSystemRoutes = ({
|
93
|
+
routes
|
94
|
+
}) => `
|
95
|
+
import loadable from '@modern-js/runtime/loadable';
|
96
|
+
|
97
|
+
${routes.map(({
|
98
|
+
component,
|
99
|
+
_component
|
100
|
+
}) => `const ${component} = loadable(() => import('${_component}'));`).join('\n\n')}
|
101
|
+
|
102
|
+
|
103
|
+
export const routes = ${JSON.stringify(routes, null, 2).replace(/"component"\s*:\s*"(\S+)"/g, '"component": $1')}
|
104
|
+
`;
|
105
|
+
|
106
|
+
exports.fileSystemRoutes = fileSystemRoutes;
|