@modern-js/app-tools 1.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/modern.js +3 -0
- package/dist/js/modern/commands/build.js +116 -0
- package/dist/js/modern/commands/dev.js +65 -0
- package/dist/js/modern/commands/index.js +3 -0
- package/dist/js/modern/commands/start.js +32 -0
- package/dist/js/modern/index.js +60 -0
- package/dist/js/modern/lifecycle.js +20 -0
- package/dist/js/modern/locale/en.js +21 -0
- package/dist/js/modern/locale/index.js +9 -0
- package/dist/js/modern/locale/zh.js +21 -0
- package/dist/js/modern/utils/createCompiler.js +73 -0
- package/dist/js/modern/utils/createServer.js +12 -0
- package/dist/js/modern/utils/language.js +5 -0
- package/dist/js/modern/utils/printInstructions.js +17 -0
- package/dist/js/node/commands/build.js +135 -0
- package/dist/js/node/commands/dev.js +82 -0
- package/dist/js/node/commands/index.js +44 -0
- package/dist/js/node/commands/start.js +49 -0
- package/dist/js/node/index.js +87 -0
- package/dist/js/node/lifecycle.js +39 -0
- package/dist/js/node/locale/en.js +28 -0
- package/dist/js/node/locale/index.js +20 -0
- package/dist/js/node/locale/zh.js +28 -0
- package/dist/js/node/utils/createCompiler.js +95 -0
- package/dist/js/node/utils/createServer.js +23 -0
- package/dist/js/node/utils/language.js +13 -0
- package/dist/js/node/utils/printInstructions.js +29 -0
- package/dist/types/commands/build.d.ts +1 -0
- package/dist/types/commands/dev.d.ts +1 -0
- package/dist/types/commands/index.d.ts +3 -0
- package/dist/types/commands/start.d.ts +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/lifecycle.d.ts +17 -0
- package/dist/types/locale/en.d.ts +21 -0
- package/dist/types/locale/index.d.ts +44 -0
- package/dist/types/locale/zh.d.ts +21 -0
- package/dist/types/utils/createCompiler.d.ts +11 -0
- package/dist/types/utils/createServer.d.ts +2 -0
- package/dist/types/utils/language.d.ts +1 -0
- package/dist/types/utils/printInstructions.d.ts +2 -0
- package/lib/types.d.ts +138 -0
- package/modern.config.js +6 -0
- package/package.json +58 -0
- package/src/commands/build.ts +123 -0
- package/src/commands/dev.ts +80 -0
- package/src/commands/index.ts +3 -0
- package/src/commands/start.ts +30 -0
- package/src/index.ts +87 -0
- package/src/lifecycle.ts +37 -0
- package/src/locale/en.ts +17 -0
- package/src/locale/index.ts +9 -0
- package/src/locale/zh.ts +17 -0
- package/src/utils/createCompiler.ts +80 -0
- package/src/utils/createServer.ts +16 -0
- package/src/utils/language.ts +6 -0
- package/src/utils/printInstructions.ts +22 -0
- package/tsconfig.json +16 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @modern-js/app-tools
|
|
2
|
+
|
|
3
|
+
## 1.0.0-alpha.3
|
|
4
|
+
### Patch Changes
|
|
5
|
+
|
|
6
|
+
- feat: initial
|
|
7
|
+
- Updated dependencies [undefined]
|
|
8
|
+
- @modern-js/core@1.0.0-alpha.3
|
|
9
|
+
- @modern-js/i18n-cli-language-detector@1.0.0-alpha.3
|
|
10
|
+
- @modern-js/plugin-analyze@1.0.0-alpha.3
|
|
11
|
+
- @modern-js/plugin-fast-refresh@1.0.0-alpha.3
|
|
12
|
+
- @modern-js/plugin-i18n@1.0.0-alpha.3
|
|
13
|
+
- @modern-js/webpack@1.0.0-alpha.3
|
|
14
|
+
- @modern-js/new-action@1.0.0-alpha.3
|
|
15
|
+
- @modern-js/plugin-polyfill@1.0.0-alpha.3
|
|
16
|
+
- @modern-js/server@1.0.0-alpha.3
|
|
17
|
+
- @modern-js/plugin@1.0.0-alpha.3
|
|
18
|
+
- @modern-js/types@1.0.0-alpha.3
|
|
19
|
+
- @modern-js/utils@1.0.0-alpha.3
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Modern.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
<p align="center">
|
|
3
|
+
<a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">现代 Web 工程体系</p>
|
|
7
|
+
|
|
8
|
+
## 背景
|
|
9
|
+
- [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
|
|
10
|
+
- [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
|
|
11
|
+
|
|
12
|
+
## 计划
|
|
13
|
+
|
|
14
|
+
Modern.js 的 1.0.0.rc 版已经发到 npm,目前在做测试改进,README 文档之后统一提供(现阶段加入测试和开发,可以发 [issue](https://github.com/modern-js-dev/modern.js/issues) 留微信联系),完整的文档站计划在10月14日上线
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
package/bin/modern.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { webpack } from 'webpack';
|
|
2
|
+
import { WebpackConfigTarget, getWebpackConfig } from '@modern-js/webpack';
|
|
3
|
+
import { useAppContext, useResolvedConfigContext, mountHook } from '@modern-js/core';
|
|
4
|
+
import { fs, formatWebpackMessages, measureFileSizesBeforeBuild, printFileSizesAfterBuild, printBuildError, logger, isUseSSRBundle } from '@modern-js/utils'; // These sizes are pretty large. We'll warn for bundles exceeding them.
|
|
5
|
+
|
|
6
|
+
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
|
7
|
+
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
|
8
|
+
export const build = async () => {
|
|
9
|
+
const webpackBuild = async (webpackConfig, type) => {
|
|
10
|
+
const compiler = webpack(webpackConfig);
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
let label = process.env.NODE_ENV || '';
|
|
13
|
+
|
|
14
|
+
if (type && type !== 'legacy') {
|
|
15
|
+
label += ` ${type}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
logger.info(`Creating a ${label} build...`);
|
|
19
|
+
compiler.run((err, stats) => {
|
|
20
|
+
let messages;
|
|
21
|
+
|
|
22
|
+
if (!err) {
|
|
23
|
+
messages = formatWebpackMessages(stats.toJson({
|
|
24
|
+
all: false,
|
|
25
|
+
warnings: true,
|
|
26
|
+
errors: true
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
if (messages.errors.length === 0) {
|
|
30
|
+
logger.info(`File sizes after ${label} build:\n`);
|
|
31
|
+
printFileSizesAfterBuild(stats, previousFileSizes, outputPath, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
|
|
32
|
+
logger.log();
|
|
33
|
+
}
|
|
34
|
+
} // When using run or watch, call close and wait for it to finish before calling run or watch again.
|
|
35
|
+
// Concurrent compilations will corrupt the output files.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
compiler.close(closeErr => {
|
|
39
|
+
if (closeErr) {
|
|
40
|
+
logger.error(closeErr);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (err) {
|
|
44
|
+
reject(err);
|
|
45
|
+
} else {
|
|
46
|
+
if (messages.errors.length) {
|
|
47
|
+
reject(new Error(messages.errors.join('\n\n')));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
resolve({
|
|
52
|
+
warnings: messages.warnings
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
/* eslint-disable react-hooks/rules-of-hooks */
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
const {
|
|
63
|
+
value: resolvedConfig
|
|
64
|
+
} = useResolvedConfigContext();
|
|
65
|
+
const {
|
|
66
|
+
value: appContext
|
|
67
|
+
} = useAppContext();
|
|
68
|
+
/* eslint-enable react-hooks/rules-of-hooks */
|
|
69
|
+
|
|
70
|
+
const outputPath = appContext.distDirectory;
|
|
71
|
+
const previousFileSizes = await measureFileSizesBeforeBuild(outputPath);
|
|
72
|
+
fs.emptyDirSync(outputPath);
|
|
73
|
+
const buildConfigs = [];
|
|
74
|
+
buildConfigs.push({
|
|
75
|
+
type: 'legacy',
|
|
76
|
+
config: getWebpackConfig(WebpackConfigTarget.CLIENT)
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (resolvedConfig.output.enableModernMode) {
|
|
80
|
+
buildConfigs.push({
|
|
81
|
+
type: 'modern',
|
|
82
|
+
config: getWebpackConfig(WebpackConfigTarget.MODERN)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (isUseSSRBundle(resolvedConfig)) {
|
|
87
|
+
buildConfigs.push({
|
|
88
|
+
type: 'ssr',
|
|
89
|
+
config: getWebpackConfig(WebpackConfigTarget.NODE)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await mountHook().beforeBuild({
|
|
94
|
+
webpackConfigs: buildConfigs.map(({
|
|
95
|
+
config
|
|
96
|
+
}) => config)
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
for (const buildConfig of buildConfigs) {
|
|
100
|
+
const {
|
|
101
|
+
type: buildType,
|
|
102
|
+
config
|
|
103
|
+
} = buildConfig;
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
await webpackBuild(config, buildType);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
printBuildError(error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
await mountHook().afterBuild(); // force exit after build.
|
|
113
|
+
// eslint-disable-next-line no-process-exit
|
|
114
|
+
|
|
115
|
+
process.exit(0);
|
|
116
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
|
|
8
|
+
import { fs, logger, HMR_SOCK_PATH, clearConsole, chalk, isSSR } from '@modern-js/utils';
|
|
9
|
+
import { useAppContext, useResolvedConfigContext, mountHook } from '@modern-js/core';
|
|
10
|
+
import { createCompiler } from "../utils/createCompiler";
|
|
11
|
+
import { createServer } from "../utils/createServer";
|
|
12
|
+
export const dev = async () => {
|
|
13
|
+
/* eslint-disable react-hooks/rules-of-hooks */
|
|
14
|
+
const {
|
|
15
|
+
value: appContext
|
|
16
|
+
} = useAppContext();
|
|
17
|
+
const {
|
|
18
|
+
value: userConfig
|
|
19
|
+
} = useResolvedConfigContext();
|
|
20
|
+
/* eslint-enable react-hooks/rules-of-hooks */
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
appDirectory,
|
|
24
|
+
distDirectory,
|
|
25
|
+
port
|
|
26
|
+
} = appContext;
|
|
27
|
+
fs.emptyDirSync(distDirectory);
|
|
28
|
+
await mountHook().beforeDev();
|
|
29
|
+
const webpackConfigs = [isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE), getWebpackConfig(WebpackConfigTarget.CLIENT)].filter(Boolean);
|
|
30
|
+
const compiler = await createCompiler({
|
|
31
|
+
webpackConfigs,
|
|
32
|
+
userConfig,
|
|
33
|
+
appContext
|
|
34
|
+
});
|
|
35
|
+
const app = await createServer({
|
|
36
|
+
dev: _objectSpread(_objectSpread({}, {
|
|
37
|
+
client: {
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
39
|
+
port: port.toString(),
|
|
40
|
+
overlay: false,
|
|
41
|
+
logging: 'none',
|
|
42
|
+
path: HMR_SOCK_PATH,
|
|
43
|
+
host: 'localhost'
|
|
44
|
+
},
|
|
45
|
+
dev: {
|
|
46
|
+
writeToDisk: file => !file.includes('.hot-update.')
|
|
47
|
+
},
|
|
48
|
+
hot: true,
|
|
49
|
+
liveReload: true,
|
|
50
|
+
port
|
|
51
|
+
}), userConfig.tools.devServer),
|
|
52
|
+
compiler,
|
|
53
|
+
pwd: appDirectory,
|
|
54
|
+
config: userConfig,
|
|
55
|
+
plugins: appContext.plugins.filter(p => p.server).map(p => p.server)
|
|
56
|
+
});
|
|
57
|
+
app.listen(port, err => {
|
|
58
|
+
if (err) {
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
clearConsole();
|
|
63
|
+
logger.log(chalk.cyan(`Starting the development server...`));
|
|
64
|
+
});
|
|
65
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { logger, chalk } from '@modern-js/utils';
|
|
2
|
+
import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
|
3
|
+
import server from '@modern-js/server';
|
|
4
|
+
import { printInstructions } from "../utils/printInstructions";
|
|
5
|
+
export const start = async () => {
|
|
6
|
+
/* eslint-disable react-hooks/rules-of-hooks */
|
|
7
|
+
const {
|
|
8
|
+
value: appContext
|
|
9
|
+
} = useAppContext();
|
|
10
|
+
const {
|
|
11
|
+
value: userConfig
|
|
12
|
+
} = useResolvedConfigContext();
|
|
13
|
+
/* eslint-enable react-hooks/rules-of-hooks */
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
appDirectory,
|
|
17
|
+
port
|
|
18
|
+
} = appContext;
|
|
19
|
+
logger.log(chalk.cyan(`Starting the modern server...`));
|
|
20
|
+
const app = await server({
|
|
21
|
+
pwd: appDirectory,
|
|
22
|
+
config: userConfig,
|
|
23
|
+
plugins: appContext.plugins.filter(p => p.server).map(p => p.server)
|
|
24
|
+
});
|
|
25
|
+
app.listen(port, async err => {
|
|
26
|
+
if (err) {
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await printInstructions(appContext);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 { createPlugin, defineConfig, usePlugins, cli } from '@modern-js/core';
|
|
8
|
+
import { lifecycle } from "./lifecycle";
|
|
9
|
+
import { i18n, localeKeys } from "./locale";
|
|
10
|
+
import { getLocaleLanguage } from "./utils/language";
|
|
11
|
+
import { start } from "./commands/start";
|
|
12
|
+
import { dev } from "./commands/dev";
|
|
13
|
+
export { defineConfig }; // eslint-disable-next-line react-hooks/rules-of-hooks
|
|
14
|
+
|
|
15
|
+
usePlugins([require.resolve('@modern-js/plugin-analyze/cli'), require.resolve('@modern-js/plugin-fast-refresh/cli'), require.resolve('@modern-js/plugin-polyfill/cli')]);
|
|
16
|
+
export default createPlugin(() => {
|
|
17
|
+
const locale = getLocaleLanguage();
|
|
18
|
+
i18n.changeLanguage({
|
|
19
|
+
locale
|
|
20
|
+
});
|
|
21
|
+
lifecycle();
|
|
22
|
+
return {
|
|
23
|
+
commands({
|
|
24
|
+
program
|
|
25
|
+
}) {
|
|
26
|
+
program.command('dev').usage('[options]').description(i18n.t(localeKeys.command.dev.describe)).option('-c --config <config>', i18n.t(localeKeys.command.dev.config)).action(async () => {
|
|
27
|
+
await dev();
|
|
28
|
+
});
|
|
29
|
+
program.command('build').usage('[options]').description(i18n.t(localeKeys.command.build.describe)).action(async () => {
|
|
30
|
+
const {
|
|
31
|
+
build
|
|
32
|
+
} = await import("./commands/build");
|
|
33
|
+
await build();
|
|
34
|
+
});
|
|
35
|
+
program.command('start').usage('[options]').description(i18n.t(localeKeys.command.start.describe)).action(async () => {
|
|
36
|
+
await start();
|
|
37
|
+
});
|
|
38
|
+
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 => {
|
|
39
|
+
const {
|
|
40
|
+
MWANewAction
|
|
41
|
+
} = await import('@modern-js/new-action');
|
|
42
|
+
await MWANewAction(_objectSpread(_objectSpread({}, options), {}, {
|
|
43
|
+
locale
|
|
44
|
+
}));
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
async fileChange() {
|
|
49
|
+
// restart cli.
|
|
50
|
+
const shouldRestart = await cli.restart();
|
|
51
|
+
|
|
52
|
+
if (shouldRestart) {
|
|
53
|
+
await dev();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
};
|
|
58
|
+
}, {
|
|
59
|
+
post: ['@modern-js/plugin-analyze', '@modern-js/plugin-fast-refresh', '@modern-js/plugin-ssr', '@modern-js/plugin-state', '@modern-js/plugin-router', '@modern-js/plugin-polyfill']
|
|
60
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createAsyncWaterfall, createAsyncWorkflow } from '@modern-js/plugin';
|
|
2
|
+
import { registerHook } from '@modern-js/core';
|
|
3
|
+
export const beforeDev = createAsyncWorkflow();
|
|
4
|
+
export const afterDev = createAsyncWorkflow();
|
|
5
|
+
export const beforeCreateCompiler = createAsyncWorkflow();
|
|
6
|
+
export const afterCreateCompiler = createAsyncWorkflow();
|
|
7
|
+
export const beforePrintInstructions = createAsyncWaterfall();
|
|
8
|
+
export const beforeBuild = createAsyncWorkflow();
|
|
9
|
+
export const afterBuild = createAsyncWorkflow();
|
|
10
|
+
export const lifecycle = () => {
|
|
11
|
+
registerHook({
|
|
12
|
+
beforeDev,
|
|
13
|
+
afterDev,
|
|
14
|
+
beforeCreateCompiler,
|
|
15
|
+
afterCreateCompiler,
|
|
16
|
+
beforePrintInstructions,
|
|
17
|
+
beforeBuild,
|
|
18
|
+
afterBuild
|
|
19
|
+
});
|
|
20
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const EN_LOCALE = {
|
|
2
|
+
command: {
|
|
3
|
+
dev: {
|
|
4
|
+
describe: 'start dev server',
|
|
5
|
+
config: 'specify config file'
|
|
6
|
+
},
|
|
7
|
+
build: {
|
|
8
|
+
describe: 'build application'
|
|
9
|
+
},
|
|
10
|
+
start: {
|
|
11
|
+
describe: 'start server'
|
|
12
|
+
},
|
|
13
|
+
new: {
|
|
14
|
+
describe: 'generator runner for MWA project',
|
|
15
|
+
debug: 'using debug mode to log something',
|
|
16
|
+
config: 'set default generator config(json string)',
|
|
17
|
+
distTag: `use specified tag version for it's generator`,
|
|
18
|
+
registry: 'set npm registry url to run npm command'
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const ZH_LOCALE = {
|
|
2
|
+
command: {
|
|
3
|
+
dev: {
|
|
4
|
+
describe: '本地开发命令',
|
|
5
|
+
config: '制定配置文件路径'
|
|
6
|
+
},
|
|
7
|
+
build: {
|
|
8
|
+
describe: '构建应用命令'
|
|
9
|
+
},
|
|
10
|
+
start: {
|
|
11
|
+
describe: '应用启动命令'
|
|
12
|
+
},
|
|
13
|
+
new: {
|
|
14
|
+
describe: 'MWA 项目中中执行生成器',
|
|
15
|
+
debug: '开启 Debug 模式,打印调试日志信息',
|
|
16
|
+
config: '生成器运行默认配置(JSON 字符串)',
|
|
17
|
+
distTag: '生成器使用特殊的 npm Tag 版本',
|
|
18
|
+
registry: '生成器运行过程中定制 npm Registry'
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import webpack from 'webpack';
|
|
2
|
+
import { mountHook } from '@modern-js/core';
|
|
3
|
+
import { chalk, logger, formatWebpackMessages, clearConsole } from '@modern-js/utils';
|
|
4
|
+
import { printInstructions } from "./printInstructions";
|
|
5
|
+
|
|
6
|
+
const prettyTime = stats => {
|
|
7
|
+
var _stats$children;
|
|
8
|
+
|
|
9
|
+
return Math.max(...(((_stats$children = stats.children) === null || _stats$children === void 0 ? void 0 : _stats$children.map(child => child.time || 0)) || []));
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const createCompiler = async ({
|
|
13
|
+
webpackConfigs,
|
|
14
|
+
// TODO: params
|
|
15
|
+
userConfig: _userConfig,
|
|
16
|
+
appContext
|
|
17
|
+
}) => {
|
|
18
|
+
try {
|
|
19
|
+
await mountHook().beforeCreateCompiler({
|
|
20
|
+
webpackConfigs
|
|
21
|
+
});
|
|
22
|
+
const compiler = webpack(webpackConfigs);
|
|
23
|
+
await mountHook().afterCreateCompiler({
|
|
24
|
+
compiler
|
|
25
|
+
});
|
|
26
|
+
let isFirstCompile = true;
|
|
27
|
+
compiler.hooks.invalid.tap('invalid', () => {
|
|
28
|
+
clearConsole();
|
|
29
|
+
logger.log('Compiling...');
|
|
30
|
+
});
|
|
31
|
+
compiler.hooks.done.tap('done', async stats => {
|
|
32
|
+
clearConsole();
|
|
33
|
+
const statsData = stats.toJson({
|
|
34
|
+
all: false,
|
|
35
|
+
warnings: true,
|
|
36
|
+
errors: true,
|
|
37
|
+
timings: true
|
|
38
|
+
});
|
|
39
|
+
const {
|
|
40
|
+
errors,
|
|
41
|
+
warnings
|
|
42
|
+
} = formatWebpackMessages(statsData);
|
|
43
|
+
|
|
44
|
+
if (errors.length) {
|
|
45
|
+
logger.log(chalk.red(`Failed to compile.\n`));
|
|
46
|
+
logger.log(errors.join('\n\n'));
|
|
47
|
+
logger.log();
|
|
48
|
+
} else if (process.stdout.isTTY || isFirstCompile) {
|
|
49
|
+
await mountHook().afterDev();
|
|
50
|
+
|
|
51
|
+
if (warnings.length) {
|
|
52
|
+
logger.log(chalk.yellow(`Compiled with warnings.\n`));
|
|
53
|
+
logger.log(warnings.join('\n\n'));
|
|
54
|
+
logger.log();
|
|
55
|
+
} else {
|
|
56
|
+
logger.log(chalk.green(`Compiled successfully in ${prettyTime(statsData)} ms.\n`));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await printInstructions(appContext);
|
|
60
|
+
} // eslint-disable-next-line require-atomic-updates
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
isFirstCompile = false;
|
|
64
|
+
});
|
|
65
|
+
return compiler;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
logger.log(chalk.red(`Failed to compile.`));
|
|
68
|
+
logger.log();
|
|
69
|
+
logger.log(err); // eslint-disable-next-line no-process-exit
|
|
70
|
+
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Server } from '@modern-js/server';
|
|
2
|
+
let server;
|
|
3
|
+
export const createServer = async options => {
|
|
4
|
+
if (server) {
|
|
5
|
+
await server.close();
|
|
6
|
+
} // eslint-disable-next-line require-atomic-updates
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
server = new Server(options);
|
|
10
|
+
const app = await server.init();
|
|
11
|
+
return app;
|
|
12
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { prettyInstructions, logger, isDev, chalk } from '@modern-js/utils';
|
|
2
|
+
import { mountHook } from '@modern-js/core';
|
|
3
|
+
export const printInstructions = async appContext => {
|
|
4
|
+
let message = prettyInstructions(appContext);
|
|
5
|
+
|
|
6
|
+
if (isDev()) {
|
|
7
|
+
message += `\n${chalk.cyanBright([`Note that the development build is not optimized.`, `To create a production build, use yarn build.`].join('\n'))}`;
|
|
8
|
+
} // call beforePrintInstructions hook.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const {
|
|
12
|
+
instructions
|
|
13
|
+
} = await mountHook().beforePrintInstructions({
|
|
14
|
+
instructions: message
|
|
15
|
+
});
|
|
16
|
+
logger.log(instructions);
|
|
17
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.build = void 0;
|
|
7
|
+
|
|
8
|
+
var _webpack = require("webpack");
|
|
9
|
+
|
|
10
|
+
var _webpack2 = require("@modern-js/webpack");
|
|
11
|
+
|
|
12
|
+
var _core = require("@modern-js/core");
|
|
13
|
+
|
|
14
|
+
var _utils = require("@modern-js/utils");
|
|
15
|
+
|
|
16
|
+
// These sizes are pretty large. We'll warn for bundles exceeding them.
|
|
17
|
+
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
|
|
18
|
+
const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
const build = async () => {
|
|
21
|
+
const webpackBuild = async (webpackConfig, type) => {
|
|
22
|
+
const compiler = (0, _webpack.webpack)(webpackConfig);
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
let label = process.env.NODE_ENV || '';
|
|
25
|
+
|
|
26
|
+
if (type && type !== 'legacy') {
|
|
27
|
+
label += ` ${type}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_utils.logger.info(`Creating a ${label} build...`);
|
|
31
|
+
|
|
32
|
+
compiler.run((err, stats) => {
|
|
33
|
+
let messages;
|
|
34
|
+
|
|
35
|
+
if (!err) {
|
|
36
|
+
messages = (0, _utils.formatWebpackMessages)(stats.toJson({
|
|
37
|
+
all: false,
|
|
38
|
+
warnings: true,
|
|
39
|
+
errors: true
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
if (messages.errors.length === 0) {
|
|
43
|
+
_utils.logger.info(`File sizes after ${label} build:\n`);
|
|
44
|
+
|
|
45
|
+
(0, _utils.printFileSizesAfterBuild)(stats, previousFileSizes, outputPath, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
|
|
46
|
+
|
|
47
|
+
_utils.logger.log();
|
|
48
|
+
}
|
|
49
|
+
} // When using run or watch, call close and wait for it to finish before calling run or watch again.
|
|
50
|
+
// Concurrent compilations will corrupt the output files.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
compiler.close(closeErr => {
|
|
54
|
+
if (closeErr) {
|
|
55
|
+
_utils.logger.error(closeErr);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (err) {
|
|
59
|
+
reject(err);
|
|
60
|
+
} else {
|
|
61
|
+
if (messages.errors.length) {
|
|
62
|
+
reject(new Error(messages.errors.join('\n\n')));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
resolve({
|
|
67
|
+
warnings: messages.warnings
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
/* eslint-disable react-hooks/rules-of-hooks */
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const {
|
|
78
|
+
value: resolvedConfig
|
|
79
|
+
} = (0, _core.useResolvedConfigContext)();
|
|
80
|
+
const {
|
|
81
|
+
value: appContext
|
|
82
|
+
} = (0, _core.useAppContext)();
|
|
83
|
+
/* eslint-enable react-hooks/rules-of-hooks */
|
|
84
|
+
|
|
85
|
+
const outputPath = appContext.distDirectory;
|
|
86
|
+
const previousFileSizes = await (0, _utils.measureFileSizesBeforeBuild)(outputPath);
|
|
87
|
+
|
|
88
|
+
_utils.fs.emptyDirSync(outputPath);
|
|
89
|
+
|
|
90
|
+
const buildConfigs = [];
|
|
91
|
+
buildConfigs.push({
|
|
92
|
+
type: 'legacy',
|
|
93
|
+
config: (0, _webpack2.getWebpackConfig)(_webpack2.WebpackConfigTarget.CLIENT)
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (resolvedConfig.output.enableModernMode) {
|
|
97
|
+
buildConfigs.push({
|
|
98
|
+
type: 'modern',
|
|
99
|
+
config: (0, _webpack2.getWebpackConfig)(_webpack2.WebpackConfigTarget.MODERN)
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if ((0, _utils.isUseSSRBundle)(resolvedConfig)) {
|
|
104
|
+
buildConfigs.push({
|
|
105
|
+
type: 'ssr',
|
|
106
|
+
config: (0, _webpack2.getWebpackConfig)(_webpack2.WebpackConfigTarget.NODE)
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await (0, _core.mountHook)().beforeBuild({
|
|
111
|
+
webpackConfigs: buildConfigs.map(({
|
|
112
|
+
config
|
|
113
|
+
}) => config)
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
for (const buildConfig of buildConfigs) {
|
|
117
|
+
const {
|
|
118
|
+
type: buildType,
|
|
119
|
+
config
|
|
120
|
+
} = buildConfig;
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
await webpackBuild(config, buildType);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
(0, _utils.printBuildError)(error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await (0, _core.mountHook)().afterBuild(); // force exit after build.
|
|
130
|
+
// eslint-disable-next-line no-process-exit
|
|
131
|
+
|
|
132
|
+
process.exit(0);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
exports.build = build;
|