@modern-js/core 1.7.1 → 1.9.1

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 (215) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +47 -0
  3. package/bin/modern-js.js +1 -1
  4. package/compiled/ajv/codegen.js +1 -0
  5. package/compiled/ajv/dist/ajv.d.ts +16 -0
  6. package/compiled/ajv/dist/compile/codegen/code.d.ts +40 -0
  7. package/compiled/ajv/dist/compile/codegen/index.d.ts +79 -0
  8. package/compiled/ajv/dist/compile/codegen/scope.d.ts +79 -0
  9. package/compiled/ajv/dist/compile/errors.d.ts +13 -0
  10. package/compiled/ajv/dist/compile/index.d.ts +80 -0
  11. package/compiled/ajv/dist/compile/ref_error.d.ts +6 -0
  12. package/compiled/ajv/dist/compile/resolve.d.ts +12 -0
  13. package/compiled/ajv/dist/compile/rules.d.ts +28 -0
  14. package/compiled/ajv/dist/compile/util.d.ts +40 -0
  15. package/compiled/ajv/dist/compile/validate/index.d.ts +42 -0
  16. package/compiled/ajv/dist/compile/validate/subschema.d.ts +47 -0
  17. package/compiled/ajv/dist/core.d.ts +173 -0
  18. package/compiled/ajv/dist/runtime/validation_error.d.ts +7 -0
  19. package/compiled/ajv/dist/types/index.d.ts +183 -0
  20. package/compiled/ajv/dist/types/json-schema.d.ts +124 -0
  21. package/compiled/ajv/dist/types/jtd-schema.d.ts +169 -0
  22. package/compiled/ajv/index.js +9 -0
  23. package/compiled/ajv/license +22 -0
  24. package/compiled/ajv/package.json +1 -0
  25. package/compiled/ajv/uri-js/dist/es5/uri.all.d.ts +59 -0
  26. package/compiled/ajv-keywords/ajv/dist/ajv.d.ts +16 -0
  27. package/compiled/ajv-keywords/ajv/dist/compile/codegen/code.d.ts +40 -0
  28. package/compiled/ajv-keywords/ajv/dist/compile/codegen/index.d.ts +79 -0
  29. package/compiled/ajv-keywords/ajv/dist/compile/codegen/scope.d.ts +79 -0
  30. package/compiled/ajv-keywords/ajv/dist/compile/errors.d.ts +13 -0
  31. package/compiled/ajv-keywords/ajv/dist/compile/index.d.ts +80 -0
  32. package/compiled/ajv-keywords/ajv/dist/compile/ref_error.d.ts +6 -0
  33. package/compiled/ajv-keywords/ajv/dist/compile/resolve.d.ts +12 -0
  34. package/compiled/ajv-keywords/ajv/dist/compile/rules.d.ts +28 -0
  35. package/compiled/ajv-keywords/ajv/dist/compile/util.d.ts +40 -0
  36. package/compiled/ajv-keywords/ajv/dist/compile/validate/index.d.ts +42 -0
  37. package/compiled/ajv-keywords/ajv/dist/compile/validate/subschema.d.ts +47 -0
  38. package/compiled/ajv-keywords/ajv/dist/core.d.ts +173 -0
  39. package/compiled/ajv-keywords/ajv/dist/runtime/validation_error.d.ts +7 -0
  40. package/compiled/ajv-keywords/ajv/dist/types/index.d.ts +183 -0
  41. package/compiled/ajv-keywords/ajv/dist/types/json-schema.d.ts +124 -0
  42. package/compiled/ajv-keywords/ajv/dist/types/jtd-schema.d.ts +169 -0
  43. package/compiled/ajv-keywords/dist/index.d.ts +4 -0
  44. package/compiled/ajv-keywords/index.js +1 -0
  45. package/compiled/ajv-keywords/license +21 -0
  46. package/compiled/ajv-keywords/package.json +1 -0
  47. package/compiled/ajv-keywords/uri-js/dist/es5/uri.all.d.ts +59 -0
  48. package/compiled/better-ajv-errors/ajv/dist/ajv.d.ts +16 -0
  49. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/code.d.ts +40 -0
  50. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/index.d.ts +79 -0
  51. package/compiled/better-ajv-errors/ajv/dist/compile/codegen/scope.d.ts +79 -0
  52. package/compiled/better-ajv-errors/ajv/dist/compile/errors.d.ts +13 -0
  53. package/compiled/better-ajv-errors/ajv/dist/compile/index.d.ts +80 -0
  54. package/compiled/better-ajv-errors/ajv/dist/compile/ref_error.d.ts +6 -0
  55. package/compiled/better-ajv-errors/ajv/dist/compile/resolve.d.ts +12 -0
  56. package/compiled/better-ajv-errors/ajv/dist/compile/rules.d.ts +28 -0
  57. package/compiled/better-ajv-errors/ajv/dist/compile/util.d.ts +40 -0
  58. package/compiled/better-ajv-errors/ajv/dist/compile/validate/index.d.ts +42 -0
  59. package/compiled/better-ajv-errors/ajv/dist/compile/validate/subschema.d.ts +47 -0
  60. package/compiled/better-ajv-errors/ajv/dist/core.d.ts +173 -0
  61. package/compiled/better-ajv-errors/ajv/dist/runtime/validation_error.d.ts +7 -0
  62. package/compiled/better-ajv-errors/ajv/dist/types/index.d.ts +183 -0
  63. package/compiled/better-ajv-errors/ajv/dist/types/json-schema.d.ts +124 -0
  64. package/compiled/better-ajv-errors/ajv/dist/types/jtd-schema.d.ts +169 -0
  65. package/compiled/better-ajv-errors/index.js +1 -0
  66. package/compiled/better-ajv-errors/license +13 -0
  67. package/compiled/better-ajv-errors/package.json +1 -0
  68. package/compiled/better-ajv-errors/typings.d.ts +24 -0
  69. package/compiled/better-ajv-errors/uri-js/dist/es5/uri.all.d.ts +59 -0
  70. package/compiled/v8-compile-cache/index.js +1 -0
  71. package/compiled/v8-compile-cache/license +21 -0
  72. package/compiled/v8-compile-cache/package.json +1 -0
  73. package/dist/cli.d.ts +1 -0
  74. package/dist/cli.js +33 -0
  75. package/dist/config/defaults.d.ts +28 -0
  76. package/dist/config/defaults.js +98 -0
  77. package/dist/{types/config → config}/index.d.ts +2 -2
  78. package/dist/config/index.js +120 -0
  79. package/dist/config/mergeConfig.d.ts +30 -0
  80. package/dist/config/mergeConfig.js +26 -0
  81. package/dist/config/schema/deploy.d.ts +16 -0
  82. package/dist/config/schema/deploy.js +16 -0
  83. package/dist/config/schema/index.d.ts +466 -0
  84. package/dist/config/schema/index.js +95 -0
  85. package/dist/config/schema/output.d.ts +146 -0
  86. package/dist/config/schema/output.js +68 -0
  87. package/dist/config/schema/server.d.ts +182 -0
  88. package/dist/config/schema/server.js +107 -0
  89. package/dist/config/schema/source.d.ts +58 -0
  90. package/dist/config/schema/source.js +36 -0
  91. package/dist/config/schema/tools.d.ts +36 -0
  92. package/dist/config/schema/tools.js +19 -0
  93. package/dist/config/types/electron.d.ts +13 -0
  94. package/dist/config/types/electron.js +2 -0
  95. package/dist/config/types/index.d.ts +240 -0
  96. package/dist/config/types/index.js +2 -0
  97. package/dist/config/types/less.d.ts +10 -0
  98. package/dist/config/types/less.js +2 -0
  99. package/dist/config/types/sass.d.ts +8 -0
  100. package/dist/config/types/sass.js +2 -0
  101. package/dist/{types/config → config}/types/ssg.d.ts +8 -8
  102. package/dist/config/types/ssg.js +2 -0
  103. package/dist/config/types/test.d.ts +14 -0
  104. package/dist/config/types/test.js +2 -0
  105. package/dist/config/types/unbundle.d.ts +25 -0
  106. package/dist/config/types/unbundle.js +2 -0
  107. package/dist/{types/context.d.ts → context.d.ts} +12 -22
  108. package/dist/context.js +59 -0
  109. package/dist/index.d.ts +64 -0
  110. package/dist/index.js +177 -0
  111. package/dist/{types/initWatcher.d.ts → initWatcher.d.ts} +1 -1
  112. package/dist/initWatcher.js +75 -0
  113. package/dist/{types/loadEnv.d.ts → loadEnv.d.ts} +1 -1
  114. package/dist/loadEnv.js +21 -0
  115. package/dist/{types/loadPlugins.d.ts → loadPlugins.d.ts} +15 -15
  116. package/dist/loadPlugins.js +109 -0
  117. package/dist/manager.d.ts +72 -0
  118. package/dist/manager.js +24 -0
  119. package/dist/{types/pluginAPI.d.ts → pluginAPI.d.ts} +5 -6
  120. package/dist/pluginAPI.js +16 -0
  121. package/dist/{types/utils → utils}/commander.d.ts +1 -1
  122. package/dist/utils/commander.js +20 -0
  123. package/dist/{types/utils → utils}/repeatKeyWarning.d.ts +1 -1
  124. package/dist/utils/repeatKeyWarning.js +22 -0
  125. package/package.json +16 -29
  126. package/tsconfig.json +3 -4
  127. package/dist/js/modern/cli.js +0 -29
  128. package/dist/js/modern/config/defaults.js +0 -103
  129. package/dist/js/modern/config/index.js +0 -115
  130. package/dist/js/modern/config/mergeConfig.js +0 -22
  131. package/dist/js/modern/config/schema/deploy.js +0 -17
  132. package/dist/js/modern/config/schema/index.js +0 -107
  133. package/dist/js/modern/config/schema/output.js +0 -147
  134. package/dist/js/modern/config/schema/server.js +0 -170
  135. package/dist/js/modern/config/schema/source.js +0 -59
  136. package/dist/js/modern/config/schema/tools.js +0 -36
  137. package/dist/js/modern/config/types/index.js +0 -1
  138. package/dist/js/modern/config/types/less.js +0 -0
  139. package/dist/js/modern/config/types/sass.js +0 -0
  140. package/dist/js/modern/config/types/ssg.js +0 -0
  141. package/dist/js/modern/config/types/test.js +0 -0
  142. package/dist/js/modern/config/types/unbundle.js +0 -0
  143. package/dist/js/modern/context.js +0 -63
  144. package/dist/js/modern/index.js +0 -168
  145. package/dist/js/modern/initWatcher.js +0 -62
  146. package/dist/js/modern/loadEnv.js +0 -14
  147. package/dist/js/modern/loadPlugins.js +0 -117
  148. package/dist/js/modern/manager.js +0 -28
  149. package/dist/js/modern/pluginAPI.js +0 -11
  150. package/dist/js/modern/utils/commander.js +0 -19
  151. package/dist/js/modern/utils/repeatKeyWarning.js +0 -18
  152. package/dist/js/node/cli.js +0 -35
  153. package/dist/js/node/config/defaults.js +0 -110
  154. package/dist/js/node/config/index.js +0 -182
  155. package/dist/js/node/config/mergeConfig.js +0 -32
  156. package/dist/js/node/config/schema/deploy.js +0 -26
  157. package/dist/js/node/config/schema/index.js +0 -127
  158. package/dist/js/node/config/schema/output.js +0 -156
  159. package/dist/js/node/config/schema/server.js +0 -179
  160. package/dist/js/node/config/schema/source.js +0 -68
  161. package/dist/js/node/config/schema/tools.js +0 -43
  162. package/dist/js/node/config/types/index.js +0 -5
  163. package/dist/js/node/config/types/less.js +0 -0
  164. package/dist/js/node/config/types/sass.js +0 -0
  165. package/dist/js/node/config/types/ssg.js +0 -0
  166. package/dist/js/node/config/types/test.js +0 -0
  167. package/dist/js/node/config/types/unbundle.js +0 -0
  168. package/dist/js/node/context.js +0 -93
  169. package/dist/js/node/index.js +0 -328
  170. package/dist/js/node/initWatcher.js +0 -82
  171. package/dist/js/node/loadEnv.js +0 -30
  172. package/dist/js/node/loadPlugins.js +0 -129
  173. package/dist/js/node/manager.js +0 -45
  174. package/dist/js/node/pluginAPI.js +0 -54
  175. package/dist/js/node/utils/commander.js +0 -32
  176. package/dist/js/node/utils/repeatKeyWarning.js +0 -31
  177. package/dist/types/cli.d.ts +0 -1
  178. package/dist/types/config/defaults.d.ts +0 -28
  179. package/dist/types/config/mergeConfig.d.ts +0 -31
  180. package/dist/types/config/schema/deploy.d.ts +0 -16
  181. package/dist/types/config/schema/index.d.ts +0 -466
  182. package/dist/types/config/schema/output.d.ts +0 -146
  183. package/dist/types/config/schema/server.d.ts +0 -182
  184. package/dist/types/config/schema/source.d.ts +0 -58
  185. package/dist/types/config/schema/tools.d.ts +0 -36
  186. package/dist/types/config/types/index.d.ts +0 -239
  187. package/dist/types/config/types/less.d.ts +0 -10
  188. package/dist/types/config/types/sass.d.ts +0 -8
  189. package/dist/types/config/types/test.d.ts +0 -15
  190. package/dist/types/config/types/unbundle.d.ts +0 -28
  191. package/dist/types/index.d.ts +0 -61
  192. package/dist/types/manager.d.ts +0 -74
  193. package/modern.config.js +0 -6
  194. package/tests/.eslintrc.js +0 -6
  195. package/tests/btsm.test.ts +0 -20
  196. package/tests/config.test.ts +0 -146
  197. package/tests/context.test.ts +0 -85
  198. package/tests/fixtures/index-test/modern.server-runtime.config.js +0 -0
  199. package/tests/fixtures/index-test/package.json +0 -3
  200. package/tests/fixtures/load-plugin/not-found/package.json +0 -3
  201. package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +0 -1
  202. package/tests/fixtures/load-plugin/user-plugins/package.json +0 -3
  203. package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +0 -1
  204. package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +0 -3
  205. package/tests/fixtures/load-plugin/user-plugins/test-plugin-c.js +0 -3
  206. package/tests/index.test.ts +0 -83
  207. package/tests/initWatcher.test.ts +0 -63
  208. package/tests/loadEnv.test.ts +0 -173
  209. package/tests/loadPlugin.test.ts +0 -116
  210. package/tests/mergeConfig.test.ts +0 -97
  211. package/tests/pluginAPI.test.ts +0 -19
  212. package/tests/repeatKeyWarning.test.ts +0 -68
  213. package/tests/schema.test.ts +0 -107
  214. package/tests/tsconfig.json +0 -11
  215. package/tests/utils.test.ts +0 -7
