@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,69 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
|
3
|
+
import { fs, logger, isUseSSRBundle, chalk } from '@modern-js/utils';
|
4
|
+
import WebpackChain from '@modern-js/utils/webpack-chain';
|
5
|
+
export const formatWebpackConfig = (config, verbose) => {
|
6
|
+
const stringify = WebpackChain.toString;
|
7
|
+
return `module.exports = ${stringify(config, {
|
8
|
+
verbose
|
9
|
+
})};`;
|
10
|
+
};
|
11
|
+
export const inspect = (api, options) => {
|
12
|
+
process.env.NODE_ENV = options.env;
|
13
|
+
const resolvedConfig = api.useResolvedConfigContext();
|
14
|
+
const appContext = api.useAppContext();
|
15
|
+
const outputFiles = [];
|
16
|
+
outputFiles.push(printInspectResult(WebpackConfigTarget.CLIENT, appContext, resolvedConfig, options));
|
17
|
+
|
18
|
+
if (resolvedConfig.output.enableModernMode) {
|
19
|
+
outputFiles.push(printInspectResult(WebpackConfigTarget.MODERN, appContext, resolvedConfig, options));
|
20
|
+
}
|
21
|
+
|
22
|
+
if (isUseSSRBundle(resolvedConfig)) {
|
23
|
+
outputFiles.push(printInspectResult(WebpackConfigTarget.NODE, appContext, resolvedConfig, options));
|
24
|
+
}
|
25
|
+
|
26
|
+
logger.success('Inspect succeed, you can open following files to view the full webpack config: \n');
|
27
|
+
outputFiles.forEach(file => {
|
28
|
+
logger.log(` - ${chalk.yellow(path.relative(appContext.appDirectory, file))}`);
|
29
|
+
});
|
30
|
+
logger.log();
|
31
|
+
};
|
32
|
+
export const getTagByWebpackTarget = webpackTarget => {
|
33
|
+
switch (webpackTarget) {
|
34
|
+
case WebpackConfigTarget.CLIENT:
|
35
|
+
return 'client';
|
36
|
+
|
37
|
+
case WebpackConfigTarget.MODERN:
|
38
|
+
return 'modern';
|
39
|
+
|
40
|
+
case WebpackConfigTarget.NODE:
|
41
|
+
return 'ssr';
|
42
|
+
|
43
|
+
default:
|
44
|
+
throw Error(`Unsupported webpack target: ${webpackTarget}`);
|
45
|
+
}
|
46
|
+
};
|
47
|
+
export const printInspectResult = (webpackTarget, appContext, resolvedConfig, options) => {
|
48
|
+
const webpackConfig = getWebpackConfig(webpackTarget, appContext, resolvedConfig);
|
49
|
+
const {
|
50
|
+
output,
|
51
|
+
verbose,
|
52
|
+
console = true
|
53
|
+
} = options;
|
54
|
+
const outputPath = output ? path.posix.join(appContext.distDirectory, output) : appContext.distDirectory;
|
55
|
+
const tag = getTagByWebpackTarget(webpackTarget);
|
56
|
+
const outputFile = `webpack.${tag}.inspect.js`;
|
57
|
+
const outputFilePath = path.posix.join(outputPath, outputFile);
|
58
|
+
const rawWebpackConfig = formatWebpackConfig(webpackConfig, verbose);
|
59
|
+
fs.outputFileSync(outputFilePath, rawWebpackConfig);
|
60
|
+
|
61
|
+
if (console) {
|
62
|
+
logger.log(`
|
63
|
+
webpack config for ${tag} build:
|
64
|
+
${rawWebpackConfig}
|
65
|
+
`);
|
66
|
+
}
|
67
|
+
|
68
|
+
return outputFilePath;
|
69
|
+
};
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { logger, chalk, isApiOnly } from '@modern-js/utils';
|
2
|
+
import server from '@modern-js/prod-server';
|
3
|
+
import { printInstructions } from "../utils/printInstructions";
|
4
|
+
export const start = async api => {
|
5
|
+
var _userConfig$source;
|
6
|
+
|
7
|
+
const appContext = api.useAppContext();
|
8
|
+
const userConfig = api.useResolvedConfigContext();
|
9
|
+
const hookRunners = api.useHookRunners();
|
10
|
+
const {
|
11
|
+
appDirectory,
|
12
|
+
port,
|
13
|
+
serverConfigFile
|
14
|
+
} = appContext;
|
15
|
+
logger.log(chalk.cyan(`Starting the modern server...`));
|
16
|
+
const apiOnly = await isApiOnly(appContext.appDirectory, userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.entriesDir);
|
17
|
+
const app = await server({
|
18
|
+
pwd: appDirectory,
|
19
|
+
config: userConfig,
|
20
|
+
plugins: appContext.plugins.filter(p => p.server).map(p => p.server),
|
21
|
+
serverConfigFile,
|
22
|
+
apiOnly
|
23
|
+
});
|
24
|
+
app.listen(port, async err => {
|
25
|
+
if (err) {
|
26
|
+
throw err;
|
27
|
+
}
|
28
|
+
|
29
|
+
await printInstructions(hookRunners, appContext, userConfig);
|
30
|
+
});
|
31
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export { defineServerConfig as defineConfig } from "../utils/config";
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { createAsyncWaterfall, createAsyncWorkflow } from '@modern-js/plugin';
|
2
|
+
export const beforeDev = createAsyncWorkflow();
|
3
|
+
export const afterDev = createAsyncWorkflow();
|
4
|
+
export const beforeCreateCompiler = createAsyncWorkflow();
|
5
|
+
export const afterCreateCompiler = createAsyncWorkflow();
|
6
|
+
export const beforePrintInstructions = createAsyncWaterfall();
|
7
|
+
export const beforeBuild = createAsyncWorkflow();
|
8
|
+
export const afterBuild = createAsyncWorkflow();
|
9
|
+
export const beforeDeploy = createAsyncWorkflow();
|
10
|
+
export const afterDeploy = createAsyncWorkflow();
|
11
|
+
export const hooks = {
|
12
|
+
beforeDev,
|
13
|
+
afterDev,
|
14
|
+
beforeCreateCompiler,
|
15
|
+
afterCreateCompiler,
|
16
|
+
beforePrintInstructions,
|
17
|
+
beforeBuild,
|
18
|
+
afterBuild,
|
19
|
+
beforeDeploy,
|
20
|
+
afterDeploy
|
21
|
+
};
|
@@ -0,0 +1,109 @@
|
|
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 path from 'path';
|
8
|
+
import { defineConfig, cli } from '@modern-js/core';
|
9
|
+
import LintPlugin from '@modern-js/plugin-jarvis';
|
10
|
+
import { cleanRequireCache, Import } from '@modern-js/utils';
|
11
|
+
import AnalyzePlugin from "./analyze";
|
12
|
+
import { hooks } from "./hooks";
|
13
|
+
import { i18n, localeKeys } from "./locale";
|
14
|
+
import { getLocaleLanguage } from "./utils/language";
|
15
|
+
export { defineConfig };
|
16
|
+
const upgradeModel = Import.lazy('@modern-js/upgrade', require);
|
17
|
+
export default (() => ({
|
18
|
+
name: '@modern-js/app-tools',
|
19
|
+
post: ['@modern-js/plugin-analyze', '@modern-js/plugin-ssr', '@modern-js/plugin-state', '@modern-js/plugin-router', '@modern-js/plugin-polyfill'],
|
20
|
+
registerHook: hooks,
|
21
|
+
usePlugins: [AnalyzePlugin(), LintPlugin()],
|
22
|
+
setup: api => {
|
23
|
+
const locale = getLocaleLanguage();
|
24
|
+
i18n.changeLanguage({
|
25
|
+
locale
|
26
|
+
});
|
27
|
+
return {
|
28
|
+
commands({
|
29
|
+
program
|
30
|
+
}) {
|
31
|
+
program.command('dev').usage('[options]').description(i18n.t(localeKeys.command.dev.describe)).option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).option('-e --entry [entry...]', i18n.t(localeKeys.command.dev.entry)).option('--analyze', i18n.t(localeKeys.command.shared.analyze)).option('--api-only', i18n.t(localeKeys.command.dev.apiOnly)).action(async options => {
|
32
|
+
const {
|
33
|
+
dev
|
34
|
+
} = await import("./commands/dev");
|
35
|
+
await dev(api, options);
|
36
|
+
});
|
37
|
+
program.command('build').usage('[options]').description(i18n.t(localeKeys.command.build.describe)).option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).option('--analyze', i18n.t(localeKeys.command.shared.analyze)).action(async options => {
|
38
|
+
const {
|
39
|
+
build
|
40
|
+
} = await import("./commands/build");
|
41
|
+
await build(api, options); // force exit after build.
|
42
|
+
// eslint-disable-next-line no-process-exit
|
43
|
+
|
44
|
+
process.exit(0);
|
45
|
+
});
|
46
|
+
program.command('start').usage('[options]').description(i18n.t(localeKeys.command.start.describe)).option('--api-only', i18n.t(localeKeys.command.dev.apiOnly)).option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).action(async () => {
|
47
|
+
const {
|
48
|
+
start
|
49
|
+
} = await import("./commands/start");
|
50
|
+
await start(api);
|
51
|
+
});
|
52
|
+
program.command('deploy').usage('[options]').option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).description(i18n.t(localeKeys.command.deploy.describe)).action(async options => {
|
53
|
+
const {
|
54
|
+
build
|
55
|
+
} = await import("./commands/build");
|
56
|
+
await build(api);
|
57
|
+
const {
|
58
|
+
deploy
|
59
|
+
} = await import("./commands/deploy");
|
60
|
+
await deploy(api, options); // eslint-disable-next-line no-process-exit
|
61
|
+
|
62
|
+
process.exit(0);
|
63
|
+
});
|
64
|
+
program.command('new').usage('[options]').description(i18n.t(localeKeys.command.new.describe)).option('-d, --debug', i18n.t(localeKeys.command.new.debug), false).option('-c, --config <config>', i18n.t(localeKeys.command.new.config)).option('--dist-tag <tag>', i18n.t(localeKeys.command.new.distTag)).option('--registry', i18n.t(localeKeys.command.new.registry)).action(async options => {
|
65
|
+
const {
|
66
|
+
MWANewAction
|
67
|
+
} = await import('@modern-js/new-action');
|
68
|
+
await MWANewAction(_objectSpread(_objectSpread({}, options), {}, {
|
69
|
+
locale
|
70
|
+
}));
|
71
|
+
});
|
72
|
+
program.command('inspect').description('inspect internal webpack config').option(`--env <env>`, i18n.t(localeKeys.command.inspect.env), 'development').option('--output <output>', i18n.t(localeKeys.command.inspect.output), '/').option('--no-console', i18n.t(localeKeys.command.inspect.noConsole)).option('--verbose', i18n.t(localeKeys.command.inspect.verbose)).option('-c --config <config>', i18n.t(localeKeys.command.shared.config)).action(async options => {
|
73
|
+
const {
|
74
|
+
inspect
|
75
|
+
} = await import("./commands/inspect");
|
76
|
+
inspect(api, options);
|
77
|
+
});
|
78
|
+
upgradeModel.defineCommand(program.command('upgrade'));
|
79
|
+
},
|
80
|
+
|
81
|
+
// 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
|
82
|
+
async fileChange(e) {
|
83
|
+
const {
|
84
|
+
filename,
|
85
|
+
eventType
|
86
|
+
} = e;
|
87
|
+
const appContext = api.useAppContext();
|
88
|
+
const {
|
89
|
+
appDirectory,
|
90
|
+
srcDirectory
|
91
|
+
} = appContext;
|
92
|
+
const absolutePath = path.resolve(appDirectory, filename);
|
93
|
+
|
94
|
+
if (!absolutePath.includes(srcDirectory) && (eventType === 'change' || eventType === 'unlink')) {
|
95
|
+
const {
|
96
|
+
closeServer
|
97
|
+
} = await import("./utils/createServer");
|
98
|
+
await closeServer();
|
99
|
+
await cli.restart();
|
100
|
+
}
|
101
|
+
},
|
102
|
+
|
103
|
+
async beforeRestart() {
|
104
|
+
cleanRequireCache([require.resolve("./analyze")]);
|
105
|
+
}
|
106
|
+
|
107
|
+
};
|
108
|
+
}
|
109
|
+
}));
|
@@ -0,0 +1,35 @@
|
|
1
|
+
export const EN_LOCALE = {
|
2
|
+
command: {
|
3
|
+
shared: {
|
4
|
+
analyze: 'analyze bundle size',
|
5
|
+
config: 'specify config file'
|
6
|
+
},
|
7
|
+
dev: {
|
8
|
+
describe: 'start dev server',
|
9
|
+
entry: 'compiler by entry',
|
10
|
+
apiOnly: 'start api server only'
|
11
|
+
},
|
12
|
+
build: {
|
13
|
+
describe: 'build application'
|
14
|
+
},
|
15
|
+
start: {
|
16
|
+
describe: 'start server'
|
17
|
+
},
|
18
|
+
deploy: {
|
19
|
+
describe: 'deploy application'
|
20
|
+
},
|
21
|
+
new: {
|
22
|
+
describe: 'generator runner for MWA project',
|
23
|
+
debug: 'using debug mode to log something',
|
24
|
+
config: 'set default generator config(json string)',
|
25
|
+
distTag: `use specified tag version for it's generator`,
|
26
|
+
registry: 'set npm registry url to run npm command'
|
27
|
+
},
|
28
|
+
inspect: {
|
29
|
+
env: 'specify env mode',
|
30
|
+
output: 'specify inspect content output path',
|
31
|
+
noConsole: 'do not log the result in terminal',
|
32
|
+
verbose: 'show full function definitions in output'
|
33
|
+
}
|
34
|
+
}
|
35
|
+
};
|
@@ -0,0 +1,35 @@
|
|
1
|
+
export const ZH_LOCALE = {
|
2
|
+
command: {
|
3
|
+
shared: {
|
4
|
+
analyze: '分析构建产物体积,查看各个模块打包后的大小',
|
5
|
+
config: '指定配置文件路径,可以为相对路径或绝对路径'
|
6
|
+
},
|
7
|
+
dev: {
|
8
|
+
describe: '本地开发命令',
|
9
|
+
entry: '指定入口,编译特定的页面',
|
10
|
+
apiOnly: '仅启动 API 接口服务'
|
11
|
+
},
|
12
|
+
build: {
|
13
|
+
describe: '构建应用命令'
|
14
|
+
},
|
15
|
+
start: {
|
16
|
+
describe: '应用启动命令'
|
17
|
+
},
|
18
|
+
deploy: {
|
19
|
+
describe: '部署应用命令'
|
20
|
+
},
|
21
|
+
new: {
|
22
|
+
describe: 'MWA 项目中执行生成器',
|
23
|
+
debug: '开启 Debug 模式,打印调试日志信息',
|
24
|
+
config: '生成器运行默认配置(JSON 字符串)',
|
25
|
+
distTag: '生成器使用特殊的 npm Tag 版本',
|
26
|
+
registry: '生成器运行过程中定制 npm Registry'
|
27
|
+
},
|
28
|
+
inspect: {
|
29
|
+
env: '查看指定环境下的配置',
|
30
|
+
output: '指定在 dist 目录下输出的路径',
|
31
|
+
noConsole: '不在终端中输出完整结果',
|
32
|
+
verbose: '在结果中展示函数的完整内容'
|
33
|
+
}
|
34
|
+
}
|
35
|
+
};
|
@@ -0,0 +1,78 @@
|
|
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 { bundle } from '@modern-js/node-bundle-require';
|
9
|
+
import { CONFIG_FILE_EXTENSIONS, fs, getServerConfig, OUTPUT_CONFIG_FILE } from '@modern-js/utils';
|
10
|
+
export const defineServerConfig = config => config;
|
11
|
+
export const buildServerConfig = async ({
|
12
|
+
appDirectory,
|
13
|
+
distDirectory,
|
14
|
+
configFile,
|
15
|
+
options
|
16
|
+
}) => {
|
17
|
+
const configFilePath = await getServerConfig(appDirectory, configFile);
|
18
|
+
|
19
|
+
const getOutputFile = async filepath => path.resolve(distDirectory, `${filepath.replace(new RegExp(CONFIG_FILE_EXTENSIONS.join('|')), '')}.js`);
|
20
|
+
|
21
|
+
if (configFilePath) {
|
22
|
+
const configHelperFilePath = path.normalize(path.join(distDirectory, './config-helper.js'));
|
23
|
+
const helperCode = `
|
24
|
+
export const defineConfig = (config) => config;
|
25
|
+
`;
|
26
|
+
await fs.ensureDir(distDirectory);
|
27
|
+
await fs.writeFile(configHelperFilePath, helperCode);
|
28
|
+
await bundle(configFilePath, _objectSpread(_objectSpread({}, options), {}, {
|
29
|
+
getOutputFile,
|
30
|
+
esbuildPlugins: [{
|
31
|
+
name: 'native-build-config',
|
32
|
+
|
33
|
+
setup(ctx) {
|
34
|
+
ctx.onResolve({
|
35
|
+
filter: /app-tools\/server/
|
36
|
+
}, () => {
|
37
|
+
return {
|
38
|
+
path: configHelperFilePath
|
39
|
+
};
|
40
|
+
});
|
41
|
+
}
|
42
|
+
|
43
|
+
}]
|
44
|
+
}));
|
45
|
+
}
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
*
|
49
|
+
* 处理循环引用的 replacer
|
50
|
+
*/
|
51
|
+
|
52
|
+
export const safeReplacer = () => {
|
53
|
+
const cache = [];
|
54
|
+
const keyCache = [];
|
55
|
+
return function (key, value) {
|
56
|
+
if (typeof value === 'object' && value !== null) {
|
57
|
+
const index = cache.indexOf(value);
|
58
|
+
|
59
|
+
if (index !== -1) {
|
60
|
+
return `[Circular ${keyCache[index]}]`;
|
61
|
+
}
|
62
|
+
|
63
|
+
cache.push(value);
|
64
|
+
keyCache.push(key || 'root');
|
65
|
+
}
|
66
|
+
|
67
|
+
return value;
|
68
|
+
};
|
69
|
+
};
|
70
|
+
export const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
|
71
|
+
var _resolvedConfig$outpu;
|
72
|
+
|
73
|
+
const outputPath = path.join(appDirectory, (resolvedConfig === null || resolvedConfig === void 0 ? void 0 : (_resolvedConfig$outpu = resolvedConfig.output) === null || _resolvedConfig$outpu === void 0 ? void 0 : _resolvedConfig$outpu.path) || './dist', OUTPUT_CONFIG_FILE);
|
74
|
+
await fs.writeJSON(outputPath, resolvedConfig, {
|
75
|
+
spaces: 2,
|
76
|
+
replacer: safeReplacer()
|
77
|
+
});
|
78
|
+
};
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import { webpack } from '@modern-js/webpack';
|
2
|
+
import { chalk, logger, formatWebpackMessages, clearConsole } from '@modern-js/utils';
|
3
|
+
import { printInstructions } from "./printInstructions";
|
4
|
+
export const createCompiler = async ({
|
5
|
+
api,
|
6
|
+
webpackConfigs,
|
7
|
+
// TODO: params
|
8
|
+
userConfig,
|
9
|
+
appContext
|
10
|
+
}) => {
|
11
|
+
try {
|
12
|
+
const hookRunners = api.useHookRunners();
|
13
|
+
await hookRunners.beforeCreateCompiler({
|
14
|
+
webpackConfigs
|
15
|
+
});
|
16
|
+
const compiler = webpack(webpackConfigs);
|
17
|
+
await hookRunners.afterCreateCompiler({
|
18
|
+
compiler
|
19
|
+
});
|
20
|
+
let isFirstCompile = true;
|
21
|
+
compiler.hooks.invalid.tap('invalid', () => {
|
22
|
+
clearConsole();
|
23
|
+
logger.log('Compiling...');
|
24
|
+
});
|
25
|
+
compiler.hooks.done.tap('done', async stats => {
|
26
|
+
const statsData = stats.toJson({
|
27
|
+
preset: 'errors-warnings'
|
28
|
+
});
|
29
|
+
const {
|
30
|
+
errors,
|
31
|
+
warnings
|
32
|
+
} = formatWebpackMessages(statsData);
|
33
|
+
|
34
|
+
if (errors.length) {
|
35
|
+
logger.log(chalk.red(`Failed to compile.\n`));
|
36
|
+
logger.log(errors.join('\n\n'));
|
37
|
+
logger.log();
|
38
|
+
} else if (process.stdout.isTTY || isFirstCompile) {
|
39
|
+
await hookRunners.afterDev();
|
40
|
+
|
41
|
+
if (warnings.length) {
|
42
|
+
logger.log(chalk.yellow(`Compiled with warnings.\n`));
|
43
|
+
logger.log(warnings.join('\n\n'));
|
44
|
+
logger.log();
|
45
|
+
}
|
46
|
+
|
47
|
+
await printInstructions(hookRunners, appContext, userConfig);
|
48
|
+
}
|
49
|
+
|
50
|
+
isFirstCompile = false;
|
51
|
+
});
|
52
|
+
return compiler;
|
53
|
+
} catch (err) {
|
54
|
+
logger.log(chalk.red(`Failed to compile.`));
|
55
|
+
logger.log();
|
56
|
+
logger.log(err); // FIXME: 这里最好抛出异常,执行 process.exit 的地方尽可能少或者控制在几个统一的地方比较合适
|
57
|
+
// eslint-disable-next-line no-process-exit
|
58
|
+
|
59
|
+
process.exit(1);
|
60
|
+
}
|
61
|
+
};
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { Server } from '@modern-js/server';
|
2
|
+
let server = null;
|
3
|
+
export const getServer = () => server;
|
4
|
+
export const closeServer = async () => {
|
5
|
+
if (server) {
|
6
|
+
await server.close();
|
7
|
+
server = null;
|
8
|
+
}
|
9
|
+
};
|
10
|
+
export const createServer = async options => {
|
11
|
+
if (server) {
|
12
|
+
await server.close();
|
13
|
+
}
|
14
|
+
|
15
|
+
server = new Server(options);
|
16
|
+
const app = await server.init();
|
17
|
+
return app;
|
18
|
+
};
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { inquirer } from '@modern-js/utils';
|
2
|
+
export const getSpecifiedEntries = async (entry, entrypoints) => {
|
3
|
+
const entryNames = entrypoints.map(e => e.entryName);
|
4
|
+
|
5
|
+
if (!entry) {
|
6
|
+
return entryNames;
|
7
|
+
}
|
8
|
+
|
9
|
+
if (typeof entry === 'boolean') {
|
10
|
+
const {
|
11
|
+
selected
|
12
|
+
} = await inquirer.prompt([{
|
13
|
+
type: 'checkbox',
|
14
|
+
name: 'selected',
|
15
|
+
choices: entryNames,
|
16
|
+
message: '请选择需要构建的入口',
|
17
|
+
|
18
|
+
validate(answer) {
|
19
|
+
if (answer.length < 1) {
|
20
|
+
return 'You must choose at least one topping.';
|
21
|
+
}
|
22
|
+
|
23
|
+
return true;
|
24
|
+
}
|
25
|
+
|
26
|
+
}]);
|
27
|
+
return selected;
|
28
|
+
}
|
29
|
+
|
30
|
+
entry.forEach(name => {
|
31
|
+
if (!entryNames.includes(name)) {
|
32
|
+
throw new Error(`can not found entry ${name}, compiler entry should in ${entryNames.join(', ')}`);
|
33
|
+
}
|
34
|
+
});
|
35
|
+
return entry;
|
36
|
+
};
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { prettyInstructions, logger } from '@modern-js/utils';
|
2
|
+
export const printInstructions = async (hookRunners, appContext, config) => {
|
3
|
+
const message = prettyInstructions(appContext, config); // call beforePrintInstructions hook.
|
4
|
+
|
5
|
+
const {
|
6
|
+
instructions
|
7
|
+
} = await hookRunners.beforePrintInstructions({
|
8
|
+
instructions: message
|
9
|
+
});
|
10
|
+
logger.log(instructions);
|
11
|
+
};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import { fs, ROUTE_SPEC_FILE } from '@modern-js/utils';
|
3
|
+
|
4
|
+
const generateRoutes = async appContext => {
|
5
|
+
const {
|
6
|
+
serverRoutes,
|
7
|
+
distDirectory
|
8
|
+
} = appContext;
|
9
|
+
const output = JSON.stringify({
|
10
|
+
routes: serverRoutes
|
11
|
+
}, null, 2);
|
12
|
+
await fs.outputFile(path.join(distDirectory, ROUTE_SPEC_FILE), output);
|
13
|
+
};
|
14
|
+
|
15
|
+
export { generateRoutes };
|
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.PAGES_DIR_NAME = exports.JS_EXTENSIONS = exports.INDEX_FILE_NAME = exports.HTML_PARTIALS_FOLDER = exports.HTML_PARTIALS_EXTENSIONS = exports.FILE_SYSTEM_ROUTES_LAYOUT = exports.FILE_SYSTEM_ROUTES_INDEX = exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = exports.FILE_SYSTEM_ROUTES_FILE_NAME = exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = exports.ENTRY_POINT_FILE_NAME = exports.ENTRY_BOOTSTRAP_FILE_NAME = exports.APP_FILE_NAME = void 0;
|
7
|
+
const JS_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx'];
|
8
|
+
exports.JS_EXTENSIONS = JS_EXTENSIONS;
|
9
|
+
const INDEX_FILE_NAME = 'index';
|
10
|
+
exports.INDEX_FILE_NAME = INDEX_FILE_NAME;
|
11
|
+
const APP_FILE_NAME = 'App';
|
12
|
+
exports.APP_FILE_NAME = APP_FILE_NAME;
|
13
|
+
const PAGES_DIR_NAME = 'pages';
|
14
|
+
exports.PAGES_DIR_NAME = PAGES_DIR_NAME;
|
15
|
+
const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
|
16
|
+
exports.FILE_SYSTEM_ROUTES_FILE_NAME = FILE_SYSTEM_ROUTES_FILE_NAME;
|
17
|
+
const ENTRY_POINT_FILE_NAME = 'index.js';
|
18
|
+
exports.ENTRY_POINT_FILE_NAME = ENTRY_POINT_FILE_NAME;
|
19
|
+
const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
|
20
|
+
exports.ENTRY_BOOTSTRAP_FILE_NAME = ENTRY_BOOTSTRAP_FILE_NAME;
|
21
|
+
const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
|
22
|
+
exports.FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP;
|
23
|
+
const FILE_SYSTEM_ROUTES_LAYOUT = '_layout';
|
24
|
+
exports.FILE_SYSTEM_ROUTES_LAYOUT = FILE_SYSTEM_ROUTES_LAYOUT;
|
25
|
+
const FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = '_app';
|
26
|
+
exports.FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT = FILE_SYSTEM_ROUTES_GLOBAL_LAYOUT;
|
27
|
+
const FILE_SYSTEM_ROUTES_INDEX = 'index';
|
28
|
+
exports.FILE_SYSTEM_ROUTES_INDEX = FILE_SYSTEM_ROUTES_INDEX;
|
29
|
+
const FILE_SYSTEM_ROUTES_IGNORED_REGEX = /\.(d|test|spec|e2e)\.(js|jsx|ts|tsx)$/;
|
30
|
+
exports.FILE_SYSTEM_ROUTES_IGNORED_REGEX = FILE_SYSTEM_ROUTES_IGNORED_REGEX;
|
31
|
+
const HTML_PARTIALS_FOLDER = 'html';
|
32
|
+
exports.HTML_PARTIALS_FOLDER = HTML_PARTIALS_FOLDER;
|
33
|
+
const HTML_PARTIALS_EXTENSIONS = ['.htm', '.html', '.ejs'];
|
34
|
+
exports.HTML_PARTIALS_EXTENSIONS = HTML_PARTIALS_EXTENSIONS;
|
35
|
+
const FILE_SYSTEM_ROUTES_COMPONENTS_DIR = 'internal_components';
|
36
|
+
exports.FILE_SYSTEM_ROUTES_COMPONENTS_DIR = FILE_SYSTEM_ROUTES_COMPONENTS_DIR;
|