@modern-js/app-tools 1.4.4 → 1.4.7-canary.0

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 (42) hide show
  1. package/.eslintrc.js +8 -0
  2. package/CHANGELOG.md +47 -0
  3. package/dist/js/modern/commands/build.js +15 -11
  4. package/dist/js/modern/commands/deploy.js +4 -4
  5. package/dist/js/modern/commands/dev.js +17 -11
  6. package/dist/js/modern/commands/start.js +9 -10
  7. package/dist/js/modern/exports/server.js +1 -0
  8. package/dist/js/modern/{lifecycle.js → hooks.js} +10 -13
  9. package/dist/js/modern/index.js +74 -73
  10. package/dist/js/modern/utils/config.js +53 -0
  11. package/dist/js/modern/utils/createCompiler.js +7 -7
  12. package/dist/js/modern/utils/createServer.js +1 -2
  13. package/dist/js/modern/utils/printInstructions.js +2 -3
  14. package/dist/js/node/commands/build.js +15 -10
  15. package/dist/js/node/commands/deploy.js +4 -5
  16. package/dist/js/node/commands/dev.js +18 -14
  17. package/dist/js/node/commands/start.js +9 -11
  18. package/dist/js/node/exports/server.js +13 -0
  19. package/dist/js/node/{lifecycle.js → hooks.js} +12 -18
  20. package/dist/js/node/index.js +81 -77
  21. package/dist/js/node/utils/config.js +78 -0
  22. package/dist/js/node/utils/createCompiler.js +7 -8
  23. package/dist/js/node/utils/createServer.js +1 -2
  24. package/dist/js/node/utils/printInstructions.js +2 -4
  25. package/dist/types/commands/build.d.ts +2 -1
  26. package/dist/types/commands/deploy.d.ts +2 -1
  27. package/dist/types/commands/dev.d.ts +2 -1
  28. package/dist/types/commands/start.d.ts +2 -1
  29. package/dist/types/exports/server.d.ts +1 -0
  30. package/dist/types/{lifecycle.d.ts → hooks.d.ts} +19 -1
  31. package/dist/types/index.d.ts +2 -7
  32. package/dist/types/utils/config.d.ts +12 -0
  33. package/dist/types/utils/createCompiler.d.ts +3 -1
  34. package/dist/types/utils/printInstructions.d.ts +2 -2
  35. package/dist/types/utils/routes.d.ts +1 -1
  36. package/jest.config.js +0 -1
  37. package/modern.config.js +1 -0
  38. package/package.json +28 -22
  39. package/tests/.eslintrc.js +8 -0
  40. package/tests/__snapshots__/utils.test.ts.snap +5 -0
  41. package/tests/commands/build.test.ts +25 -24
  42. package/tests/utils.test.ts +46 -1
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,52 @@
1
1
  # @modern-js/app-tools
2
2
 
3
+ ## 1.4.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 8491b6dd: fix: optimise "types" exports from plugin
8
+ - Updated dependencies [bebb39b6]
9
+ - Updated dependencies [4b4e73b7]
10
+ - Updated dependencies [da60172c]
11
+ - Updated dependencies [ef28a4e6]
12
+ - Updated dependencies [6cff93dc]
13
+ - Updated dependencies [132f7b53]
14
+ - Updated dependencies [ff73a5cc]
15
+ - Updated dependencies [9d4a005b]
16
+ - @modern-js/plugin-analyze@1.3.5
17
+ - @modern-js/plugin-fast-refresh@1.2.3
18
+ - @modern-js/webpack@1.5.3
19
+ - @modern-js/prod-server@1.0.6
20
+ - @modern-js/server@1.4.9
21
+ - @modern-js/core@1.6.1
22
+ - @modern-js/types@1.3.6
23
+ - @modern-js/utils@1.3.7
24
+
25
+ ## 1.4.5
26
+
27
+ ### Patch Changes
28
+
29
+ - 32c9772c: feat: convert to new plugin
30
+ - Updated dependencies [05ce88a0]
31
+ - Updated dependencies [a8df060e]
32
+ - Updated dependencies [c2046f37]
33
+ - Updated dependencies [dc88abf9]
34
+ - Updated dependencies [d2d0fa11]
35
+ - Updated dependencies [66cbef42]
36
+ - Updated dependencies [0462ff77]
37
+ - Updated dependencies [61e3f623]
38
+ - Updated dependencies [6a7acb81]
39
+ - Updated dependencies [681a1ff9]
40
+ - Updated dependencies [4e2026e4]
41
+ - @modern-js/core@1.6.0
42
+ - @modern-js/server@1.4.8
43
+ - @modern-js/utils@1.3.6
44
+ - @modern-js/plugin@1.3.2
45
+ - @modern-js/prod-server@1.0.5
46
+ - @modern-js/webpack@1.5.0
47
+ - @modern-js/plugin-fast-refresh@1.2.2
48
+ - @modern-js/plugin-analyze@1.3.4
49
+
3
50
  ## 1.4.4
4
51
 
5
52
  ### Patch Changes
