@modern-js/app-tools 1.3.1 → 1.3.2

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 CHANGED
@@ -1,5 +1,26 @@
1
1
  # @modern-js/app-tools
2
2
 
3
+ ## 1.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - Updated dependencies [c3de9882]
10
+ - Updated dependencies [33ff48af]
11
+ - Updated dependencies [b7c48198]
12
+ - @modern-js/core@1.3.2
13
+ - @modern-js/i18n-cli-language-detector@1.2.1
14
+ - @modern-js/plugin-analyze@1.2.1
15
+ - @modern-js/plugin-fast-refresh@1.2.1
16
+ - @modern-js/plugin-i18n@1.2.1
17
+ - @modern-js/webpack@1.2.1
18
+ - @modern-js/new-action@1.3.1
19
+ - @modern-js/server@1.3.2
20
+ - @modern-js/plugin@1.2.1
21
+ - @modern-js/types@1.2.1
22
+ - @modern-js/utils@1.2.2
23
+
3
24
  ## 1.3.1
4
25
 
5
26
  ### Patch Changes
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.3.1",
14
+ "version": "1.3.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -50,17 +50,17 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@babel/runtime": "^7",
53
- "@modern-js/core": "^1.3.1",
54
- "@modern-js/types": "^1.2.0",
55
- "@modern-js/i18n-cli-language-detector": "^1.2.0",
56
- "@modern-js/new-action": "^1.3.0",
57
- "@modern-js/plugin": "^1.2.0",
58
- "@modern-js/plugin-analyze": "^1.2.0",
59
- "@modern-js/plugin-fast-refresh": "^1.2.0",
60
- "@modern-js/plugin-i18n": "^1.2.0",
61
- "@modern-js/server": "^1.3.1",
62
- "@modern-js/utils": "^1.2.1",
63
- "@modern-js/webpack": "^1.2.0",
53
+ "@modern-js/core": "^1.3.2",
54
+ "@modern-js/types": "^1.2.1",
55
+ "@modern-js/i18n-cli-language-detector": "^1.2.1",
56
+ "@modern-js/new-action": "^1.3.1",
57
+ "@modern-js/plugin": "^1.2.1",
58
+ "@modern-js/plugin-analyze": "^1.2.1",
59
+ "@modern-js/plugin-fast-refresh": "^1.2.1",
60
+ "@modern-js/plugin-i18n": "^1.2.1",
61
+ "@modern-js/server": "^1.3.2",
62
+ "@modern-js/utils": "^1.2.2",
63
+ "@modern-js/webpack": "^1.2.1",
64
64
  "webpack": "^5.54.0"
65
65
  },
