@modern-js/app-tools 1.4.2 → 1.4.5

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.
Files changed (36) hide show
  1. package/.eslintrc.js +8 -0
  2. package/CHANGELOG.md +84 -0
  3. package/dist/js/modern/commands/build.js +24 -19
  4. package/dist/js/modern/commands/deploy.js +4 -4
  5. package/dist/js/modern/commands/dev.js +12 -11
  6. package/dist/js/modern/commands/start.js +5 -9
  7. package/dist/js/modern/{lifecycle.js → hooks.js} +10 -13
  8. package/dist/js/modern/index.js +74 -73
  9. package/dist/js/modern/locale/zh.js +1 -1
  10. package/dist/js/modern/utils/createCompiler.js +6 -5
  11. package/dist/js/modern/utils/printInstructions.js +3 -3
  12. package/dist/js/node/commands/build.js +19 -17
  13. package/dist/js/node/commands/deploy.js +4 -5
  14. package/dist/js/node/commands/dev.js +17 -16
  15. package/dist/js/node/commands/start.js +6 -11
  16. package/dist/js/node/{lifecycle.js → hooks.js} +12 -18
  17. package/dist/js/node/index.js +81 -77
  18. package/dist/js/node/locale/zh.js +1 -1
  19. package/dist/js/node/utils/createCompiler.js +6 -6
  20. package/dist/js/node/utils/printInstructions.js +3 -4
  21. package/dist/types/commands/build.d.ts +3 -5
  22. package/dist/types/commands/deploy.d.ts +2 -1
  23. package/dist/types/commands/dev.d.ts +2 -1
  24. package/dist/types/commands/start.d.ts +2 -1
  25. package/dist/types/{lifecycle.d.ts → hooks.d.ts} +19 -1
  26. package/dist/types/index.d.ts +2 -19
  27. package/dist/types/utils/createCompiler.d.ts +3 -1
  28. package/dist/types/utils/createServer.d.ts +2 -2
  29. package/dist/types/utils/printInstructions.d.ts +2 -2
  30. package/dist/types/utils/routes.d.ts +1 -1
  31. package/dist/types/utils/types.d.ts +4 -1
  32. package/lib/types.d.ts +92 -0
  33. package/modern.config.js +1 -0
  34. package/package.json +12 -18
  35. package/tests/.eslintrc.js +8 -0
  36. package/tests/commands/build.test.ts +25 -20
package/.eslintrc.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@modern-js'],
4
+ parserOptions: {
5
+ tsconfigRootDir: __dirname,
6
+ project: ['./tsconfig.json'],
7
+ },
8
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,89 @@
1
1
  # @modern-js/app-tools
2
2
 