@@ -6,32 +6,32 @@ 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';
9
+ import { manager, ResolvedConfigContext } from '@modern-js/core';
10
10
  import { formatWebpackMessages, measureFileSizesBeforeBuild, printFileSizesAfterBuild, printBuildError, logger, isUseSSRBundle, emptyDir } from '@modern-js/utils';
11
11
  import { generateRoutes } from "../utils/routes";
12
+ import { buildServerConfig, emitResolvedConfig } from "../utils/config";
12
13
  // These sizes are pretty large. We'll warn for bundles exceeding them.
13
14
  const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
14
15
  const WARN_AFTER_CHUNK_GZIP_SIZE = 1024 * 1024; // eslint-disable-next-line max-statements
15
16
 
16
- export const build = async options => {
17
- /* eslint-disable react-hooks/rules-of-hooks */
18
- const resolvedConfig = useResolvedConfigContext();
19
- const appContext = useAppContext();
17
+ export const build = async (api, options) => {
18
+ const resolvedConfig = api.useResolvedConfigContext();
19
+ const appContext = api.useAppContext();
20
+ const hookRunners = api.useHookRunners();
20
21
  const {
21
22
  existSrc
22
23
  } = appContext;
23
- /* eslint-enable react-hooks/rules-of-hooks */
24
24
 
25
25
  if (!existSrc) {
26
26
  const {
27
27
  distDirectory
28
28
  } = appContext;
29
29
  await emptyDir(distDirectory);
30
- await mountHook().beforeBuild({
30
+ await hookRunners.beforeBuild({
31
31
  webpackConfigs: []
32
32
  });
33
33
  await generateRoutes(appContext);
34
- await mountHook().afterBuild();
34
+ await hookRunners.afterBuild();
35
35
  return;
36
36
  }
37
37
 
@@ -92,10 +92,13 @@ export const build = async options => {
92
92
  }));
93
93
  });
94
94
  const {
95
- distDirectory
95
+ distDirectory,
96
+ appDirectory,
97
+ serverConfigFile
96
98
  } = appContext;
97
99
  const previousFileSizes = await measureFileSizesBeforeBuild(distDirectory);
98
100
  await emptyDir(distDirectory);
101
+ await buildServerConfig(appDirectory, serverConfigFile);
99
102
  const buildConfigs = [];
100
103
  buildConfigs.push({
101
104
  type: 'legacy',
@@ -116,7 +119,7 @@ export const build = async options => {
116
119
  });
117
120
  }
118
121
 
