@modern-js/app-tools 2.53.0 → 2.53.1-alpha.1
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 +5 -13
- package/dist/cjs/plugins/deploy/platforms/netlify.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/node.js +1 -1
- package/dist/cjs/plugins/deploy/platforms/vercel.js +1 -1
- package/dist/esm/plugins/deploy/dependencies/index.js +20 -28
- package/dist/esm/plugins/deploy/platforms/netlify.js +1 -1
- package/dist/esm/plugins/deploy/platforms/node.js +1 -1
- package/dist/esm/plugins/deploy/platforms/vercel.js +1 -1
- package/dist/esm-node/plugins/deploy/dependencies/index.js +6 -14
- package/dist/esm-node/plugins/deploy/platforms/netlify.js +1 -1
- package/dist/esm-node/plugins/deploy/platforms/node.js +1 -1
- package/dist/esm-node/plugins/deploy/platforms/vercel.js +1 -1
- 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/config/initialize/inits.d.ts +1 -1
- package/dist/types/plugins/deploy/dependencies/index.d.ts +2 -1
- package/package.json +5 -5
@@ -0,0 +1,148 @@
|
|
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
|
+
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
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
|
+
|
7
|
+
import * as path from 'path';
|
8
|
+
import { createAsyncWaterfall } from '@modern-js/plugin';
|
9
|
+
import { createDebugger, fs, isApiOnly } from '@modern-js/utils';
|
10
|
+
import { cloneDeep } from '@modern-js/utils/lodash';
|
11
|
+
import { isRouteComponentFile } from "./utils";
|
12
|
+
const debug = createDebugger('plugin-analyze');
|
13
|
+
export const modifyEntryImports = createAsyncWaterfall();
|
14
|
+
export const modifyEntryExport = createAsyncWaterfall();
|
15
|
+
export const addRuntimeExports = createAsyncWaterfall();
|
16
|
+
export const modifyEntryRuntimePlugins = createAsyncWaterfall();
|
17
|
+
export const modifyEntryRenderFunction = createAsyncWaterfall();
|
18
|
+
export const modifyAsyncEntry = createAsyncWaterfall();
|
19
|
+
export const modifyFileSystemRoutes = createAsyncWaterfall();
|
20
|
+
export const modifyServerRoutes = createAsyncWaterfall();
|
21
|
+
export const htmlPartials = createAsyncWaterfall();
|
22
|
+
export const beforeGenerateRoutes = createAsyncWaterfall();
|
23
|
+
export const addDefineTypes = createAsyncWaterfall();
|
24
|
+
export default (() => ({
|
25
|
+
name: '@modern-js/plugin-analyze',
|
26
|
+
registerHook: {
|
27
|
+
modifyAsyncEntry,
|
28
|
+
modifyEntryImports,
|
29
|
+
modifyEntryExport,
|
30
|
+
modifyEntryRuntimePlugins,
|
31
|
+
modifyEntryRenderFunction,
|
32
|
+
modifyFileSystemRoutes,
|
33
|
+
modifyServerRoutes,
|
34
|
+
htmlPartials,
|
35
|
+
addRuntimeExports,
|
36
|
+
beforeGenerateRoutes,
|
37
|
+
addDefineTypes
|
38
|
+
},
|
39
|
+
setup: api => {
|
40
|
+
let pagesDir = [];
|
41
|
+
let originEntrypoints = [];
|
42
|
+
return {
|
43
|
+
async prepare() {
|
44
|
+
var _resolvedConfig$sourc;
|
45
|
+
|
46
|
+
const appContext = api.useAppContext();
|
47
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
48
|
+
const hookRunners = api.useHookRunners();
|
49
|
+
|
50
|
+
try {
|
51
|
+
fs.emptydirSync(appContext.internalDirectory);
|
52
|
+
} catch (_unused) {// FIXME:
|
53
|
+
}
|
54
|
+
|
55
|
+
const apiOnly = await isApiOnly(appContext.appDirectory, resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$sourc = resolvedConfig.source) === null || _resolvedConfig$sourc === void 0 ? void 0 : _resolvedConfig$sourc.entriesDir);
|
56
|
+
await hookRunners.addRuntimeExports();
|
57
|
+
|
58
|
+
if (apiOnly) {
|
59
|
+
const {
|
60
|
+
routes
|
61
|
+
} = await hookRunners.modifyServerRoutes({
|
62
|
+
routes: []
|
63
|
+
});
|
64
|
+
debug(`server routes: %o`, routes);
|
65
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
66
|
+
apiOnly,
|
67
|
+
serverRoutes: routes
|
68
|
+
}));
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
|
72
|
+
const [{
|
73
|
+
getBundleEntry
|
74
|
+
}, {
|
75
|
+
getServerRoutes
|
76
|
+
}, {
|
77
|
+
generateCode
|
78
|
+
}, {
|
79
|
+
getHtmlTemplate
|
80
|
+
}] = await Promise.all([import("./getBundleEntry"), import("./getServerRoutes"), import("./generateCode"), import("./getHtmlTemplate")]);
|
81
|
+
const entrypoints = getBundleEntry(appContext, resolvedConfig);
|
82
|
+
const defaultChecked = entrypoints.map(point => point.entryName);
|
83
|
+
debug(`entrypoints: %o`, entrypoints);
|
84
|
+
const initialRoutes = getServerRoutes(entrypoints, {
|
85
|
+
appContext,
|
86
|
+
config: resolvedConfig
|
87
|
+
});
|
88
|
+
const {
|
89
|
+
routes
|
90
|
+
} = await hookRunners.modifyServerRoutes({
|
91
|
+
routes: initialRoutes
|
92
|
+
});
|
93
|
+
debug(`server routes: %o`, routes);
|
94
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
95
|
+
entrypoints,
|
96
|
+
serverRoutes: routes
|
97
|
+
}));
|
98
|
+
pagesDir = entrypoints.map(point => point.entry);
|
99
|
+
originEntrypoints = cloneDeep(entrypoints);
|
100
|
+
await generateCode(appContext, resolvedConfig, entrypoints, api);
|
101
|
+
const htmlTemplates = await getHtmlTemplate(entrypoints, api, {
|
102
|
+
appContext,
|
103
|
+
config: resolvedConfig
|
104
|
+
});
|
105
|
+
debug(`html templates: %o`, htmlTemplates);
|
106
|
+
await hookRunners.addDefineTypes();
|
107
|
+
debug(`add Define Types`);
|
108
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
109
|
+
entrypoints,
|
110
|
+
checkedEntries: defaultChecked,
|
111
|
+
apiOnly,
|
112
|
+
serverRoutes: routes,
|
113
|
+
htmlTemplates
|
114
|
+
}));
|
115
|
+
},
|
116
|
+
|
117
|
+
watchFiles() {
|
118
|
+
return pagesDir;
|
119
|
+
},
|
120
|
+
|
121
|
+
async fileChange(e) {
|
122
|
+
const appContext = api.useAppContext();
|
123
|
+
const {
|
124
|
+
appDirectory
|
125
|
+
} = appContext;
|
126
|
+
const {
|
127
|
+
filename,
|
128
|
+
eventType
|
129
|
+
} = e;
|
130
|
+
|
131
|
+
const isPageFile = name => pagesDir.some(pageDir => name.includes(pageDir));
|
132
|
+
|
133
|
+
const absoluteFilePath = path.resolve(appDirectory, filename);
|
134
|
+
const isRouteComponent = isPageFile(absoluteFilePath) && isRouteComponentFile(absoluteFilePath);
|
135
|
+
|
136
|
+
if (isRouteComponent && (eventType === 'add' || eventType === 'unlink')) {
|
137
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
138
|
+
const {
|
139
|
+
generateCode
|
140
|
+
} = await import("./generateCode");
|
141
|
+
const entrypoints = cloneDeep(originEntrypoints);
|
142
|
+
generateCode(appContext, resolvedConfig, entrypoints, api);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
};
|
147
|
+
}
|
148
|
+
}));
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import { parse } from '@babel/parser';
|
3
|
+
import traverse from '@babel/traverse';
|
4
|
+
import * as t from '@babel/types';
|
5
|
+
|
6
|
+
const isFunction = node => t.isFunctionDeclaration(node) || t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
|
7
|
+
|
8
|
+
export const isDefaultExportFunction = file => {
|
9
|
+
if (!file || !fs.existsSync(file)) {
|
10
|
+
return false;
|
11
|
+
}
|
12
|
+
|
13
|
+
const ast = parse(fs.readFileSync(file, 'utf8'), {
|
14
|
+
sourceType: 'unambiguous',
|
15
|
+
plugins: ['jsx', 'typescript', 'classProperties', 'dynamicImport', 'exportDefaultFrom', 'exportNamespaceFrom', 'decorators-legacy', 'functionBind', 'classPrivateMethods', ['pipelineOperator', {
|
16
|
+
proposal: 'minimal'
|
17
|
+
}], 'optionalChaining', 'optionalCatchBinding', 'objectRestSpread', 'numericSeparator']
|
18
|
+
});
|
19
|
+
let isExportFunction = false;
|
20
|
+
traverse(ast, {
|
21
|
+
ExportDefaultDeclaration: path => {
|
22
|
+
const {
|
23
|
+
declaration
|
24
|
+
} = path.node;
|
25
|
+
|
26
|
+
if (isFunction(declaration)) {
|
27
|
+
isExportFunction = true;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
});
|
31
|
+
return isExportFunction;
|
32
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/**
|
2
|
+
* modified from https://github.com/rollup/plugins/blob/master/packages/pluginutils
|
3
|
+
* license at https://github.com/rollup/plugins/blob/master/LICENSE
|
4
|
+
*/
|
5
|
+
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';
|
6
|
+
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';
|
7
|
+
const forbidList = new Set(`${reservedWords} ${builtins}`.split(' '));
|
8
|
+
export function makeLegalIdentifier(str) {
|
9
|
+
const identifier = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');
|
10
|
+
|
11
|
+
if (/\d/.test(identifier[0]) || forbidList.has(identifier)) {
|
12
|
+
return `_${identifier}`;
|
13
|
+
}
|
14
|
+
|
15
|
+
return identifier || '_';
|
16
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
export const index = ({
|
2
|
+
mountId,
|
3
|
+
imports,
|
4
|
+
renderFunction,
|
5
|
+
exportStatement
|
6
|
+
}) => `
|
7
|
+
const IS_BROWSER = typeof window !== 'undefined' && window.name !== 'nodejs';
|
8
|
+
const IS_REACT18 = process.env.IS_REACT18 === 'true';
|
9
|
+
const MOUNT_ID = '${mountId}';
|
10
|
+
|
11
|
+
${imports}
|
12
|
+
|
13
|
+
let AppWrapper = null;
|
14
|
+
|
15
|
+
let root = null;
|
16
|
+
|
17
|
+
function render() {
|
18
|
+
${renderFunction}
|
19
|
+
}
|
20
|
+
|
21
|
+
AppWrapper = render();
|
22
|
+
|
23
|
+
${exportStatement};
|
24
|
+
`;
|
25
|
+
export const renderFunction = ({
|
26
|
+
plugins,
|
27
|
+
customBootstrap,
|
28
|
+
fileSystemRoutes
|
29
|
+
}) => `
|
30
|
+
AppWrapper = createApp({
|
31
|
+
plugins: [
|
32
|
+
${plugins.map(({
|
33
|
+
name,
|
34
|
+
options,
|
35
|
+
args
|
36
|
+
}) => `${name}({...${options}, ...App?.config?.${args || name}}),`).join('\n')}
|
37
|
+
]
|
38
|
+
})(${fileSystemRoutes ? '' : `App`})
|
39
|
+
|
40
|
+
if (IS_BROWSER) {
|
41
|
+
${customBootstrap ? `customBootstrap(AppWrapper);` : `bootstrap(AppWrapper, MOUNT_ID, root, ReactDOM);`}
|
42
|
+
}
|
43
|
+
|
44
|
+
return AppWrapper
|
45
|
+
`;
|
46
|
+
export const html = partials => `
|
47
|
+
<!DOCTYPE html>
|
48
|
+
<html>
|
49
|
+
<head>
|
50
|
+
<%= meta %>
|
51
|
+
<title><%= title %></title>
|
52
|
+
|
53
|
+
${partials.top.join('\n')}
|
54
|
+
|
55
|
+
<script>
|
56
|
+
window.__assetPrefix__ = '<%= assetPrefix %>';
|
57
|
+
</script>
|
58
|
+
${partials.head.join('\n')}
|
59
|
+
|
60
|
+
<!--<?- chunksMap.css ?>-->
|
61
|
+
</head>
|
62
|
+
|
63
|
+
<body>
|
64
|
+
<noscript>
|
65
|
+
We're sorry but react app doesn't work properly without JavaScript enabled. Please enable it to continue.
|
66
|
+
</noscript>
|
67
|
+
<div id="<%= mountId %>"><!--<?- html ?>--></div>
|
68
|
+
${partials.body.join('\n')}
|
69
|
+
<!--<?- chunksMap.js ?>-->
|
70
|
+
<!--<?- SSRDataScript ?>-->
|
71
|
+
<!--<?- bottomTemplate ?>-->
|
72
|
+
</body>
|
73
|
+
|
74
|
+
</html>
|
75
|
+
`;
|
76
|
+
export const fileSystemRoutes = ({
|
77
|
+
routes
|
78
|
+
}) => `
|
79
|
+
import loadable from '@modern-js/runtime/loadable';
|
80
|
+
|
81
|
+
${routes.map(({
|
82
|
+
component,
|
83
|
+
_component
|
84
|
+
}) => `const ${component} = loadable(() => import('${_component}'));`).join('\n\n')}
|
85
|
+
|
86
|
+
|
87
|
+
export const routes = ${JSON.stringify(routes, null, 2).replace(/"component"\s*:\s*"(\S+)"/g, '"component": $1')}
|
88
|
+
`;
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
import { isReact18, normalizeToPosixPath } from '@modern-js/utils';
|
4
|
+
import { FILE_SYSTEM_ROUTES_FILE_NAME } from "./constants";
|
5
|
+
export const walkDirectory = dir => fs.readdirSync(dir).reduce((previous, filename) => {
|
6
|
+
const filePath = path.join(dir, filename);
|
7
|
+
|
8
|
+
if (fs.statSync(filePath).isDirectory()) {
|
9
|
+
return [...previous, ...walkDirectory(filePath)];
|
10
|
+
} else {
|
11
|
+
return [...previous, filePath];
|
12
|
+
}
|
13
|
+
}, []);
|
14
|
+
export const getDefaultImports = ({
|
15
|
+
entrypoint,
|
16
|
+
srcDirectory,
|
17
|
+
internalSrcAlias,
|
18
|
+
internalDirAlias,
|
19
|
+
internalDirectory
|
20
|
+
}) => {
|
21
|
+
const {
|
22
|
+
entryName,
|
23
|
+
fileSystemRoutes,
|
24
|
+
customBootstrap,
|
25
|
+
entry
|
26
|
+
} = entrypoint;
|
27
|
+
const imports = [{
|
28
|
+
specifiers: [{
|
29
|
+
local: 'React'
|
30
|
+
}],
|
31
|
+
value: 'react'
|
32
|
+
}, {
|
33
|
+
specifiers: [{
|
34
|
+
local: 'ReactDOM'
|
35
|
+
}],
|
36
|
+
value: isReact18(path.join(internalDirectory, '../../')) ? 'react-dom/client' : 'react-dom'
|
37
|
+
}, {
|
38
|
+
specifiers: [{
|
39
|
+
imported: 'createApp'
|
40
|
+
}, {
|
41
|
+
imported: 'bootstrap'
|
42
|
+
}],
|
43
|
+
value: '@modern-js/runtime'
|
44
|
+
}, customBootstrap && {
|
45
|
+
specifiers: [{
|
46
|
+
local: 'customBootstrap'
|
47
|
+
}],
|
48
|
+
value: normalizeToPosixPath(customBootstrap.replace(srcDirectory, internalSrcAlias))
|
49
|
+
}].filter(Boolean);
|
50
|
+
|
51
|
+
if (fileSystemRoutes) {
|
52
|
+
const route = {
|
53
|
+
specifiers: [{
|
54
|
+
imported: 'routes'
|
55
|
+
}],
|
56
|
+
value: normalizeToPosixPath(`${internalDirAlias}/${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`)
|
57
|
+
};
|
58
|
+
|
59
|
+
if (fileSystemRoutes.globalApp) {
|
60
|
+
imports.push({
|
61
|
+
specifiers: [{
|
62
|
+
local: 'App'
|
63
|
+
}],
|
64
|
+
value: normalizeToPosixPath(fileSystemRoutes.globalApp.replace(srcDirectory, internalSrcAlias))
|
65
|
+
});
|
66
|
+
} else {
|
67
|
+
route.initialize = 'const App = false;';
|
68
|
+
}
|
69
|
+
|
70
|
+
imports.push(route);
|
71
|
+
} else {
|
72
|
+
imports.push({
|
73
|
+
specifiers: [{
|
74
|
+
local: 'App'
|
75
|
+
}],
|
76
|
+
value: normalizeToPosixPath(entry.replace(srcDirectory, internalSrcAlias))
|
77
|
+
});
|
78
|
+
}
|
79
|
+
|
80
|
+
return imports;
|
81
|
+
};
|
82
|
+
export const isRouteComponentFile = filePath => {
|
83
|
+
if (/\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/.test(filePath)) {
|
84
|
+
return false;
|
85
|
+
}
|
86
|
+
|
87
|
+
if (['.js', '.jsx', '.ts', '.tsx'].includes(path.extname(filePath))) {
|
88
|
+
return true;
|
89
|
+
}
|
90
|
+
|
91
|
+
return false;
|
92
|
+
};
|
@@ -0,0 +1,154 @@
|
|
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
|
+
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
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
|
+
|
7
|
+
import { webpack, getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
|
8
|
+
import { ResolvedConfigContext } from '@modern-js/core';
|
9
|
+
import { formatWebpackMessages, measureFileSizesBeforeBuild, printFileSizesAfterBuild, printBuildError, logger, isUseSSRBundle, emptyDir } from '@modern-js/utils';
|
10
|
+
import { generateRoutes } from "../utils/routes";
|
11
|
+
import { buildServerConfig, emitResolvedConfig } from "../utils/config";
|
12
|
+
// These sizes are pretty large. We'll warn for bundles exceeding them.
|
13
|
+
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
14
|
+
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
15
|
+
export const build = async (api, options) => {
|
16
|
+
let resolvedConfig = api.useResolvedConfigContext();
|
17
|
+
const appContext = api.useAppContext();
|
18
|
+
const hookRunners = api.useHookRunners();
|
19
|
+
const {
|
20
|
+
apiOnly
|
21
|
+
} = appContext;
|
22
|
+
|
23
|
+
if (apiOnly) {
|
24
|
+
const {
|
25
|
+
appDirectory,
|
26
|
+
distDirectory,
|
27
|
+
serverConfigFile
|
28
|
+
} = appContext;
|
29
|
+
await emptyDir(distDirectory);
|
30
|
+
await hookRunners.beforeBuild({
|
31
|
+
webpackConfigs: []
|
32
|
+
});
|
33
|
+
await buildServerConfig({
|
34
|
+
appDirectory,
|
35
|
+
distDirectory,
|
36
|
+
configFile: serverConfigFile
|
37
|
+
});
|
38
|
+
await generateRoutes(appContext);
|
39
|
+
await hookRunners.afterBuild();
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
const webpackBuild = async (webpackConfig, type) => {
|
44
|
+
const compiler = webpack(webpackConfig);
|
45
|
+
return new Promise((resolve, reject) => {
|
46
|
+
let label = process.env.NODE_ENV || '';
|
47
|
+
|
48
|
+
if (type && type !== 'legacy') {
|
49
|
+
label += ` ${type}`;
|
50
|
+
}
|
51
|
+
|
52
|
+
logger.info(`Creating a ${label} build...`);
|
53
|
+
compiler.run((err, stats) => {
|
54
|
+
let messages;
|
55
|
+
|
56
|
+
if (!err) {
|
57
|
+
messages = formatWebpackMessages(stats.toJson({
|
58
|
+
all: false,
|
59
|
+
warnings: true,
|
60
|
+
errors: true
|
61
|
+
}));
|
62
|
+
|
63
|
+
if (messages.errors.length === 0) {
|
64
|
+
logger.info(`File sizes after ${label} build:\n`);
|
65
|
+
printFileSizesAfterBuild(stats, previousFileSizes, distDirectory, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
|
66
|
+
logger.log();
|
67
|
+
}
|
68
|
+
} // When using run or watch, call close and wait for it to finish before calling run or watch again.
|
69
|
+
// Concurrent compilations will corrupt the output files.
|
70
|
+
|
71
|
+
|
72
|
+
compiler.close(closeErr => {
|
73
|
+
if (closeErr) {
|
74
|
+
logger.error(closeErr);
|
75
|
+
}
|
76
|
+
|
77
|
+
if (err) {
|
78
|
+
reject(err);
|
79
|
+
} else {
|
80
|
+
if (messages.errors.length) {
|
81
|
+
reject(new Error(messages.errors.join('\n\n')));
|
82
|
+
return;
|
83
|
+
}
|
84
|
+
|
85
|
+
resolve({
|
86
|
+
warnings: messages.warnings
|
87
|
+
});
|
88
|
+
}
|
89
|
+
});
|
90
|
+
});
|
91
|
+
});
|
92
|
+
};
|
93
|
+
|
94
|
+
resolvedConfig = _objectSpread(_objectSpread({}, resolvedConfig), {}, {
|
95
|
+
cliOptions: options
|
96
|
+
});
|
97
|
+
ResolvedConfigContext.set(resolvedConfig);
|
98
|
+
const {
|
99
|
+
distDirectory,
|
100
|
+
appDirectory,
|
101
|
+
serverConfigFile
|
102
|
+
} = appContext;
|
103
|
+
const previousFileSizes = await measureFileSizesBeforeBuild(distDirectory);
|
104
|
+
await emptyDir(distDirectory);
|
105
|
+
await buildServerConfig({
|
106
|
+
appDirectory,
|
107
|
+
distDirectory,
|
108
|
+
configFile: serverConfigFile
|
109
|
+
});
|
110
|
+
const buildConfigs = [];
|
111
|
+
buildConfigs.push({
|
112
|
+
type: 'legacy',
|
113
|
+
config: getWebpackConfig(WebpackConfigTarget.CLIENT, appContext, resolvedConfig)
|
114
|
+
});
|
115
|
+
|
116
|
+
if (resolvedConfig.output.enableModernMode) {
|
117
|
+
buildConfigs.push({
|
118
|
+
type: 'modern',
|
119
|
+
config: getWebpackConfig(WebpackConfigTarget.MODERN, appContext, resolvedConfig)
|
120
|
+
});
|
121
|
+
}
|
122
|
+
|
123
|
+
if (isUseSSRBundle(resolvedConfig)) {
|
124
|
+
buildConfigs.push({
|
125
|
+
type: 'ssr',
|
126
|
+
config: getWebpackConfig(WebpackConfigTarget.NODE, appContext, resolvedConfig)
|
127
|
+
});
|
128
|
+
}
|
129
|
+
|
130
|
+
await hookRunners.beforeBuild({
|
131
|
+
webpackConfigs: buildConfigs.map(({
|
132
|
+
config
|
133
|
+
}) => config)
|
134
|
+
});
|
135
|
+
|
136
|
+
for (const buildConfig of buildConfigs) {
|
137
|
+
const {
|
138
|
+
type: buildType,
|
139
|
+
config
|
140
|
+
} = buildConfig;
|
141
|
+
|
142
|
+
try {
|
143
|
+
await webpackBuild(config, buildType);
|
144
|
+
} catch (error) {
|
145
|
+
printBuildError(error); // eslint-disable-next-line no-process-exit
|
146
|
+
|
147
|
+
process.exit(1);
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
await generateRoutes(appContext);
|
152
|
+
await hookRunners.afterBuild();
|
153
|
+
await emitResolvedConfig(appDirectory, resolvedConfig);
|
154
|
+
};
|
@@ -0,0 +1,95 @@
|
|
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
|
+
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
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
|
+
|
7
|
+
import { fs, logger, chalk, isSSR } from '@modern-js/utils';
|
8
|
+
import { ResolvedConfigContext } from '@modern-js/core';
|
9
|
+
import { createCompiler } from "../utils/createCompiler";
|
10
|
+
import { createServer } from "../utils/createServer";
|
11
|
+
import { generateRoutes } from "../utils/routes";
|
12
|
+
import { printInstructions } from "../utils/printInstructions";
|
13
|
+
import { getSpecifiedEntries } from "../utils/getSpecifiedEntries";
|
14
|
+
import { buildServerConfig } from "../utils/config";
|
15
|
+
export const dev = async (api, options) => {
|
16
|
+
let userConfig = api.useResolvedConfigContext();
|
17
|
+
const appContext = api.useAppContext();
|
18
|
+
const hookRunners = api.useHookRunners();
|
19
|
+
userConfig = _objectSpread(_objectSpread({}, userConfig), {}, {
|
20
|
+
cliOptions: options
|
21
|
+
});
|
22
|
+
ResolvedConfigContext.set(userConfig);
|
23
|
+
const {
|
24
|
+
appDirectory,
|
25
|
+
distDirectory,
|
26
|
+
port,
|
27
|
+
apiOnly,
|
28
|
+
entrypoints,
|
29
|
+
serverConfigFile
|
30
|
+
} = appContext;
|
31
|
+
const checkedEntries = await getSpecifiedEntries(options.entry || false, entrypoints);
|
32
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
33
|
+
checkedEntries
|
34
|
+
}));
|
35
|
+
appContext.checkedEntries = checkedEntries;
|
36
|
+
fs.emptyDirSync(distDirectory);
|
37
|
+
await buildServerConfig({
|
38
|
+
appDirectory,
|
39
|
+
distDirectory,
|
40
|
+
configFile: serverConfigFile,
|
41
|
+
options: {
|
42
|
+
esbuildOptions: {
|
43
|
+
watch: true
|
44
|
+
}
|
45
|
+
}
|
46
|
+
});
|
47
|
+
await hookRunners.beforeDev();
|
48
|
+
let compiler = null;
|
49
|
+
|
50
|
+
if (!apiOnly) {
|
51
|
+
const {
|
52
|
+
getWebpackConfig,
|
53
|
+
WebpackConfigTarget
|
54
|
+
} = await import('@modern-js/webpack');
|
55
|
+
const webpackConfigs = [isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE, appContext, userConfig), getWebpackConfig(WebpackConfigTarget.CLIENT, appContext, userConfig)].filter(Boolean);
|
56
|
+
compiler = await createCompiler({
|
57
|
+
api,
|
58
|
+
webpackConfigs,
|
59
|
+
userConfig,
|
60
|
+
appContext
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
await generateRoutes(appContext);
|
65
|
+
const app = await createServer({
|
66
|
+
dev: _objectSpread(_objectSpread({}, {
|
67
|
+
client: {
|
68
|
+
port: port.toString()
|
69
|
+
},
|
70
|
+
devMiddleware: {
|
71
|
+
writeToDisk: file => !file.includes('.hot-update.')
|
72
|
+
},
|
73
|
+
hot: true,
|
74
|
+
liveReload: true,
|
75
|
+
port,
|
76
|
+
https: userConfig.dev.https
|
77
|
+
}), userConfig.tools.devServer),
|
78
|
+
compiler,
|
79
|
+
pwd: appDirectory,
|
80
|
+
config: userConfig,
|
81
|
+
serverConfigFile,
|
82
|
+
plugins: appContext.plugins.filter(p => p.server).map(p => p.server)
|
83
|
+
});
|
84
|
+
app.listen(port, async err => {
|
85
|
+
if (err) {
|
86
|
+
throw err;
|
87
|
+
}
|
88
|
+
|
89
|
+
if (apiOnly) {
|
90
|
+
return printInstructions(hookRunners, appContext, userConfig);
|
91
|
+
}
|
92
|
+
|
93
|
+
return logger.log(chalk.cyan(`Starting the development server...`));
|
94
|
+
});
|
95
|
+
};
|