3
+ ## 1.4.5
4
+
5
+ ### Patch Changes
6
+
7
+ - 32c9772c: feat: convert to new plugin
8
+ - Updated dependencies [05ce88a0]
9
+ - Updated dependencies [a8df060e]
10
+ - Updated dependencies [c2046f37]
11
+ - Updated dependencies [dc88abf9]
12
+ - Updated dependencies [d2d0fa11]
13
+ - Updated dependencies [66cbef42]
14
+ - Updated dependencies [0462ff77]
15
+ - Updated dependencies [61e3f623]
16
+ - Updated dependencies [6a7acb81]
17
+ - Updated dependencies [681a1ff9]
18
+ - Updated dependencies [4e2026e4]
19
+ - @modern-js/core@1.6.0
20
+ - @modern-js/server@1.4.8
21
+ - @modern-js/utils@1.3.6
22
+ - @modern-js/plugin@1.3.2
23
+ - @modern-js/prod-server@1.0.5
24
+ - @modern-js/webpack@1.5.0
25
+ - @modern-js/plugin-fast-refresh@1.2.2
26
+ - @modern-js/plugin-analyze@1.3.4
27
+
28
+ ## 1.4.4
29
+
30
+ ### Patch Changes
31
+
32
+ - d5bf095a: fix: disable load webpack when apiOnly mode
33
+ - Updated dependencies [5bf5868d]
34
+ - Updated dependencies [80d3cfb7]
35
+ - Updated dependencies [42c6b136]
36
+ - Updated dependencies [80d8ddfe]
37
+ - Updated dependencies [4e7dcbd5]
38
+ - Updated dependencies [d95f28c3]
39
+ - Updated dependencies [d95f28c3]
40
+ - Updated dependencies [d5bf095a]
41
+ - Updated dependencies [9e8bc4ab]
42
+ - Updated dependencies [0923c182]
43
+ - Updated dependencies [0c556e59]
44
+ - Updated dependencies [80d8ddfe]
45
+ - Updated dependencies [491145e3]
46
+ - Updated dependencies [2008fdbd]
47
+ - Updated dependencies [ca0bcf13]
48
+ - Updated dependencies [9229dfd1]
49
+ - @modern-js/utils@1.3.5
50
+ - @modern-js/core@1.5.0
51
+ - @modern-js/plugin@1.3.0
52
+ - @modern-js/prod-server@1.0.4
53
+ - @modern-js/types@1.3.5
54
+ - @modern-js/server@1.4.7
55
+
56
+ ## 1.4.3
57
+
58
+ ### Patch Changes
59
+
60
+ - 4b5d4bf4: feat: add img resource's inline and url type declaration
61
+ - bdcf0865: fix: empty distDirectory for api service
62
+ - 02fb4146: support product server
63
+ - edc3bd3d: fix: @modern-js/core package not found
64
+ - Updated dependencies [969f172f]
65
+ - Updated dependencies [0ad75faa]
66
+ - Updated dependencies [4c792f68]
67
+ - Updated dependencies [83059b93]
68
+ - Updated dependencies [4b5d4bf4]
69
+ - Updated dependencies [0ad75faa]
70
+ - Updated dependencies [62f5b8c8]
71
+ - Updated dependencies [55e18278]
72
+ - Updated dependencies [4499a674]
73
+ - Updated dependencies [0ad75faa]
74
+ - Updated dependencies [02fb4146]
75
+ - Updated dependencies [403f5169]
76
+ - Updated dependencies [e37ea5b2]
77
+ - Updated dependencies [a7f42f48]
78
+ - Updated dependencies [83059b93]
79
+ - @modern-js/core@1.4.4
80
+ - @modern-js/webpack@1.4.1
81
+ - @modern-js/utils@1.3.3
82
+ - @modern-js/new-action@1.3.3
83
+ - @modern-js/server@1.4.4
84
+ - @modern-js/types@1.3.4
85
+ - @modern-js/prod-server@1.0.1
86
+
3
87
  ## 1.4.2
4
88
 
5
89
  ### Patch Changes
@@ -6,28 +6,31 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
6
6
 
7
7
  import { webpack } from 'webpack';
8
8
  import { WebpackConfigTarget, getWebpackConfig } from '@modern-js/webpack';
9
- import { useAppContext, useResolvedConfigContext, mountHook, ResolvedConfigContext, manager } from '@modern-js/core';
10
- import { fs, formatWebpackMessages, measureFileSizesBeforeBuild, printFileSizesAfterBuild, printBuildError, logger, isUseSSRBundle } from '@modern-js/utils';
11
- import { generateRoutes } from "../utils/routes"; // These sizes are pretty large. We'll warn for bundles exceeding them.
12
-
9
+ import { manager, ResolvedConfigContext } from '@modern-js/core';
10
+ import { formatWebpackMessages, measureFileSizesBeforeBuild, printFileSizesAfterBuild, printBuildError, logger, isUseSSRBundle, emptyDir } from '@modern-js/utils';
11
+ import { generateRoutes } from "../utils/routes";
12
+ // These sizes are pretty large. We'll warn for bundles exceeding them.
13
13
  const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