119
- await mountHook().beforeBuild({
122
+ await hookRunners.beforeBuild({
120
123
  webpackConfigs: buildConfigs.map(({
121
124
  config
122
125
  }) => config)
@@ -138,5 +141,6 @@ export const build = async options => {
138
141
  }
139
142
 
140
143
  await generateRoutes(appContext);
141
- await mountHook().afterBuild();
144
+ await hookRunners.afterBuild();
145
+ await emitResolvedConfig(appDirectory, resolvedConfig);
142
146
  };
@@ -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
  };
@@ -5,32 +5,36 @@ 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 { fs, logger, HMR_SOCK_PATH, clearConsole, chalk, isSSR } from '@modern-js/utils';
8
- import { useAppContext, useResolvedConfigContext, mountHook, AppContext } from '@modern-js/core';
9
8
  import { createCompiler } from "../utils/createCompiler";
10
9
  import { createServer } from "../utils/createServer";
11
10
  import { generateRoutes } from "../utils/routes";
12
11
  import { printInstructions } from "../utils/printInstructions";
13
12
  import { getSpecifiedEntries } from "../utils/getSpecifiedEntries";
14
- export const dev = async options => {
15
- /* eslint-disable react-hooks/rules-of-hooks */
16
- const appContext = useAppContext();
17
- const userConfig = useResolvedConfigContext();
18
- /* eslint-enable react-hooks/rules-of-hooks */
19
-
13
+ import { buildServerConfig } from "../utils/config";
14
+ export const dev = async (api, options) => {
15
+ const appContext = api.useAppContext();
16
+ const userConfig = api.useResolvedConfigContext();
17
+ const hookRunners = api.useHookRunners();
20
18
  const {
21
19
  appDirectory,
22
20
  distDirectory,
23
21
  port,
24
22
  existSrc,
25
- entrypoints
23
+ entrypoints,
24
+ serverConfigFile
26
25
  } = appContext;
27
26
  const checkedEntries = await getSpecifiedEntries(options.entry || false, entrypoints);
28
- AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
27
+ api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
29
28
  checkedEntries
30
29
  }));
31
30
  appContext.checkedEntries = checkedEntries;
32
31
  fs.emptyDirSync(distDirectory);
33
- await mountHook().beforeDev();
32
+ await buildServerConfig(appDirectory, serverConfigFile, {
33
+ esbuildOptions: {
34
+ watch: true
35
+ }
36
+ });
37
+ await hookRunners.beforeDev();
34
38
  let compiler = null;
35
39
 
36
40
  if (existSrc) {
@@ -40,6 +44,7 @@ export const dev = async options => {
40
44
  } = await import('@modern-js/webpack');
41
45
  const webpackConfigs = [isSSR(userConfig) && getWebpackConfig(WebpackConfigTarget.NODE), getWebpackConfig(WebpackConfigTarget.CLIENT)].filter(Boolean);
42
46
  compiler = await createCompiler({
47
+ api,
43
48
  webpackConfigs,
44
49
  userConfig,
45
50
  appContext
@@ -68,6 +73,7 @@ export const dev = async options => {
68
73
  compiler,
69
74
  pwd: appDirectory,
70
75
  config: userConfig,
76
+ serverConfigFile,
71
77
  plugins: appContext.plugins.filter(p => p.server).map(p => p.server)
72
78
  });
73
79
  app.listen(port, async err => {
@@ -79,7 +85,7 @@ export const dev = async options => {
79
85
  clearConsole();
80
86
  logger.log(chalk.cyan(`Starting the development server...`));
81
87
  } else {
82
- await printInstructions(appContext, userConfig);
88
+ await printInstructions(hookRunners, appContext, userConfig);
83
89
  }
84
90
  });
85
91
  };
@@ -1,28 +1,27 @@
1
1
  import { logger, chalk } from '@modern-js/utils';
2
- import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
3
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();
7
+ const hookRunners = api.useHookRunners();
11
8
  const {
12
9
  appDirectory,
13
- port
10
+ port,
11
+ serverConfigFile
14
12
  } = appContext;
15
13
  logger.log(chalk.cyan(`Starting the modern server...`));
16
14
  const app = await server({
17
15
  pwd: appDirectory,
18
16
  config: userConfig,
19
- plugins: appContext.plugins.filter(p => p.server).map(p => p.server)
17
+ plugins: appContext.plugins.filter(p => p.server).map(p => p.server),
18
+ serverConfigFile
20
19
  });
21
20
  app.listen(port, async err => {
22
21
  if (err) {
23
22
  throw err;
24
23
  }
25
24
 
26
- await printInstructions(appContext, userConfig);
25
+ await printInstructions(hookRunners, appContext, userConfig);
27
26
  });
28
27
  };
@@ -0,0 +1 @@
1
+ export { defineServerConfig as defineConfig } from "../utils/config";
@@ -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
+ }));
@@ -0,0 +1,53 @@
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_CACHE_DIR, CONFIG_FILE_EXTENSIONS, fs, getServerConfig, OUTPUT_CONFIG_FILE } from '@modern-js/utils';
10
+ export const defineServerConfig = config => config;
11
+ export const buildServerConfig = async (appDirectory, configFile, options) => {
12
+ const configFilePath = await getServerConfig(appDirectory, configFile);
13
+
14
+ const getOutputFile = filepath => path.resolve(CONFIG_CACHE_DIR, `${filepath.replace(new RegExp(CONFIG_FILE_EXTENSIONS.join('|')), '')}.js`);
15
+
16
+ if (configFilePath) {
17
+ await bundle(configFilePath, _objectSpread(_objectSpread({}, options), {}, {
18
+ getOutputFile
19
+ }));
20
+ }
21
+ };
22
+ /**
23
+ *
24
+ * 处理循环引用的 replacer
25
+ */
26
+
27
+ export const safeReplacer = () => {
28
+ const cache = [];
29
+ const keyCache = [];
30
+ return function (key, value) {
31
+ if (typeof value === 'object' && value !== null) {
32
+ const index = cache.indexOf(value);
33
+
34
+ if (index !== -1) {
35
+ return `[Circular ${keyCache[index]}]`;
36
+ }
37
+
38
+ cache.push(value);
39
+ keyCache.push(key || 'root');
40
+ }
41
+
42
+ return value;
43
+ };
44
+ };
45
+ export const emitResolvedConfig = async (appDirectory, resolvedConfig) => {
46
+ var _resolvedConfig$outpu;
47
+
48
+ 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);
49
+ await fs.writeJSON(outputPath, resolvedConfig, {
50
+ spaces: 2,
51
+ replacer: safeReplacer()
52
+ });
53
+ };
@@ -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,9 +57,8 @@ 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
- } // eslint-disable-next-line require-atomic-updates
61
-
60
+ await printInstructions(hookRunners, appContext, userConfig);
61
+ }
62
62
 
63
63
  isFirstCompile = false;
64
64
  });
@@ -10,8 +10,7 @@ export const closeServer = async () => {
10
10
  export const createServer = async options => {
11
11
  if (server) {
12
12
  await server.close();
13
- } // eslint-disable-next-line require-atomic-updates
14
-
13
+ }
15
14
 
16
15
  server = new Server(options);
17
16
  const app = await server.init();
@@ -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 (hookRunners, appContext, config) => {
4
3
  let message = prettyInstructions(appContext, config);
5
4
  const {
6
5
  existSrc
@@ -13,7 +12,7 @@ export const printInstructions = async (appContext, config) => {
13
12
 
14
13
  const {
15
14
  instructions
16
- } = await mountHook().beforePrintInstructions({
15
+ } = await hookRunners.beforePrintInstructions({
17
16
  instructions: message
18
17
  });
19
18
  logger.log(instructions);