@@ -1,74 +0,0 @@
1
- import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWaterfall, ParallelWorkflow } from '@modern-js/plugin';
2
- import type { Hooks } from '@modern-js/types';
3
- import type { Command } from './utils/commander';
4
- import type { NormalizedConfig } from './config/mergeConfig';
5
- import { pluginAPI } from './pluginAPI';
6
- export declare type HooksRunner = ToRunners<{
7
- config: ParallelWorkflow<void>;
8
- resolvedConfig: AsyncWaterfall<{
9
- resolved: NormalizedConfig;
10
- }>;
11
- validateSchema: ParallelWorkflow<void>;
12
- prepare: AsyncWorkflow<void, void>;
13
- commands: AsyncWorkflow<{
14
- program: Command;
15
- }, void>;
16
- watchFiles: ParallelWorkflow<void>;
17
- fileChange: AsyncWorkflow<{
18
- filename: string;
19
- eventType: 'add' | 'change' | 'unlink';
20
- }, void>;
21
- beforeExit: AsyncWorkflow<void, void>;
22
- beforeRestart: AsyncWorkflow<void, void>;
23
- }>;
24
- declare const baseHooks: {
25
- config: ParallelWorkflow<void, unknown>;
26
- resolvedConfig: AsyncWaterfall<{
27
- resolved: NormalizedConfig;
28
- }>;
29
- validateSchema: ParallelWorkflow<void, unknown>;
30
- prepare: AsyncWorkflow<void, void>;
31
- commands: AsyncWorkflow<{
32
- program: Command;
33
- }, void>;
34
- watchFiles: ParallelWorkflow<void, unknown>;
35
- fileChange: AsyncWorkflow<{
36
- filename: string;
37
- eventType: 'add' | 'change' | 'unlink';
38
- }, void>;
39
- beforeExit: AsyncWorkflow<void, void>;
40
- beforeRestart: AsyncWorkflow<void, void>;
41
- };
42
- /** All hooks of cli plugin. */
43
-
44
- export declare type CliHooks = typeof baseHooks & Hooks;
45
- /** All hook callbacks of cli plugin. */
46
-
47
- export declare type CliHookCallbacks = ToThreads<CliHooks>;
48
- export declare const manager: import("@modern-js/plugin").AsyncManager<CliHooks, {
49
- setAppContext: (value: import("@modern-js/types").IAppContext) => void;
50
- useAppContext: () => import("@modern-js/types").IAppContext;
51
- useConfigContext: () => import("./config").UserConfig;
52
- useResolvedConfigContext: () => NormalizedConfig;
53
- }>;
54
- /** Plugin options of a cli plugin. */
55
-
56
- export declare type CliPlugin = PluginOptions<CliHooks, AsyncSetup<CliHooks, typeof pluginAPI>>;
57
- export declare const createPlugin: (setup?: AsyncSetup<CliHooks, {
58
- setAppContext: (value: import("@modern-js/types").IAppContext) => void;
59
- useAppContext: () => import("@modern-js/types").IAppContext;
60
- useConfigContext: () => import("./config").UserConfig;
61
- useResolvedConfigContext: () => NormalizedConfig;
62
- }> | undefined, options?: PluginOptions<CliHooks, AsyncSetup<CliHooks, {
63
- setAppContext: (value: import("@modern-js/types").IAppContext) => void;
64
- useAppContext: () => import("@modern-js/types").IAppContext;
65
- useConfigContext: () => import("./config").UserConfig;
66
- useResolvedConfigContext: () => NormalizedConfig;
67
- }>> | undefined) => import("@modern-js/plugin").AsyncPlugin<CliHooks, {
68
- setAppContext: (value: import("@modern-js/types").IAppContext) => void;
69
- useAppContext: () => import("@modern-js/types").IAppContext;
70
- useConfigContext: () => import("./config").UserConfig;
71
- useResolvedConfigContext: () => NormalizedConfig;
72
- }>, registerHook: (newHooks: Partial<CliHooks>) => void, mountHook: () => ToRunners<CliHooks>;
73
- export declare const usePlugins: (plugins: string[]) => void;
74
- export {};
package/modern.config.js DELETED
@@ -1,6 +0,0 @@
1
- /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {
3
- output: {
4
- disableSourceMap: true,
5
- },
6
- };
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['@modern-js'],
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
@@ -1,20 +0,0 @@
1
- import path from 'path';
2
- import { spawnSync } from 'child_process';
3
-
4
- const kPackageDir = path.resolve(__dirname, '..');
5
-
6
- describe('jsnext:source', () => {
7
- test('process exit status is 0', () => {
8
- const { status, stdout, stderr } = spawnSync(
9
- process.execPath,
10
- ['--conditions=jsnext:source', '-r', 'btsm', 'src/cli.ts'],
11
- {
12
- cwd: kPackageDir,
13
- encoding: 'utf-8',
14
- },
15
- );
16
- expect(stdout).toBe('');
17
- expect(stderr.startsWith('Usage: modern <command> [options]')).toBe(true);
18
- expect(status).toBe(1);
19
- });
20
- });
@@ -1,146 +0,0 @@
1
- import path from 'path';
2
- // import os from 'os';
3
- import { isDev, getPort, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
4
- import { resolveConfig, addServerConfigToDeps } from '../src/config';
5
- import {
6
- cli,
7
- loadUserConfig,
8
- initAppContext,
9
- initAppDir,
10
- manager,
11
- createPlugin,
12
- registerHook,
13
- useRunner,
14
- } from '../src';
15
- import { defaults } from '../src/config/defaults';
16
-
17
- jest.mock('@modern-js/utils', () => ({
18
- __esModule: true,
19
- ...jest.requireActual('@modern-js/utils'),
20
- isDev: jest.fn(),
21
- getPort: jest.fn(),
22
- }));
23
-
24
- // const kOSRootDir =
25
- // os.platform() === 'win32' ? process.cwd().split(path.sep)[0] : '/';
26
-
27
- describe('config', () => {
28
- /**
29
- * TypeScript Type annotations cannot be used for esbuild-jest
30
- * test files that use jest.mock('@some/module')
31
- * refer to this esbuild-jest issue:
32
- * https://github.com/aelbore/esbuild-jest/issues/57
33
- * TODO: find a better solution to solve this problem while allowing us
34
- * to use esbuild, and have good TypeScript support
35
- */
36
- let loaded = {
37
- config: {},
38
- filePath: '',
39
- dependencies: [],
40
- pkgConfig: {},
41
- jsConfig: {},
42
- };
43
- let schemas: any[] = [];
44
- let restartWithExistingPort = 0;
45
- let argv: string[] = ['dev'];
46
- let configs: any[] = [];
47
-
48
- const getResolvedConfig = async () =>
49
- resolveConfig(loaded, configs, schemas, restartWithExistingPort, argv);
50
-
51
- const resetParams = () => {
52
- loaded = {
53
- config: {},
54
- filePath: '',
55
- dependencies: [],
56
- pkgConfig: {},
57
- jsConfig: {},
58
- };
59
- schemas = [];
60
- restartWithExistingPort = 0;
61
- argv = ['dev'];
62
- configs = [];
63
- };
64
- const resetMock = () => {
65
- jest.resetAllMocks();
66
- (isDev as jest.Mock).mockReturnValue(true);
67
- (getPort as jest.Mock).mockReturnValue(
68
- Promise.resolve(defaults.server.port),
69
- );
70
- };
71
- beforeEach(() => {
72
- resetParams();
73
- resetMock();
74
- });
75
-
76
- it('default', () => {
77
- expect(resolveConfig).toBeDefined();
78
- expect(cli).toBeDefined();
79
- expect(loadUserConfig).toBeDefined();
80
- expect(initAppContext).toBeDefined();
81
- expect(initAppDir).toBeDefined();
82
- expect(manager).toBeDefined();
83
- expect(createPlugin).toBeDefined();
84
- expect(registerHook).toBeDefined();
85
- expect(useRunner).toBeDefined();
86
- });
87
-
88
- it('initAppDir', async () => {
89
- expect(await initAppDir(__dirname)).toBe(path.resolve(__dirname, '..'));
90
- // expect(await initAppDir()).toBe(path.resolve(__dirname, '..'));
91
-
92
- // FIXME: windows 下面会失败,先忽略这个测试
93
- // try {
94
- // await initAppDir(kOSRootDir);
95
- // expect(true).toBe(false); // SHOULD NOT BE HERE
96
- // } catch (err: any) {
97
- // expect(err.message).toMatch(/no package.json found in current work dir/);
98
- // }
99
- });
100
-
101
- test('should use default port if not restarting in dev mode', async () => {
102
- let resolved = await getResolvedConfig();
103
- expect(resolved.server.port).toEqual(defaults.server.port);
104
- expect(getPort).toHaveBeenCalledWith(defaults.server.port);
105
-
106
- // getResolvedConfig should use the value givin by getPort
107
- restartWithExistingPort = -1;
108
- (getPort as jest.Mock).mockClear();
109
- (getPort as jest.Mock).mockReturnValue(1111);
110
- resolved = await getResolvedConfig();
111
- expect(resolved.server.port).toEqual(1111);
112
- expect(getPort).toHaveBeenCalledWith(defaults.server.port);
113
-
114
- argv = ['start'];
115
- (isDev as jest.Mock).mockReturnValue(false);
116
- restartWithExistingPort = 0;
117
- resolved = await getResolvedConfig();
118
- expect(resolved.server.port).toEqual(defaults.server.port);
119
-
120
- restartWithExistingPort = 1234;
121
- resolved = await getResolvedConfig();
122
- expect(resolved.server.port).toEqual(defaults.server.port);
123
-
124
- restartWithExistingPort = -1;
125
- resolved = await getResolvedConfig();
126
- expect(resolved.server.port).toEqual(defaults.server.port);
127
- });
128
-
129
- test('should reuse existing port if restarting in dev mode', async () => {
130
- restartWithExistingPort = 1234;
131
- const resolved = await getResolvedConfig();
132
- expect(resolved.server.port).toEqual(1234);
133
- });
134
- });
135
-
136
- describe('addServerConfigToDeps', () => {
137
- it('should add server config to deps', async () => {
138
- const appDirectory = path.join(__dirname, './fixtures/index-test');
139
- const deps: string[] = [];
140
- await addServerConfigToDeps(deps, appDirectory, DEFAULT_SERVER_CONFIG);
141
- expect(deps.length).toBe(1);
142
- expect(deps[0]).toBe(
143
- path.join(appDirectory, `${DEFAULT_SERVER_CONFIG}.js`),
144
- );
145
- });
146
- });
@@ -1,85 +0,0 @@
1
- import path from 'path';
2
- import { DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
3
- import { initAppContext } from '../src/context';
4
-
5
- describe('context', () => {
6
- it('initAppContext', () => {
7
- const appDirectory = path.resolve(
8
- __dirname,
9
- './fixtures/load-plugin/user-plugins',
10
- );
11
- const appContext = initAppContext({
12
- appDirectory,
13
- plugins: [],
14
- configFile: false,
15
- serverConfigFile: DEFAULT_SERVER_CONFIG,
16
- });
17
-
18
- expect(appContext).toEqual({
19
- appDirectory,
20
- configFile: false,
21
- serverConfigFile: DEFAULT_SERVER_CONFIG,
22
- ip: expect.any(String),
23
- port: 0,
24
- packageName: expect.any(String),
25
- srcDirectory: expect.any(String),
26
- distDirectory: expect.any(String),
27
- sharedDirectory: expect.any(String),
28
- nodeModulesDirectory: expect.any(String),
29
- internalDirectory: expect.any(String),
30
- plugins: [],
31
- htmlTemplates: {},
32
- serverRoutes: [],
33
- entrypoints: [],
34
- checkedEntries: [],
35
- existSrc: true,
36
- internalDirAlias: '@_modern_js_internal',
37
- internalSrcAlias: '@_modern_js_src',
38
- metaName: 'modern-js',
39
- });
40
- });
41
-
42
- it('custom AppContext', () => {
43
- const appDirectory = path.resolve(
44
- __dirname,
45
- './fixtures/load-plugin/user-plugins',
46
- );
47
-
48
- const customOptions = {
49
- srcDir: 'source',
50
- distDir: 'dist',
51
- sharedDir: 'myShared',
52
- metaName: 'jupiter',
53
- };
54
-
55
- const appContext = initAppContext({
56
- appDirectory,
57
- plugins: [],
58
- configFile: false,
59
- options: customOptions,
60
- serverConfigFile: DEFAULT_SERVER_CONFIG,
61
- });
62
- expect(appContext).toEqual({
63
- appDirectory,
64
- configFile: false,
65
- serverConfigFile: DEFAULT_SERVER_CONFIG,
66
- ip: expect.any(String),
67
- port: 0,
68
- packageName: 'user-plugins',
69
- srcDirectory: path.resolve(appDirectory, './source'),
70
- distDirectory: 'dist',
71
- sharedDirectory: path.resolve(appDirectory, './myShared'),
72
- nodeModulesDirectory: expect.any(String),
73
- internalDirectory: path.resolve(appDirectory, './node_modules/.jupiter'),
74
- plugins: [],
75
- htmlTemplates: {},
76
- serverRoutes: [],
77
- entrypoints: [],
78
- checkedEntries: [],
79
- existSrc: true,
80
- internalDirAlias: '@_jupiter_internal',
81
- internalSrcAlias: '@_jupiter_src',
82
- metaName: 'jupiter',
83
- });
84
- });
85
- });
@@ -1,3 +0,0 @@
1
- {
2
- "name": "index-test"
3
- }
@@ -1,3 +0,0 @@
1
- {
2
- "name": "not-found"
3
- }
@@ -1 +0,0 @@
1
- module.exports = { name: 'a' };
@@ -1,3 +0,0 @@
1
- {
2
- "name": "user-plugins"
3
- }
@@ -1 +0,0 @@
1
- module.exports = { name: 'a' };
@@ -1,3 +0,0 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
-
3
- exports.default = { name: 'b' };
@@ -1,3 +0,0 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
-
3
- exports.default = name => ({ name });
@@ -1,83 +0,0 @@
1
- import path from 'path';
2
- import { cli, mergeOptions } from '../src';
3
- import { resolveConfig, loadUserConfig } from '../src/config';
4
- import { loadEnv } from '../src/loadEnv';
5
-
6
- jest.mock('../src/config', () => ({
7
- __esModule: true,
8
- ...jest.requireActual('../src/config'),
9
- loadUserConfig: jest.fn(),
10
- resolveConfig: jest.fn(),
11
- }));
12
-
13
- jest.mock('../src/loadEnv', () => ({
14
- __esModule: true,
15
- ...jest.requireActual('../src/loadEnv'),
16
- loadEnv: jest.fn(),
17
- }));
18
-
19
- describe('@modern-js/core test', () => {
20
- let mockResolveConfig: any = {};
21
- let mockLoadedConfig: any = {};
22
- const cwdSpy = jest.spyOn(process, 'cwd');
23
- const cwd = path.join(__dirname, './fixtures/index-test');
24
-
25
- const resetMock = () => {
26
- jest.resetAllMocks();
27
- cwdSpy.mockReturnValue(cwd);
28
- (resolveConfig as jest.Mock).mockReturnValue(
29
- Promise.resolve(mockResolveConfig),
30
- );
31
- (loadUserConfig as jest.Mock).mockImplementation(() =>
32
- Promise.resolve(mockLoadedConfig),
33
- );
34
- };
35
-
36
- const resetValues = () => {
37
- mockLoadedConfig = {
38
- config: {},
39
- filePath: false,
40
- dependencies: [],
41
- pkgConfig: {},
42
- jsConfig: {},
43
- };
44
- mockResolveConfig = {
45
- server: {
46
- port: 8080,
47
- },
48
- output: {
49
- path: './my/test/path',
50
- },
51
- };
52
- };
53
-
54
- beforeEach(() => {
55
- resetValues();
56
- resetMock();
57
- });
58
-
59
- it('test cli create', () => {
60
- expect(cli).toBeTruthy();
61
- });
62
-
63
- it('test cli init dev', async () => {
64
- cwdSpy.mockReturnValue(path.join(cwd, 'nested-folder'));
65
- await cli.init(['dev']);
66
- expect(loadEnv).toHaveBeenCalledWith(cwd, undefined);
67
- // TODO: add more test cases
68
- });
69
- });
70
-
71
- describe('test mergeOptions', () => {
72
- it('serverConfigFile must exist', () => {
73
- const options = mergeOptions({});
74
- expect(options).toHaveProperty('serverConfigFile');
75
- });
76
-
77
- it('serverConfigFile can be overwritten', () => {
78
- const options = mergeOptions({
79
- serverConfigFile: 'test',
80
- });
81
- expect(options.serverConfigFile).toBe('test');
82
- });
83
- });
@@ -1,63 +0,0 @@
1
- import * as path from 'path';
2
- import { fs, wait } from '@modern-js/utils';
3
- import { initWatcher } from '../src/initWatcher';
4
-
5
- jest.useRealTimers();
6
-
7
- const mockAppDirectory = path.join(__dirname, './fixtures/index-test');
8
- const mockConfigDir = './config';
9
- const mockSrcDirectory = path.join(mockAppDirectory, './src');
10
-
11
- describe('initWatcher', () => {
12
- afterAll(() => {
13
- const file = path.join(mockSrcDirectory, './index.ts');
14
- if (fs.existsSync(file)) {
15
- fs.unlinkSync(file);
16
- }
17
- });
18
-
19
- xtest('will trigger add event', async () => {
20
- let triggeredType = '';
21
- let triggeredFile = '';
22
- const loaded = {
23
- filePath: '',
24
- dependencies: [],
25
- };
26
- const hooksRunner = {
27
- watchFiles: async () => [mockSrcDirectory],
28
- fileChange: jest.fn(({ filename, eventType }) => {
29
- triggeredType = eventType;
30
- triggeredFile = filename;
31
- }),
32
- };
33
-
34
- if (await fs.pathExists(mockSrcDirectory)) {
35
- await fs.remove(mockSrcDirectory);
36
- }
37
-
38
- const watcher = await initWatcher(
39
- loaded as any,
40
- mockAppDirectory,
41
- mockConfigDir,
42
- hooksRunner as any,
43
- ['dev'],
44
- );
45
- await wait(100);
46
-
47
- const file = path.join(mockSrcDirectory, './index.ts');
48
- await fs.outputFile(file, '');
49
- await wait(100);
50
- // expect(hooksRunner.fileChange).toBeCalledTimes(1);
51
- // expect(triggeredType).toBe('add');
52
- expect(file.includes(triggeredFile)).toBeTruthy();
53
-
54
- await wait(100);
55
- await fs.remove(file);
56
- await wait(200);
57
- expect(hooksRunner.fileChange).toBeCalledTimes(2);
58
- expect(triggeredType).toBe('unlink');
59
- expect(file.includes(triggeredFile)).toBeTruthy();
60
-
61
- watcher?.close();
62
- });
63
- });
@@ -1,173 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { loadEnv } from '../src/loadEnv';
4
-
5
- const fixture = path.resolve(__dirname, './fixtures/load-env');
6
-
7
- fs.mkdirSync(fixture);
8
-
9
- const createFixtures = (
10
- dir: string,
11
- files: {
12
- name: string;
13
- content: string;
14
- }[],
15
- ) => {
16
- fs.mkdirSync(path.join(fixture, dir));
17
- for (const { name, content } of files) {
18
- fs.writeFileSync(path.join(fixture, dir, name), content, 'utf8');
19
- }
20
- };
21
-
22
- describe('load environment variables', () => {
23
- const defaultEnv = process.env;
24
-
25
- beforeEach(() => (process.env = { ...defaultEnv }));
26
-
27
- afterAll(() => {
28
- process.env = { ...defaultEnv };
29
- fs.rmSync(fixture, { force: true, recursive: true });
30
- });
31
-
32
- test(`support .env file`, () => {
33
- createFixtures('base', [
34
- {
35
- name: '.env',
36
- content: `DB_HOST=localhost
37
- DB_USER=root
38
- DB_PASS=root
39
- `,
40
- },
41
- ]);
42
- loadEnv(path.join(fixture, 'base'));
43
-
44
- expect(process.env.DB_HOST).toBe('localhost');
45
-
46
- expect(process.env.DB_USER).toBe('root');
47
-
48
- expect(process.env.DB_PASS).toBe('root');
49
- });
50
-
51
- test(`support "environment" .env file`, () => {
52
- createFixtures('environment', [
53
- {
54
- name: '.env',
55
- content: `DB_HOST=localhost
56
- DB_USER=root
57
- DB_PASS=root
58
- `,
59
- },
60
- {
61
- name: '.env.production',
62
- content: `DB_HOST=localhost
63
- DB_USER=root-local-dev
64
- `,
65
- },
66
- ]);
67
-
68
- process.env.NODE_ENV = 'production';
69
-
70
- loadEnv(path.join(fixture, 'environment'));
71
-
72
- expect(process.env.DB_HOST).toBe('localhost');
73
-
74
- expect(process.env.DB_USER).toBe('root-local-dev');
75
-
76
- expect(process.env.DB_PASS).toBe('root');
77
-
78
- delete process.env.NODE_ENV;
79
- });
80
-
81
- test(`should have correct priority`, () => {
82
- createFixtures('priority', [
83
- {
84
- name: '.env',
85
- content: `DB_HOST=localhost
86
- DB_USER=user
87
- DB_PASS=pass
88
- `,
89
- },
90
- {
91
- name: '.env.production',
92
- content: `DB_USER=user_production
93
- DB_PASS=pass_production
94
- FOO=foo
95
- BAR=bar
96
- `,
97
- },
98
- {
99
- name: '.env.local',
100
- content: `FOO=foo_local
101
- BAR=bar_local
102
- `,
103
- },
104
- {
105
- name: '.env.production.local',
106
- content: `BAR=bar_production_local`,
107
- },
108
- ]);
109
-
110
- process.env.NODE_ENV = 'production';
111
-
112
- loadEnv(path.join(fixture, 'priority'));
113
-
114
- expect(process.env.DB_HOST).toBe('localhost');
115
-
116
- expect(process.env.DB_USER).toBe('user_production');
117
-
118
- expect(process.env.DB_PASS).toBe('pass_production');
119
-
120
- expect(process.env.FOO).toBe('foo_local');
121
-
122
- expect(process.env.BAR).toBe('bar_production_local');
123
-
124
- delete process.env.NODE_ENV;
125
- });
126
-
127
- test(`get custom .env file by MODERN_ENV`, () => {
128
- createFixtures('custom_environment', [
129
- {
130
- name: '.env',
131
- content: `DB_HOST=localhost
132
- DB_USER=root
133
- DB_PASS=root
134
- `,
135
- },
136
- {
137
- name: '.env.staging',
138
- content: `DB_HOST=localhost
139
- DB_USER=root-local-dev
140
- `,
141
- },
142
- ]);
143
-
144
- loadEnv(path.join(fixture, 'custom_environment'), 'staging');
145
-
146
- expect(process.env.DB_HOST).toBe('localhost');
147
-
148
- expect(process.env.DB_USER).toBe('root-local-dev');
149
-
150
- expect(process.env.DB_PASS).toBe('root');
151
-
152
- delete process.env.MODERN_ENV;
153
- });
154
-
155
- test(`support dotenv-expand`, () => {
156
- createFixtures('expand', [
157
- {
158
- name: '.env',
159
- content: `DB_HOST=localhost
160
- DB_USER=\${DB_HOST}001
161
- DB_PASS=root
162
- `,
163
- },
164
- ]);
165
- loadEnv(path.join(fixture, 'expand'));
166
-
167
- expect(process.env.DB_HOST).toBe('localhost');
168
-
169
- expect(process.env.DB_USER).toBe('localhost001');
170
-
171
- expect(process.env.DB_PASS).toBe('root');
172
- });
173
- });