14
- const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
15
- // eslint-disable-next-line max-statements
16
- export const build = async options => {
17
- /* eslint-disable react-hooks/rules-of-hooks */
18
- const resolvedConfig = useResolvedConfigContext();
19
- const appContext = useAppContext();
14
+ const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; // eslint-disable-next-line max-statements
15
+
16
+ export const build = async (api, options) => {
17
+ const resolvedConfig = api.useResolvedConfigContext();
18
+ const appContext = api.useAppContext();
19
+ const hookRunners = api.useHookRunners();
20
20
  const {
21
21
  existSrc
22
22
  } = appContext;
23
- /* eslint-enable react-hooks/rules-of-hooks */
24
23
 
25
24
  if (!existSrc) {
26
- await mountHook().beforeBuild({
25
+ const {
26
+ distDirectory
27
+ } = appContext;
28
+ await emptyDir(distDirectory);
29
+ await hookRunners.beforeBuild({
27
30
  webpackConfigs: []
28
31
  });
29
32
  await generateRoutes(appContext);
30
- await mountHook().afterBuild();
33
+ await hookRunners.afterBuild();
31
34
  return;
32
35
  }
33
36
 
@@ -53,7 +56,7 @@ export const build = async options => {
53
56
 
54
57
  if (messages.errors.length === 0) {
55
58
  logger.info(`File sizes after ${label} build:\n`);
56
- printFileSizesAfterBuild(stats, previousFileSizes, outputPath, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
59
+ printFileSizesAfterBuild(stats, previousFileSizes, distDirectory, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
57
60
  logger.log();
58
61
  }
59
62
  } // When using run or watch, call close and wait for it to finish before calling run or watch again.
@@ -87,9 +90,11 @@ export const build = async options => {
87
90
  cliOptions: options
88
91
  }));
89
92
  });
90
- const outputPath = appContext.distDirectory;
91
- const previousFileSizes = await measureFileSizesBeforeBuild(outputPath);
92
- fs.emptyDirSync(outputPath);
93
+ const {
94
+ distDirectory
95
+ } = appContext;
96
+ const previousFileSizes = await measureFileSizesBeforeBuild(distDirectory);
97
+ await emptyDir(distDirectory);
93
98
  const buildConfigs = [];
94
99
  buildConfigs.push({
95
100
  type: 'legacy',
@@ -110,7 +115,7 @@ export const build = async options => {
110
115
  });
111
116
  }
112
117
 
113
- await mountHook().beforeBuild({
118
+ await hookRunners.beforeBuild({
114
119
  webpackConfigs: buildConfigs.map(({
115
120
  config
116
121
  }) => config)
@@ -132,5 +137,5 @@ export const build = async options => {
132
137
  }
133
138
 
134
139
  await generateRoutes(appContext);
135
- await mountHook().afterBuild();
140
+ await hookRunners.afterBuild();
136
141
  };
@@ -1,5 +1,5 @@
1
- import { mountHook } from '@modern-js/core';
2
- export const deploy = async options => {
3
- await mountHook().beforeDeploy(options);
4
- await mountHook().afterDeploy(options);
1
+ export const deploy = async (api, options) => {
2
+ const hookRunners = api.useHookRunners();
3
+ await hookRunners.beforeDeploy(options);
4
+ await hookRunners.afterDeploy(options);
5
5
  };
@@ -4,20 +4,16 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
4
4
 
5
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
6
 
7
- import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
8
7
  import { fs, logger, HMR_SOCK_PATH, clearConsole, chalk, isSSR } from '@modern-js/utils';
9
- import { useAppContext, useResolvedConfigContext, mountHook, AppContext } from '@modern-js/core';
10
8
  import { createCompiler } from "../utils/createCompiler";
11
9
  import { createServer } from "../utils/createServer";
12
10
  import { generateRoutes } from "../utils/routes";
13
11
  import { printInstructions } from "../utils/printInstructions";
14
12
  import { getSpecifiedEntries } from "../utils/getSpecifiedEntries";
15
- export const dev = async options => {
16
- /* eslint-disable react-hooks/rules-of-hooks */
17
- const appContext = useAppContext();
18
- const userConfig = useResolvedConfigContext();
19
- /* eslint-enable react-hooks/rules-of-hooks */
20
-
13
+ export const dev = async (api, options) => {
14
+ const appContext = api.useAppContext();
15
+ const userConfig = api.useResolvedConfigContext();
16
+ const hookRunners = api.useHookRunners();
21
17
  const {
22
18
  appDirectory,
23
19
  distDirectory,
@@ -26,17 +22,22 @@ export const dev = async options => {
26
22
  entrypoints
27
23
  } = appContext;
28
24
  const checkedEntries = await getSpecifiedEntries(options.entry || false, entrypoints);
29
- AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
25
+ api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
30
26
  checkedEntries
31
27
  }));
32
28
  appContext.checkedEntries = checkedEntries;
33
29
  fs.emptyDirSync(distDirectory);
34
- await mountHook().beforeDev();
30
+ await hookRunners.beforeDev();
35
31
  let compiler = null;
36
32
 
37
33
  if (existSrc) {
34
+ const {
35
+ getWebpackConfig,
36
+ WebpackConfigTarget
37
+ } = await import('@modern-js/webpack');
38
38
  const webpackConfigs = [isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE), getWebpackConfig(WebpackConfigTarget.CLIENT)].filter(Boolean);
39
39
  compiler = await createCompiler({
40
+ api,
40
41
  webpackConfigs,
41
42
  userConfig,
42
43
  appContext
@@ -75,7 +76,7 @@ export const dev = async options => {
75
76
  clearConsole();
76
77
  logger.log(chalk.cyan(`Starting the development server...`));
77
78
  } else {
78
- await printInstructions(appContext, userConfig);
79
+ await printInstructions(api, appContext, userConfig);
79
80
  }
80
81
  });
81
82
  };
@@ -1,13 +1,9 @@
1
1
  import { logger, chalk } from '@modern-js/utils';
2
- import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
3
- import server from '@modern-js/server';
2
+ import server from '@modern-js/prod-server';
4
3
  import { printInstructions } from "../utils/printInstructions";
5
- export const start = async () => {
6
- /* eslint-disable react-hooks/rules-of-hooks */
7
- const appContext = useAppContext();
8
- const userConfig = useResolvedConfigContext();
9
- /* eslint-enable react-hooks/rules-of-hooks */
10
-
4
+ export const start = async api => {
5
+ const appContext = api.useAppContext();
6
+ const userConfig = api.useResolvedConfigContext();
11
7
  const {
12
8
  appDirectory,
13
9
  port
@@ -23,6 +19,6 @@ export const start = async () => {
23
19
  throw err;
24
20
  }
25
21
 
26
- await printInstructions(appContext, userConfig);
22
+ await printInstructions(api, appContext, userConfig);
27
23
  });
28
24
  };
@@ -1,5 +1,4 @@
1
1
  import { createAsyncWaterfall, createAsyncWorkflow } from '@modern-js/plugin';
2
- import { registerHook } from '@modern-js/core';
3
2
  export const beforeDev = createAsyncWorkflow();
4
3
  export const afterDev = createAsyncWorkflow();
5
4
  export const beforeCreateCompiler = createAsyncWorkflow();
@@ -9,16 +8,14 @@ export const beforeBuild = createAsyncWorkflow();
9
8
  export const afterBuild = createAsyncWorkflow();
10
9
  export const beforeDeploy = createAsyncWorkflow();
11
10
  export const afterDeploy = createAsyncWorkflow();
12
- export const lifecycle = () => {
13
- registerHook({
14
- beforeDev,
15
- afterDev,
16
- beforeCreateCompiler,
17
- afterCreateCompiler,
18
- beforePrintInstructions,
19
- beforeBuild,
20
- afterBuild,
21
- beforeDeploy,
22
- afterDeploy
23
- });
11
+ export const hooks = {
12
+ beforeDev,
13
+ afterDev,
14
+ beforeCreateCompiler,
15
+ afterCreateCompiler,
16
+ beforePrintInstructions,
17
+ beforeBuild,
18
+ afterBuild,
19
+ beforeDeploy,
20
+ afterDeploy
24
21
  };
@@ -5,90 +5,91 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
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
6
 
7
7
  import * as path from 'path';
8
- import { createPlugin, defineConfig, usePlugins, cli, useAppContext } from '@modern-js/core';
8
+ import { defineConfig, cli } from '@modern-js/core';
9
+ import AnalyzePlugin from '@modern-js/plugin-analyze';
10
+ import FastRefreshPlugin from '@modern-js/plugin-fast-refresh/cli';
9
11
  import { cleanRequireCache } from '@modern-js/utils';
10
- import { lifecycle } from "./lifecycle";
12
+ import { hooks } from "./hooks";
11
13
  import { i18n, localeKeys } from "./locale";
12
14
  import { getLocaleLanguage } from "./utils/language";
13
15
  import { start } from "./commands/start";
14
16
  import { dev } from "./commands/dev";
15
17
  import { closeServer } from "./utils/createServer";
16
- export { defineConfig }; // eslint-disable-next-line react-hooks/rules-of-hooks
18
+ export { defineConfig };
19
+ export default (() => ({
20
+ name: '@modern-js/app-tools',
21
+ 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'],
22
+ registerHook: hooks,
23
+ usePlugins: [AnalyzePlugin(), FastRefreshPlugin()],
24
+ setup: api => {
25
+ const locale = getLocaleLanguage();
26
+ i18n.changeLanguage({
27
+ locale
28
+ });
29
+ return {
30
+ commands({
31
+ program
32
+ }) {
33
+ program.command('dev').usage('[options]').description(i18n.t(localeKeys.command.dev.describe)).option('-c --config <config>', i18n.t(localeKeys.command.dev.config)).option('-e --entry [entry...]', i18n.t(localeKeys.command.dev.entry)).action(async options => {
34
+ await dev(api, options);
35
+ });
36
+ program.command('build').usage('[options]').description(i18n.t(localeKeys.command.build.describe)).option('--analyze', i18n.t(localeKeys.command.build.analyze)).action(async options => {
37
+ const {
38
+ build
39
+ } = await import("./commands/build");
40
+ await build(api, options); // force exit after build.
41
+ // eslint-disable-next-line no-process-exit
17
42
 
18
- usePlugins([require.resolve('@modern-js/plugin-analyze/cli'), require.resolve('@modern-js/plugin-fast-refresh/cli')]);
19
- export default createPlugin(() => {
20
- const locale = getLocaleLanguage();
21
- i18n.changeLanguage({
22
- locale
23
- });
24
- lifecycle();
25
- return {
26
- commands({
27
- program
28
- }) {
29
- program.command('dev').usage('[options]').description(i18n.t(localeKeys.command.dev.describe)).option('-c --config <config>', i18n.t(localeKeys.command.dev.config)).option('-e --entry [entry...]', i18n.t(localeKeys.command.dev.entry)).action(async options => {
30
- await dev(options);
31
- });
32
- program.command('build').usage('[options]').description(i18n.t(localeKeys.command.build.describe)).option('--analyze', i18n.t(localeKeys.command.build.analyze)).action(async options => {
33
- const {
34
- build
35
- } = await import("./commands/build");
36
- await build(options); // force exit after build.
37
- // eslint-disable-next-line no-process-exit
43
+ process.exit(0);
44
+ });
45
+ program.command('start').usage('[options]').description(i18n.t(localeKeys.command.start.describe)).action(async () => {
46
+ await start(api);
47
+ });
48
+ program.command('deploy').usage('[options]').description(i18n.t(localeKeys.command.deploy.describe)).action(async options => {
49
+ const {
50
+ build
51
+ } = await import("./commands/build");
52
+ await build(api);
53
+ const {
54
+ deploy
55
+ } = await import("./commands/deploy");
56
+ await deploy(api, options); // eslint-disable-next-line no-process-exit
38
57
 
39
- process.exit(0);
40
- });
41
- program.command('start').usage('[options]').description(i18n.t(localeKeys.command.start.describe)).action(async () => {
42
- await start();
43
- });
44
- program.command('deploy').usage('[options]').description(i18n.t(localeKeys.command.deploy.describe)).action(async options => {
45
- const {
46
- build
47
- } = await import("./commands/build");
48
- await build();
49
- const {
50
- deploy
51
- } = await import("./commands/deploy");
52
- await deploy(options); // eslint-disable-next-line no-process-exit
58
+ process.exit(0);
59
+ });
60
+ 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 => {
61
+ const {
62
+ MWANewAction
63
+ } = await import('@modern-js/new-action');
64
+ await MWANewAction(_objectSpread(_objectSpread({}, options), {}, {
65
+ locale
66
+ }));
67
+ });
68
+ },
53
69
 
54
- process.exit(0);
55
- });
56
- 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 => {
70
+ // 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
71
+ async fileChange(e) {
57
72
  const {
58
- MWANewAction
59
- } = await import('@modern-js/new-action');
60
- await MWANewAction(_objectSpread(_objectSpread({}, options), {}, {
61
- locale
62
- }));
63
- });
64
- },
65
-
66
- // 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
67
- async fileChange(e) {
68
- const {
69
- filename,
70
- eventType
71
- } = e; // eslint-disable-next-line react-hooks/rules-of-hooks
73
+ filename,
74
+ eventType
75
+ } = e;
76
+ const appContext = api.useAppContext();
77
+ const {
78
+ appDirectory,
79
+ srcDirectory
80
+ } = appContext;
81
+ const absolutePath = path.resolve(appDirectory, filename);
72
82
 
73
- const appContext = useAppContext();
74
- const {
75
- appDirectory,
76
- srcDirectory
77
- } = appContext;
78
- const absolutePath = path.resolve(appDirectory, filename);
83
+ if (!absolutePath.includes(srcDirectory) && (eventType === 'change' || eventType === 'unlink')) {
84
+ await closeServer();
85
+ await cli.restart();
86
+ }
87
+ },
79
88
 
80
- if (!absolutePath.includes(srcDirectory) && (eventType === 'change' || eventType === 'unlink')) {
81
- await closeServer();
82
- await cli.restart();
89
+ async beforeRestart() {
90
+ cleanRequireCache([require.resolve('@modern-js/plugin-analyze/cli'), require.resolve('@modern-js/plugin-fast-refresh/cli')]);
83
91
  }
84
- },
85
92
 
86
- async beforeRestart() {
87
- cleanRequireCache([require.resolve('@modern-js/plugin-analyze/cli'), require.resolve('@modern-js/plugin-fast-refresh/cli')]);
88
- }
89
-
90
- };
91
- }, {
92
- name: '@modern-js/app-tools',
93
- 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']
94
- });
93
+ };
94
+ }
95
+ }));
@@ -7,7 +7,7 @@ export const ZH_LOCALE = {
7
7
  },
8
8
  build: {
9
9
  describe: '构建应用命令',
10
- analyze: '分析构建产物'
10
+ analyze: '分析构建产物体积,查看各个模块打包后的大小'
11
11
  },
12
12
  start: {
13
13
  describe: '应用启动命令'
@@ -1,5 +1,4 @@
1
1
  import webpack from 'webpack';
2
- import { mountHook } from '@modern-js/core';
3
2
  import { chalk, logger, formatWebpackMessages, clearConsole } from '@modern-js/utils';
4
3
  import { printInstructions } from "./printInstructions";
5
4
 
@@ -10,17 +9,19 @@ const prettyTime = stats => {
10
9
  };
11
10
 
12
11
  export const createCompiler = async ({
12
+ api,
13
13
  webpackConfigs,
14
14
  // TODO: params
15
15
  userConfig,
16
16
  appContext
17
17
  }) => {
18
18
  try {
19
- await mountHook().beforeCreateCompiler({
19
+ const hookRunners = api.useHookRunners();
20
+ await hookRunners.beforeCreateCompiler({
20
21
  webpackConfigs
21
22
  });
22
23
  const compiler = webpack(webpackConfigs);
23
- await mountHook().afterCreateCompiler({
24
+ await hookRunners.afterCreateCompiler({
24
25
  compiler
25
26
  });
26
27
  let isFirstCompile = true;
@@ -46,7 +47,7 @@ export const createCompiler = async ({
46
47
  logger.log(errors.join('\n\n'));
47
48
  logger.log();
48
49
  } else if (process.stdout.isTTY || isFirstCompile) {
49
- await mountHook().afterDev();
50
+ await hookRunners.afterDev();
50
51
 
51
52
  if (warnings.length) {
52
53
  logger.log(chalk.yellow(`Compiled with warnings.\n`));
@@ -56,7 +57,7 @@ export const createCompiler = async ({
56
57
  logger.log(chalk.green(`Compiled successfully in ${prettyTime(statsData)} ms.\n`));
57
58
  }
58
59
 
59
- await printInstructions(appContext, userConfig);
60
+ await printInstructions(api, appContext, userConfig);
60
61
  } // eslint-disable-next-line require-atomic-updates
61
62
 
62
63
 
@@ -1,6 +1,5 @@
1
1
  import { prettyInstructions, logger, isDev, chalk } from '@modern-js/utils';
2
- import { mountHook } from '@modern-js/core';
3
- export const printInstructions = async (appContext, config) => {
2
+ export const printInstructions = async (api, appContext, config) => {
4
3
  let message = prettyInstructions(appContext, config);
5
4
  const {
6
5
  existSrc
@@ -11,9 +10,10 @@ export const printInstructions = async (appContext, config) => {
11
10
  } // call beforePrintInstructions hook.
12
11
 
13
12
 
13
+ const hookRunners = api.useHookRunners();
14
14
  const {
15
15
  instructions
16
- } = await mountHook().beforePrintInstructions({
16
+ } = await hookRunners.beforePrintInstructions({
17
17
  instructions: message
18
18
  });
19
19
  logger.log(instructions);
@@ -23,24 +23,26 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
23
23
 
24
24
  // These sizes are pretty large. We'll warn for bundles exceeding them.
25
25
  const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
26
- const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024;
26
+ const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; // eslint-disable-next-line max-statements
27
27
 
28
- // eslint-disable-next-line max-statements
29
- const build = async options => {
30
- /* eslint-disable react-hooks/rules-of-hooks */
31
- const resolvedConfig = (0, _core.useResolvedConfigContext)();
32
- const appContext = (0, _core.useAppContext)();
28
+ const build = async (api, options) => {
29
+ const resolvedConfig = api.useResolvedConfigContext();
30
+ const appContext = api.useAppContext();
31
+ const hookRunners = api.useHookRunners();
33
32
  const {
34
33
  existSrc
35
34
  } = appContext;
36
- /* eslint-enable react-hooks/rules-of-hooks */
37
35
 
38
36
  if (!existSrc) {
39
- await (0, _core.mountHook)().beforeBuild({
37
+ const {
38
+ distDirectory
39
+ } = appContext;
40
+ await (0, _utils.emptyDir)(distDirectory);
41
+ await hookRunners.beforeBuild({
40
42
  webpackConfigs: []
41
43
  });
42
44
  await (0, _routes.generateRoutes)(appContext);
43
- await (0, _core.mountHook)().afterBuild();
45
+ await hookRunners.afterBuild();
44
46
  return;
45
47
  }
46
48
 
@@ -68,7 +70,7 @@ const build = async options => {
68
70
  if (messages.errors.length === 0) {
69
71
  _utils.logger.info(`File sizes after ${label} build:\n`);
70
72
 
71
- (0, _utils.printFileSizesAfterBuild)(stats, previousFileSizes, outputPath, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
73
+ (0, _utils.printFileSizesAfterBuild)(stats, previousFileSizes, distDirectory, WARN_AFTER_BUNDLE_GZIP_SIZE, WARN_AFTER_CHUNK_GZIP_SIZE);
72
74
 
73
75
  _utils.logger.log();
74
76
  }
@@ -104,11 +106,11 @@ const build = async options => {
104
106
  }));
105
107
  });
106
108
 
107
- const outputPath = appContext.distDirectory;
108
- const previousFileSizes = await (0, _utils.measureFileSizesBeforeBuild)(outputPath);
109
-
110
- _utils.fs.emptyDirSync(outputPath);
111
-
109
+ const {
110
+ distDirectory
111
+ } = appContext;
112
+ const previousFileSizes = await (0, _utils.measureFileSizesBeforeBuild)(distDirectory);
113
+ await (0, _utils.emptyDir)(distDirectory);
112
114
  const buildConfigs = [];
113
115
  buildConfigs.push({
114
116
  type: 'legacy',
@@ -129,7 +131,7 @@ const build = async options => {
129
131
  });
130
132
  }
131
133
 
132
- await (0, _core.mountHook)().beforeBuild({
134
+ await hookRunners.beforeBuild({
133
135
  webpackConfigs: buildConfigs.map(({
134
136
  config
135
137
  }) => config)
@@ -151,7 +153,7 @@ const build = async options => {
151
153
  }
152
154
 
153
155
  await (0, _routes.generateRoutes)(appContext);
154
- await (0, _core.mountHook)().afterBuild();
156
+ await hookRunners.afterBuild();
155
157
  };
156
158
 
157
159
  exports.build = build;
@@ -5,11 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.deploy = void 0;
7
7
 
8
- var _core = require("@modern-js/core");
9
-
10
- const deploy = async options => {
11
- await (0, _core.mountHook)().beforeDeploy(options);
12
- await (0, _core.mountHook)().afterDeploy(options);
8
+ const deploy = async (api, options) => {
9
+ const hookRunners = api.useHookRunners();
10
+ await hookRunners.beforeDeploy(options);
11
+ await hookRunners.afterDeploy(options);
13
12
  };
14
13
 
15
14
  exports.deploy = deploy;