66
66
  "devDependencies": {
@@ -1,129 +0,0 @@
1
- import { Configuration, webpack } from 'webpack';
2
- import { WebpackConfigTarget, getWebpackConfig } from '@modern-js/webpack';
3
- import {
4
- useAppContext,
5
- useResolvedConfigContext,
6
- mountHook,
7
- ResolvedConfigContext,
8
- manager,
9
- } from '@modern-js/core';
10
- import {
11
- fs,
12
- formatWebpackMessages,
13
- measureFileSizesBeforeBuild,
14
- printFileSizesAfterBuild,
15
- printBuildError,
16
- logger,
17
- isUseSSRBundle,
18
- } from '@modern-js/utils';
19
-
20
- // These sizes are pretty large. We'll warn for bundles exceeding them.
21
- const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
22
- const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
23
-
24
- interface CliOptions {
25
- analyze?: boolean;
26
- }
27
-
28
- export const build = async (options?: CliOptions) => {
29
- const webpackBuild = async (webpackConfig: Configuration, type?: string) => {
30
- const compiler = webpack(webpackConfig);
31
-
32
- return new Promise((resolve, reject) => {
33
- let label = process.env.NODE_ENV || '';
34
- if (type && type !== 'legacy') {
35
- label += ` ${type}`;
36
- }
37
- logger.info(`Creating a ${label} build...`);
38
-
39
- compiler.run((err, stats) => {
40
- let messages: {
41
- errors: any;
42
- warnings: any;
43
- };
44
- if (!err) {
45
- messages = formatWebpackMessages(
46
- stats!.toJson({ all: false, warnings: true, errors: true }),
47
- );
48
-
49
- if (messages.errors.length === 0) {
50
- logger.info(`File sizes after ${label} build:\n`);
51
- printFileSizesAfterBuild(
52
- stats,
53
- previousFileSizes,
54
- outputPath,
55
- WARN_AFTER_BUNDLE_GZIP_SIZE,
56
- WARN_AFTER_CHUNK_GZIP_SIZE,
57
- );
58
- logger.log();
59
- }
60
- }
61
-
62
- // When using run or watch, call close and wait for it to finish before calling run or watch again.
63
- // Concurrent compilations will corrupt the output files.
64
- compiler.close(closeErr => {
65
- if (closeErr) {
66
- logger.error(closeErr);
67
- }
68
- if (err) {
69
- reject(err);
70
- } else {
71
- if (messages.errors.length) {
72
- reject(new Error(messages.errors.join('\n\n')));
73
- return;
74
- }
75
- resolve({ warnings: messages.warnings });
76
- }
77
- });
78
- });
79
- });
80
- };
81
-
82
- /* eslint-disable react-hooks/rules-of-hooks */
83
- const resolvedConfig = useResolvedConfigContext();
84
- const appContext = useAppContext();
85
- /* eslint-enable react-hooks/rules-of-hooks */
86
-
87
- manager.run(() => {
88
- ResolvedConfigContext.set({ ...resolvedConfig, cliOptions: options });
89
- });
90
-
91
- const outputPath = appContext.distDirectory;
92
- const previousFileSizes = await measureFileSizesBeforeBuild(outputPath);
93
- fs.emptyDirSync(outputPath);
94
-
95
- const buildConfigs = [];
96
-
97
- buildConfigs.push({
98
- type: 'legacy',
99
- config: getWebpackConfig(WebpackConfigTarget.CLIENT)!,
100
- });
101
-
102
- if (resolvedConfig.output.enableModernMode) {
103
- buildConfigs.push({
104
- type: 'modern',
105
- config: getWebpackConfig(WebpackConfigTarget.MODERN)!,
106
- });
107
- }
108
-
109
- if (isUseSSRBundle(resolvedConfig)) {
110
- buildConfigs.push({
111
- type: 'ssr',
112
- config: getWebpackConfig(WebpackConfigTarget.NODE)!,
113
- });
114
- }
115
-
116
- await (mountHook() as any).beforeBuild({
117
- webpackConfigs: buildConfigs.map(({ config }) => config),
118
- });
119
-
120
- for (const buildConfig of buildConfigs) {
121
- const { type: buildType, config } = buildConfig;
122
- try {
123
- await webpackBuild(config, buildType);
124
- } catch (error) {
125
- printBuildError(error as Error);
126
- }
127
- }
128
- await (mountHook() as any).afterBuild();
129
- };
@@ -1,6 +0,0 @@
1
- import { mountHook } from '@modern-js/core';
2
-
3
- export const deploy = async (options: any) => {
4
- await (mountHook() as any).beforeDeploy(options);
5
- await (mountHook() as any).afterDeploy(options);
6
- };
@@ -1,80 +0,0 @@
1
- import {
2
- Configuration,
3
- getWebpackConfig,
4
- WebpackConfigTarget,
5
- } from '@modern-js/webpack';
6
- import {
7
- fs,
8
- logger,
9
- HMR_SOCK_PATH,
10
- clearConsole,
11
- chalk,
12
- isSSR,
13
- } from '@modern-js/utils';
14
- import {
15
- useAppContext,
16
- useResolvedConfigContext,
17
- mountHook,
18
- } from '@modern-js/core';
19
- import { createCompiler } from '../utils/createCompiler';
20
- import { createServer } from '../utils/createServer';
21
-
22
- export const dev = async () => {
23
- /* eslint-disable react-hooks/rules-of-hooks */
24
- const appContext = useAppContext();
25
- const userConfig = useResolvedConfigContext();
26
- /* eslint-enable react-hooks/rules-of-hooks */
27
-
28
- const { appDirectory, distDirectory, port } = appContext;
29
-
30
- fs.emptyDirSync(distDirectory);
31
-
32
- await (mountHook() as any).beforeDev();
33
-
34
- const webpackConfigs = [
35
- isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE),
36
- getWebpackConfig(WebpackConfigTarget.CLIENT),
37
- ].filter(Boolean) as Configuration[];
38
-
39
- const compiler = await createCompiler({
40
- webpackConfigs,
41
- userConfig,
42
- appContext,
43
- });
44
-
45
- const app = await createServer({
46
- dev: {
47
- ...{
48
- client: {
49
- port: port!.toString(),
50
- overlay: false,
51
- logging: 'none',
52
- path: HMR_SOCK_PATH,
53
- host: 'localhost',
54
- },
55
- dev: { writeToDisk: (file: string) => !file.includes('.hot-update.') },
56
- hot: true,
57
- liveReload: true,
58
- port,
59
- https: userConfig.dev.https,
60
- },
61
- ...userConfig.tools.devServer,
62
- },
63
- compiler,
64
- pwd: appDirectory,
65
- config: userConfig as any,
66
- plugins: appContext.plugins
67
- .filter((p: any) => p.server)
68
- .map((p: any) => p.server),
69
- });
70
-
71
- app.listen(port, (err: Error) => {
72
- if (err) {
73
- throw err;
74
- }
75
-
76
- clearConsole();
77
-
78
- logger.log(chalk.cyan(`Starting the development server...`));
79
- });
80
- };
@@ -1,3 +0,0 @@
1
- export * from './dev';
2
- export * from './build';
3
- export * from './start';
@@ -1,30 +0,0 @@
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
-
6
- export const start = async () => {
7
- /* eslint-disable react-hooks/rules-of-hooks */
8
- const appContext = useAppContext();
9
- const userConfig = useResolvedConfigContext();
10
- /* eslint-enable react-hooks/rules-of-hooks */
11
-
12
- const { appDirectory, port } = appContext;
13
-
14
- logger.log(chalk.cyan(`Starting the modern server...`));
15
-
16
- const app = await server({
17
- pwd: appDirectory,
18
- config: userConfig as any,
19
- plugins: appContext.plugins
20
- .filter((p: any) => p.server)
21
- .map((p: any) => p.server),
22
- });
23
-
24
- app.listen(port, async (err: Error) => {
25
- if (err) {
26
- throw err;
27
- }
28
- await printInstructions(appContext, userConfig);
29
- });
30
- };
package/src/index.ts DELETED
@@ -1,99 +0,0 @@
1
- import { createPlugin, defineConfig, usePlugins, cli } from '@modern-js/core';
2
- import { lifecycle } from './lifecycle';
3
- import { i18n, localeKeys } from './locale';
4
- import { getLocaleLanguage } from './utils/language';
5
- import { start } from './commands/start';
6
- import { dev } from './commands/dev';
7
-
8
- export { defineConfig };
9
-
10
- // eslint-disable-next-line react-hooks/rules-of-hooks
11
- usePlugins([
12
- require.resolve('@modern-js/plugin-analyze/cli'),
13
- require.resolve('@modern-js/plugin-fast-refresh/cli'),
14
- ]);
15
-
16
- export default createPlugin(
17
- (() => {
18
- const locale = getLocaleLanguage();
19
- i18n.changeLanguage({ locale });
20
-
21
- lifecycle();
22
-
23
- return {
24
- commands({ program }: any) {
25
- program
26
- .command('dev')
27
- .usage('[options]')
28
- .description(i18n.t(localeKeys.command.dev.describe))
29
- .option('-c --config <config>', i18n.t(localeKeys.command.dev.config))
30
- .action(async () => {
31
- await dev();
32
- });
33
-
34
- program
35
- .command('build')
36
- .usage('[options]')
37
- .description(i18n.t(localeKeys.command.build.describe))
38
- .option('--analyze', i18n.t(localeKeys.command.build.analyze))
39
- .action(async (options: any) => {
40
- const { build } = await import('./commands/build');
41
- await build(options);
42
- // force exit after build.
43
- // eslint-disable-next-line no-process-exit
44
- process.exit(0);
45
- });
46
-
47
- program
48
- .command('start')
49
- .usage('[options]')
50
- .description(i18n.t(localeKeys.command.start.describe))
51
- .action(async () => {
52
- await start();
53
- });
54
-
55
- program
56
- .command('deploy')
57
- .usage('[options]')
58
- .description(i18n.t(localeKeys.command.deploy.describe))
59
- .action(async (options: any) => {
60
- const { build } = await import('./commands/build');
61
- await build();
62
- const { deploy } = await import('./commands/deploy');
63
- await deploy(options);
64
- // eslint-disable-next-line no-process-exit
65
- process.exit(0);
66
- });
67
-
68
- program
69
- .command('new')
70
- .usage('[options]')
71
- .description(i18n.t(localeKeys.command.new.describe))
72
- .option('-d, --debug', i18n.t(localeKeys.command.new.debug), false)
73
- .option(
74
- '-c, --config <config>',
75
- i18n.t(localeKeys.command.new.config),
76
- )
77
- .option('--dist-tag <tag>', i18n.t(localeKeys.command.new.distTag))
78
- .option('--registry', i18n.t(localeKeys.command.new.registry))
79
- .action(async (options: any) => {
80
- const { MWANewAction } = await import('@modern-js/new-action');
81
- await MWANewAction({ ...options, locale });
82
- });
83
- },
84
- async fileChange() {
85
- await cli.restart();
86
- },
87
- };
88
- }) as any,
89
- {
90
- post: [
91
- '@modern-js/plugin-analyze',
92
- '@modern-js/plugin-fast-refresh',
93
- '@modern-js/plugin-ssr',
94
- '@modern-js/plugin-state',
95
- '@modern-js/plugin-router',
96
- '@modern-js/plugin-polyfill',
97
- ],
98
- },
99
- );
package/src/lifecycle.ts DELETED
@@ -1,43 +0,0 @@
1
- import { createAsyncWaterfall, createAsyncWorkflow } from '@modern-js/plugin';
2
- import { registerHook } from '@modern-js/core';
3
-
4
- import type { Compiler, Configuration, MultiCompiler } from '@modern-js/types';
5
-
6
- export const beforeDev = createAsyncWorkflow();
7
-
8
- export const afterDev = createAsyncWorkflow();
9
-
10
- export const beforeCreateCompiler = createAsyncWorkflow<{
11
- webpackConfigs: Configuration[];
12
- }>();
13
-
14
- export const afterCreateCompiler = createAsyncWorkflow<{
15
- compiler: Compiler | MultiCompiler | undefined;
16
- }>();
17
-
18
- export const beforePrintInstructions =
19
- createAsyncWaterfall<{ instructions: string }>();
20
-
21
- export const beforeBuild = createAsyncWorkflow<{
22
- webpackConfigs: Configuration[];
23
- }>();
24
-
25
- export const afterBuild = createAsyncWorkflow();
26
-
27
- export const beforeDeploy = createAsyncWorkflow<Record<string, any>>();
28
-
29
- export const afterDeploy = createAsyncWorkflow<Record<string, any>>();
30
-
31
- export const lifecycle = () => {
32
- registerHook({
33
- beforeDev,
34
- afterDev,
35
- beforeCreateCompiler,
36
- afterCreateCompiler,
37
- beforePrintInstructions,
38
- beforeBuild,
39
- afterBuild,
40
- beforeDeploy,
41
- afterDeploy,
42
- });
43
- };
package/src/locale/en.ts DELETED
@@ -1,18 +0,0 @@
1
- export const EN_LOCALE = {
2
- command: {
3
- dev: {
4
- describe: 'start dev server',
5
- config: 'specify config file',
6
- },
7
- build: { describe: 'build application', analyze: 'analyze bundle' },
8
- start: { describe: 'start server' },
9
- deploy: { describe: 'deploy application' },
10
- new: {
11
- describe: 'generator runner for MWA project',
12
- debug: 'using debug mode to log something',
13
- config: 'set default generator config(json string)',
14
- distTag: `use specified tag version for it's generator`,
15
- registry: 'set npm registry url to run npm command',
16
- },
17
- },
18
- };
@@ -1,9 +0,0 @@
1
- import { I18n } from '@modern-js/plugin-i18n';
2
- import { ZH_LOCALE } from './zh';
3
- import { EN_LOCALE } from './en';
4
-
5
- const i18n = new I18n();
6
-
7
- const localeKeys = i18n.init('zh', { zh: ZH_LOCALE, en: EN_LOCALE });
8
-
9
- export { i18n, localeKeys };
package/src/locale/zh.ts DELETED
@@ -1,18 +0,0 @@
1
- export const ZH_LOCALE = {
2
- command: {
3
- dev: {
4
- describe: '本地开发命令',
5
- config: '制定配置文件路径',
6
- },
7
- build: { describe: '构建应用命令', analyze: '分析构建产物' },
8
- start: { describe: '应用启动命令' },
9
- deploy: { describe: '部署应用命令' },
10
- new: {
11
- describe: 'MWA 项目中中执行生成器',
12
- debug: '开启 Debug 模式,打印调试日志信息',
13
- config: '生成器运行默认配置(JSON 字符串)',
14
- distTag: '生成器使用特殊的 npm Tag 版本',
15
- registry: '生成器运行过程中定制 npm Registry',
16
- },
17
- },
18
- };
@@ -1,81 +0,0 @@
1
- import webpack, { Configuration, StatsCompilation } from 'webpack';
2
- import { IAppContext, NormalizedConfig, mountHook } from '@modern-js/core';
3
- import {
4
- chalk,
5
- logger,
6
- formatWebpackMessages,
7
- clearConsole,
8
- } from '@modern-js/utils';
9
- import { printInstructions } from './printInstructions';
10
-
11
- const prettyTime = (stats: StatsCompilation) =>
12
- Math.max(...(stats.children?.map(child => child.time || 0) || []));
13
-
14
- export const createCompiler = async ({
15
- webpackConfigs,
16
- // TODO: params
17
- userConfig,
18
- appContext,
19
- }: {
20
- webpackConfigs: Configuration[];
21
- userConfig: NormalizedConfig;
22
- appContext: IAppContext;
23
- }) => {
24
- try {
25
- await (mountHook() as any).beforeCreateCompiler({ webpackConfigs });
26
- const compiler = webpack(webpackConfigs);
27
-
28
- await (mountHook() as any).afterCreateCompiler({ compiler });
29
-
30
- let isFirstCompile = true;
31
-
32
- compiler.hooks.invalid.tap('invalid', () => {
33
- clearConsole();
34
- logger.log('Compiling...');
35
- });
36
-
37
- compiler.hooks.done.tap('done', async (stats: any) => {
38
- clearConsole();
39
-
40
- const statsData = stats.toJson({
41
- all: false,
42
- warnings: true,
43
- errors: true,
44
- timings: true,
45
- });
46
-
47
- const { errors, warnings } = formatWebpackMessages(statsData);
48
-
49
- if (errors.length) {
50
- logger.log(chalk.red(`Failed to compile.\n`));
51
- logger.log(errors.join('\n\n'));
52
- logger.log();
53
- } else if (process.stdout.isTTY || isFirstCompile) {
54
- await (mountHook() as any).afterDev();
55
- if (warnings.length) {
56
- logger.log(chalk.yellow(`Compiled with warnings.\n`));
57
- logger.log(warnings.join('\n\n'));
58
- logger.log();
59
- } else {
60
- logger.log(
61
- chalk.green(
62
- `Compiled successfully in ${prettyTime(statsData)} ms.\n`,
63
- ),
64
- );
65
- }
66
- await printInstructions(appContext, userConfig);
67
- }
68
- // eslint-disable-next-line require-atomic-updates
69
- isFirstCompile = false;
70
- });
71
-
72
- return compiler;
73
- } catch (err) {
74
- logger.log(chalk.red(`Failed to compile.`));
75
- logger.log();
76
- logger.log(err as any);
77
- // FIXME: 这里最好抛出异常,执行 process.exit 的地方尽可能少或者控制在几个统一的地方比较合适
78
- // eslint-disable-next-line no-process-exit
79
- process.exit(1);
80
- }
81
- };
@@ -1,16 +0,0 @@
1
- import { Server, ModernServerOptions } from '@modern-js/server';
2
-
3
- let server: Server;
4
-
5
- export const createServer = async (options: ModernServerOptions) => {
6
- if (server) {
7
- await server.close();
8
- }
9
-
10
- // eslint-disable-next-line require-atomic-updates
11
- server = new Server(options);
12
-
13
- const app = await server.init();
14
-
15
- return app;
16
- };
@@ -1,6 +0,0 @@
1
- import { I18CLILanguageDetector } from '@modern-js/i18n-cli-language-detector';
2
-
3
- export function getLocaleLanguage() {
4
- const detector = new I18CLILanguageDetector();
5
- return detector.detect();
6
- }
@@ -1,25 +0,0 @@
1
- import { prettyInstructions, logger, isDev, chalk } from '@modern-js/utils';
2
- import { mountHook, IAppContext, NormalizedConfig } from '@modern-js/core';
3
-
4
- export const printInstructions = async (
5
- appContext: IAppContext,
6
- config: NormalizedConfig,
7
- ) => {
8
- let message = prettyInstructions(appContext, config);
9
-
10
- if (isDev()) {
11
- message += `\n${chalk.cyanBright(
12
- [
13
- `Note that the development build is not optimized.`,
14
- `To create a production build, execute build command.`,
15
- ].join('\n'),
16
- )}`;
17
- }
18
-
19
- // call beforePrintInstructions hook.
20
- const { instructions } = await (mountHook() as any).beforePrintInstructions({
21
- instructions: message,
22
- });
23
-
24
- logger.log(instructions);
25
- };