@modern-js/core 0.0.0-windows-202110385642 → 0.0.1-20221226

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 (130) hide show
  1. package/CHANGELOG.md +1164 -6
  2. package/dist/bin.d.ts +2 -0
  3. package/dist/bin.js +5 -0
  4. package/dist/config/createDefaultConfig.d.ts +2 -0
  5. package/dist/config/createDefaultConfig.js +9 -0
  6. package/dist/config/createLoadedConfig.d.ts +6 -0
  7. package/dist/config/createLoadedConfig.js +35 -0
  8. package/dist/config/createResolvedConfig.d.ts +3 -0
  9. package/dist/config/createResolvedConfig.js +92 -0
  10. package/dist/config/index.d.ts +3 -0
  11. package/dist/config/index.js +19 -0
  12. package/dist/context.d.ts +35 -0
  13. package/dist/context.js +61 -0
  14. package/dist/index.d.ts +73 -0
  15. package/dist/index.js +150 -0
  16. package/dist/load-configs/index.d.ts +34 -0
  17. package/dist/load-configs/index.js +124 -0
  18. package/dist/{types/loadEnv.d.ts → loadEnv.d.ts} +1 -1
  19. package/dist/loadEnv.js +21 -0
  20. package/dist/loadPlugins.d.ts +18 -0
  21. package/dist/loadPlugins.js +65 -0
  22. package/dist/manager.d.ts +87 -0
  23. package/dist/manager.js +24 -0
  24. package/dist/runBin.d.ts +5 -0
  25. package/dist/runBin.js +51 -0
  26. package/dist/schema/patchSchema.d.ts +19 -0
  27. package/dist/schema/patchSchema.js +57 -0
  28. package/dist/schema/source.d.ts +9 -0
  29. package/dist/schema/source.js +10 -0
  30. package/dist/schema/testing.d.ts +13 -0
  31. package/dist/schema/testing.js +11 -0
  32. package/dist/schema/traverseSchema.d.ts +2 -0
  33. package/dist/schema/traverseSchema.js +20 -0
  34. package/dist/types/config/index.d.ts +49 -125
  35. package/dist/types/config/index.js +2 -0
  36. package/dist/types/config/testing.d.ts +15 -0
  37. package/dist/types/config/testing.js +2 -0
  38. package/dist/types/context.d.ts +28 -14
  39. package/dist/types/context.js +2 -0
  40. package/dist/types/hooks.d.ts +44 -0
  41. package/dist/types/hooks.js +2 -0
  42. package/dist/types/index.d.ts +9 -87
  43. package/dist/types/index.js +21 -0
  44. package/dist/types/plugin.d.ts +26 -0
  45. package/dist/types/plugin.js +2 -0
  46. package/dist/types/pluginAPI.d.ts +18 -0
  47. package/dist/types/pluginAPI.js +2 -0
  48. package/dist/utils/commander.d.ts +4 -0
  49. package/dist/utils/commander.js +20 -0
  50. package/dist/utils/mergeConfig.d.ts +2 -0
  51. package/dist/utils/mergeConfig.js +32 -0
  52. package/dist/utils/repeatKeyWarning.d.ts +3 -0
  53. package/dist/utils/repeatKeyWarning.js +22 -0
  54. package/package.json +52 -48
  55. package/bin/modern-js.js +0 -18
  56. package/dist/js/modern/config/defaults.js +0 -100
  57. package/dist/js/modern/config/index.js +0 -104
  58. package/dist/js/modern/config/mergeConfig.js +0 -20
  59. package/dist/js/modern/config/schema/deploy.js +0 -20
  60. package/dist/js/modern/config/schema/index.js +0 -107
  61. package/dist/js/modern/config/schema/output.js +0 -147
  62. package/dist/js/modern/config/schema/server.js +0 -170
  63. package/dist/js/modern/config/schema/source.js +0 -59
  64. package/dist/js/modern/config/schema/tools.js +0 -33
  65. package/dist/js/modern/context.js +0 -25
  66. package/dist/js/modern/index.js +0 -142
  67. package/dist/js/modern/initWatcher.js +0 -46
  68. package/dist/js/modern/loadEnv.js +0 -12
  69. package/dist/js/modern/loadPlugins.js +0 -66
  70. package/dist/js/modern/utils/commander.js +0 -19
  71. package/dist/js/modern/utils/repeatKeyWarning.js +0 -18
  72. package/dist/js/node/config/defaults.js +0 -107
  73. package/dist/js/node/config/index.js +0 -142
  74. package/dist/js/node/config/mergeConfig.js +0 -32
  75. package/dist/js/node/config/schema/deploy.js +0 -29
  76. package/dist/js/node/config/schema/index.js +0 -129
  77. package/dist/js/node/config/schema/output.js +0 -156
  78. package/dist/js/node/config/schema/server.js +0 -179
  79. package/dist/js/node/config/schema/source.js +0 -68
  80. package/dist/js/node/config/schema/tools.js +0 -40
  81. package/dist/js/node/context.js +0 -52
  82. package/dist/js/node/index.js +0 -247
  83. package/dist/js/node/initWatcher.js +0 -67
  84. package/dist/js/node/loadEnv.js +0 -28
  85. package/dist/js/node/loadPlugins.js +0 -76
  86. package/dist/js/node/utils/commander.js +0 -35
  87. package/dist/js/node/utils/repeatKeyWarning.js +0 -31
  88. package/dist/types/config/defaults.d.ts +0 -25
  89. package/dist/types/config/mergeConfig.d.ts +0 -29
  90. package/dist/types/config/schema/deploy.d.ts +0 -19
  91. package/dist/types/config/schema/index.d.ts +0 -466
  92. package/dist/types/config/schema/output.d.ts +0 -146
  93. package/dist/types/config/schema/server.d.ts +0 -182
  94. package/dist/types/config/schema/source.d.ts +0 -58
  95. package/dist/types/config/schema/tools.d.ts +0 -33
  96. package/dist/types/initWatcher.d.ts +0 -3
  97. package/dist/types/loadPlugins.d.ts +0 -16
  98. package/dist/types/utils/commander.d.ts +0 -7
  99. package/dist/types/utils/repeatKeyWarning.d.ts +0 -3
  100. package/modern.config.js +0 -13
  101. package/src/config/defaults.ts +0 -101
  102. package/src/config/index.ts +0 -296
  103. package/src/config/mergeConfig.ts +0 -68
  104. package/src/config/schema/deploy.ts +0 -17
  105. package/src/config/schema/index.ts +0 -116
  106. package/src/config/schema/output.ts +0 -66
  107. package/src/config/schema/server.ts +0 -106
  108. package/src/config/schema/source.ts +0 -34
  109. package/src/config/schema/tools.ts +0 -15
  110. package/src/context.ts +0 -46
  111. package/src/index.ts +0 -247
  112. package/src/initWatcher.ts +0 -77
  113. package/src/loadEnv.ts +0 -21
  114. package/src/loadPlugins.ts +0 -81
  115. package/src/types.d.ts +0 -0
  116. package/src/utils/commander.ts +0 -22
  117. package/src/utils/repeatKeyWarning.ts +0 -29
  118. package/tests/.eslintrc.js +0 -6
  119. package/tests/fixtures/load-plugin/not-found/package.json +0 -3
  120. package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +0 -1
  121. package/tests/fixtures/load-plugin/user-plugins/package.json +0 -3
  122. package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +0 -1
  123. package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +0 -3
  124. package/tests/loadEnv.test.ts +0 -100
  125. package/tests/loadPlugin.test.ts +0 -29
  126. package/tests/mergeConfig.test.ts +0 -78
  127. package/tests/repeatKeyWarning.test.ts +0 -68
  128. package/tests/schema.test.ts +0 -109
  129. package/tests/tsconfig.json +0 -13
  130. package/tsconfig.json +0 -14
@@ -1,15 +0,0 @@
1
- export const tools = {
2
- type: 'object',
3
- additionalProperties: false,
4
- properties: {
5
- webpack: { typeof: ['object', 'function'] },
6
- babel: { typeof: ['object', 'function'] },
7
- postcss: { typeof: ['object', 'function'] },
8
- lodash: { typeof: ['object', 'function'] },
9
- devServer: { type: 'object' },
10
- tsLoader: { typeof: ['object', 'function'] },
11
- autoprefixer: { typeof: ['object', 'function'] },
12
- terser: { typeof: ['object', 'function'] },
13
- minifyCss: { typeof: ['object', 'function'] },
14
- },
15
- };
package/src/context.ts DELETED
@@ -1,46 +0,0 @@
1
- import path from 'path';
2
- import { createContext } from '@modern-js/plugin';
3
- import address from 'address';
4
- import type { IAppContext } from '@modern-js/types';
5
- import { UserConfig } from './config';
6
- import { NormalizedConfig } from './config/mergeConfig';
7
-
8
- export type { IAppContext };
9
-
10
- export const AppContext = createContext<IAppContext>({} as IAppContext);
11
-
12
- export const ConfigContext = createContext<UserConfig>({} as UserConfig);
13
-
14
- export const ResolvedConfigContext = createContext<NormalizedConfig>(
15
- {} as NormalizedConfig,
16
- );
17
-
18
- export const useAppContext = () => AppContext.use().value;
19
-
20
- export const useConfigContext = () => ConfigContext.use().value;
21
-
22
- export const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
23
-
24
- export const initAppContext = (
25
- appDirectory: string,
26
- plugins: Array<{
27
- cli: any;
28
- server: any;
29
- }>,
30
- configFile: string | false,
31
- ): IAppContext => ({
32
- appDirectory,
33
- configFile,
34
- ip: address.ip(),
35
- port: 0,
36
- packageName: require(path.resolve(appDirectory, './package.json')).name,
37
- srcDirectory: path.resolve(appDirectory, './src'),
38
- distDirectory: '',
39
- sharedDirectory: path.resolve(appDirectory, './shared'),
40
- nodeModulesDirectory: path.resolve(appDirectory, './node_modules'),
41
- internalDirectory: path.resolve(appDirectory, './node_modules/.modern-js'),
42
- plugins,
43
- htmlTemplates: {},
44
- serverRoutes: [],
45
- entrypoints: [],
46
- });
package/src/index.ts DELETED
@@ -1,247 +0,0 @@
1
- import path from 'path';
2
- import {
3
- compatRequire,
4
- pkgUp,
5
- ensureAbsolutePath,
6
- logger,
7
- } from '@modern-js/utils';
8
- import {
9
- createAsyncManager,
10
- createAsyncWorkflow,
11
- createParallelWorkflow,
12
- ParallelWorkflow,
13
- AsyncWorkflow,
14
- Progresses2Runners,
15
- } from '@modern-js/plugin';
16
- import { enable } from '@modern-js/plugin/node';
17
-
18
- import type { Hooks } from '@modern-js/types';
19
- import { program, Command } from './utils/commander';
20
- import {
21
- resolveConfig,
22
- defineConfig,
23
- loadUserConfig,
24
- UserConfig,
25
- ToolsConfig,
26
- } from './config';
27
- import { loadPlugins } from './loadPlugins';
28
- import {
29
- AppContext,
30
- ConfigContext,
31
- IAppContext,
32
- initAppContext,
33
- ResolvedConfigContext,
34
- useAppContext,
35
- useConfigContext,
36
- useResolvedConfigContext,
37
- } from './context';
38
- import { initWatcher } from './initWatcher';
39
- import { NormalizedConfig } from './config/mergeConfig';
40
- import { loadEnv } from './loadEnv';
41
-
42
- export type { Hooks };
43
- export { defaultsConfig } from './config';
44
-
45
- export * from '@modern-js/plugin';
46
- export * from '@modern-js/plugin/node';
47
-
48
- program
49
- .name('modern')
50
- .usage('<command> [options]')
51
- .version(process.env.MODERN_JS_VERSION || '0.1.0');
52
-
53
- export type HooksRunner = Progresses2Runners<{
54
- config: ParallelWorkflow<void>;
55
- validateSchema: ParallelWorkflow<void>;
56
- prepare: AsyncWorkflow<void, void>;
57
- commands: AsyncWorkflow<
58
- {
59
- program: Command;
60
- },
61
- void
62
- >;
63
- watchFiles: ParallelWorkflow<void>;
64
- fileChange: AsyncWorkflow<
65
- {
66
- filename: string;
67
- },
68
- void
69
- >;
70
- beforeExit: AsyncWorkflow<void, void>;
71
- }>;
72
-
73
- const hooksMap = {
74
- config: createParallelWorkflow(),
75
- validateSchema: createParallelWorkflow(),
76
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
77
- prepare: createAsyncWorkflow<void, void>(),
78
- commands: createAsyncWorkflow<
79
- {
80
- program: Command;
81
- },
82
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
83
- void
84
- >(),
85
- watchFiles: createParallelWorkflow(),
86
- fileChange: createAsyncWorkflow<
87
- {
88
- filename: string;
89
- },
90
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
91
- void
92
- >(),
93
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
94
- beforeExit: createAsyncWorkflow<void, void>(),
95
- };
96
-
97
- export const manager = createAsyncManager<Hooks, typeof hooksMap>(hooksMap);
98
-
99
- export const {
100
- createPlugin,
101
- registe: registerHook,
102
- useRunner: mountHook,
103
- } = manager;
104
-
105
- export const usePlugins = (plugins: string[]) =>
106
- plugins.forEach(plugin =>
107
- manager.usePlugin(compatRequire(require.resolve(plugin))),
108
- );
109
-
110
- export {
111
- defineConfig,
112
- AppContext,
113
- useAppContext,
114
- useConfigContext,
115
- useResolvedConfigContext,
116
- };
117
-
118
- export type { NormalizedConfig, IAppContext, UserConfig, ToolsConfig };
119
-
120
- const initAppDir = async (): Promise<string> => {
121
- const pkg = await pkgUp({ cwd: process.cwd() });
122
-
123
- if (!pkg) {
124
- throw new Error(
125
- `no package.json found in current work dir: ${process.cwd()}`,
126
- );
127
- }
128
-
129
- return path.dirname(pkg);
130
- };
131
-
132
- export interface CoreOption {
133
- dryRun?: boolean;
134
- }
135
-
136
- const createCli = () => {
137
- let hooksRunner: HooksRunner;
138
- let isRestart = false;
139
-
140
- const init = async (argv: string[] = []) => {
141
- enable();
142
-
143
- manager.clear();
144
-
145
- const appDirectory = await initAppDir();
146
-
147
- loadEnv(appDirectory);
148
-
149
- const loaded = await loadUserConfig(appDirectory);
150
-
151
- const plugins = loadPlugins(appDirectory, loaded.config.plugins || []);
152
-
153
- plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
154
-
155
- const appContext = initAppContext(appDirectory, plugins, loaded.filePath);
156
-
157
- manager.run(() => {
158
- ConfigContext.set(loaded.config);
159
- AppContext.set(appContext);
160
- });
161
-
162
- hooksRunner = await manager.init();
163
-
164
- ['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException'].forEach(
165
- event => {
166
- process.on(event, async err => {
167
- await hooksRunner.beforeExit();
168
- if (err instanceof Error) {
169
- logger.error(err.stack);
170
- }
171
- process.nextTick(() => {
172
- // eslint-disable-next-line no-process-exit
173
- process.exit(1);
174
- });
175
- });
176
- },
177
- );
178
-
179
- const extraConfigs = await hooksRunner.config();
180
-
181
- const extraSchemas = await hooksRunner.validateSchema();
182
-
183
- const resolved = await resolveConfig(
184
- loaded,
185
- extraConfigs as any,
186
- extraSchemas as any,
187
- isRestart,
188
- argv,
189
- );
190
-
191
- // update context value
192
- manager.run(() => {
193
- ConfigContext.set(loaded.config);
194
- ResolvedConfigContext.set(resolved);
195
- AppContext.set({
196
- ...appContext,
197
- port: resolved.server.port!,
198
- distDirectory: ensureAbsolutePath(appDirectory, resolved.output.path!),
199
- });
200
- });
201
-
202
- await hooksRunner.prepare();
203
-
204
- return { loadedConfig: loaded, appContext, resolved };
205
- };
206
-
207
- async function run(argv: string[]) {
208
- const { loadedConfig, appContext, resolved } = await init(argv);
209
-
210
- await hooksRunner.commands({ program });
211
-
212
- initWatcher(
213
- loadedConfig,
214
- appContext.appDirectory,
215
- resolved.source.configDir,
216
- hooksRunner,
217
- argv,
218
- );
219
- manager.run(() => program.parse(process.argv));
220
- }
221
-
222
- async function restart() {
223
- isRestart = true;
224
-
225
- logger.info('Restart...\n');
226
-
227
- let hasGetError = false;
228
- try {
229
- await init(process.argv.slice(2));
230
- } catch (err) {
231
- console.error(err);
232
- hasGetError = true;
233
- } finally {
234
- if (!hasGetError) {
235
- manager.run(() => program.parse(process.argv));
236
- }
237
- }
238
- }
239
-
240
- return {
241
- init,
242
- run,
243
- restart,
244
- };
245
- };
246
-
247
- export const cli = createCli();
@@ -1,77 +0,0 @@
1
- import crypto from 'crypto';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { isDev, createDebugger } from '@modern-js/utils';
5
- import chokidar from 'chokidar';
6
- import { LoadedConfig } from './config';
7
- import { HooksRunner } from '.';
8
-
9
- const debug = createDebugger('watch-files');
10
-
11
- const md5 = (data: string) =>
12
- crypto.createHash('md5').update(data).digest('hex');
13
-
14
- const hashMap = new Map<string, string>();
15
-
16
- export const initWatcher = async (
17
- loaded: LoadedConfig,
18
- appDirectory: string,
19
- configDir: string | undefined,
20
- hooksRunner: HooksRunner,
21
- argv: string[],
22
- // eslint-disable-next-line max-params
23
- ) => {
24
- // only add fs watcher on dev mode.
25
- if (isDev() && argv[0] === 'dev') {
26
- const extraFiles = await hooksRunner.watchFiles();
27
-
28
- const configPath = path.join(appDirectory, configDir!);
29
-
30
- const watched = [
31
- `${configPath}/html`,
32
- ...(extraFiles as any),
33
- loaded?.filePath,
34
- ...loaded.dependencies,
35
- ].filter(Boolean);
36
-
37
- debug(`watched: %o`, watched);
38
-
39
- const watcher = chokidar.watch(watched, {
40
- cwd: appDirectory,
41
- ignorePermissionErrors: true,
42
- ignored: [
43
- /node_modules/,
44
- '**/__test__/**',
45
- '**/*.test.(js|jsx|ts|tsx)',
46
- '**/*.spec.(js|jsx|ts|tsx)',
47
- '**/*.stories.(js|jsx|ts|tsx)',
48
- ],
49
- });
50
-
51
- watcher.on('change', changed => {
52
- const lastHash = hashMap.get(changed);
53
- const currentHash = md5(
54
- fs.readFileSync(path.join(appDirectory, changed), 'utf8'),
55
- );
56
- if (currentHash !== lastHash) {
57
- debug(`file change: %s`, changed);
58
-
59
- hashMap.set(changed, currentHash);
60
-
61
- hooksRunner.fileChange({ filename: changed });
62
- }
63
- });
64
-
65
- watcher.on('unlink', name => {
66
- debug(`remove file: %s`, name);
67
-
68
- if (hashMap.has(name)) {
69
- hashMap.delete(name);
70
- }
71
- });
72
-
73
- watcher.on('error', err => {
74
- throw err;
75
- });
76
- }
77
- };
package/src/loadEnv.ts DELETED
@@ -1,21 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import dotenv from 'dotenv';
4
- import dotenvExpand from 'dotenv-expand';
5
-
6
- export const loadEnv = (
7
- appDirectory: string,
8
- mode: string = process.env.NODE_ENV as string,
9
- ) => {
10
- [`.env.${mode}`, '.env']
11
- .map(name => path.resolve(appDirectory, name))
12
- .filter(
13
- filePath =>
14
- fs.existsSync(filePath) && !fs.statSync(filePath).isDirectory(),
15
- )
16
- .forEach(filePath => {
17
- const envConfig = dotenv.config({ path: filePath });
18
-
19
- dotenvExpand(envConfig);
20
- });
21
- };
@@ -1,81 +0,0 @@
1
- import {
2
- isDepExists,
3
- createDebugger,
4
- compatRequire,
5
- INTERNAL_PLUGINS,
6
- } from '@modern-js/utils';
7
-
8
- const debug = createDebugger('load-plugins');
9
-
10
- export interface PluginConfigItem {
11
- cli?: string;
12
- server?: string;
13
- }
14
-
15
- export type PluginConfig = Array<PluginConfigItem>;
16
-
17
- /**
18
- * Try to resolve plugin entry file path.
19
- * @param appDirectory - Application root directory.
20
- * @param plugin - Plugin name or plugin name with options.
21
- * @returns Resolved file path.
22
- */
23
- const resolvePlugin = (appDirectory: string, plugin: PluginConfigItem) => {
24
- const tryResolve = (name: string) => {
25
- let filePath = '';
26
- try {
27
- filePath = require.resolve(name, { paths: [appDirectory] });
28
- delete require.cache[filePath];
29
- } catch (err) {
30
- if ((err as any).code === 'MODULE_NOT_FOUND') {
31
- throw new Error(`Can not find plugin ${name}.`);
32
- }
33
- throw err;
34
- }
35
- return filePath;
36
- };
37
-
38
- const resolved: PluginConfigItem = {};
39
-
40
- if (plugin.cli) {
41
- resolved.cli = tryResolve(plugin.cli);
42
- }
43
-
44
- if (plugin.server) {
45
- resolved.server = tryResolve(plugin.server);
46
- }
47
-
48
- return resolved;
49
- };
50
-
51
- /**
52
- * Load internal plugins which in @modern-js scope and user's custom plugins.
53
- * @param appDirectory - Application root directory.
54
- * @param pluginsConfig - Plugins declared in the user configuration.
55
- * @returns Plugin Objects has been required.
56
- */
57
- export const loadPlugins = (
58
- appDirectory: string,
59
- pluginConfig: PluginConfig,
60
- ) => {
61
- const plugins = [
62
- ...Object.keys(INTERNAL_PLUGINS)
63
- .filter(name => isDepExists(appDirectory, name))
64
- .map(name => INTERNAL_PLUGINS[name]),
65
- ...pluginConfig,
66
- ];
67
-
68
- return plugins.map(plugin => {
69
- const { cli, server } = resolvePlugin(appDirectory, plugin);
70
-
71
- debug(`resolve plugin %s: %s`, plugin, {
72
- cli,
73
- server,
74
- });
75
-
76
- return {
77
- cli: cli && compatRequire(cli),
78
- server: server && compatRequire(server),
79
- };
80
- });
81
- };
package/src/types.d.ts DELETED
File without changes
@@ -1,22 +0,0 @@
1
- import { program, Command } from 'commander';
2
-
3
- declare module 'commander' {
4
- interface Command {
5
- commandsMap: Map<string, Command>;
6
- }
7
- }
8
-
9
- if (!program.hasOwnProperty('commandsMap')) {
10
- Object.defineProperty(program, 'commandsMap', {
11
- get() {
12
- const map = new Map<string, Command>();
13
- for (const command of program.commands) {
14
- map.set((command as any)._name, command);
15
- }
16
- return map;
17
- },
18
- configurable: false,
19
- });
20
- }
21
-
22
- export { program, Command };
@@ -1,29 +0,0 @@
1
- import { UserConfig } from '../config';
2
- import { traverseSchema } from '../config/schema';
3
-
4
- export const deepGet = (obj: any, key: string) => {
5
- for (const p of key.split('.')) {
6
- // eslint-disable-next-line no-param-reassign
7
- obj = obj ? obj[p] : undefined;
8
- }
9
- return obj;
10
- };
11
-
12
- export const repeatKeyWarning = (
13
- schema: any,
14
- jsConfig: UserConfig,
15
- pkgConfig: UserConfig,
16
- ) => {
17
- const keys = traverseSchema(schema);
18
-
19
- for (const key of keys) {
20
- if (
21
- deepGet(jsConfig, key) !== undefined &&
22
- deepGet(pkgConfig, key) !== undefined
23
- ) {
24
- throw new Error(
25
- `The same configuration ${key} exists in modern.config.js and package.json.\n Please remove it from package.json.`,
26
- );
27
- }
28
- }
29
- };
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['@modern-js'],
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
@@ -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,100 +0,0 @@
1
- import fs from 'fs';
2
- import { exec } from 'child_process';
3
- import path from 'path';
4
- import { loadEnv } from '@/loadEnv';
5
-
6
- const fixture = path.resolve(__dirname, './fixtures/load-env');
7
-
8
- fs.mkdirSync(fixture);
9
-
10
- const createFixtures = (
11
- dir: string,
12
- files: {
13
- name: string;
14
- content: string;
15
- }[],
16
- ) => {
17
- fs.mkdirSync(path.join(fixture, dir));
18
- for (const { name, content } of files) {
19
- fs.writeFileSync(path.join(fixture, dir, name), content, 'utf8');
20
- }
21
- };
22
-
23
- describe('load environment variables', () => {
24
- const defaultEnv = process.env;
25
-
26
- beforeEach(() => (process.env = { ...defaultEnv }));
27
-
28
- afterAll(() => {
29
- process.env = { ...defaultEnv };
30
- exec(`rm -rf ${fixture}`);
31
- });
32
-
33
- test(`support .env file`, () => {
34
- createFixtures('base', [
35
- {
36
- name: '.env',
37
- content: `DB_HOST=localhost
38
- DB_USER=root
39
- DB_PASS=root
40
- `,
41
- },
42
- ]);
43
- loadEnv(path.join(fixture, 'base'));
44
-
45
- expect(process.env.DB_HOST).toBe('localhost');
46
-
47
- expect(process.env.DB_USER).toBe('root');
48
-
49
- expect(process.env.DB_PASS).toBe('root');
50
- });
51
-
52
- test(`support "environment" .env file`, () => {
53
- createFixtures('environment', [
54
- {
55
- name: '.env',
56
- content: `DB_HOST=localhost
57
- DB_USER=root
58
- DB_PASS=root
59
- `,
60
- },
61
- {
62
- name: '.env.production',
63
- content: `DB_HOST=localhost
64
- DB_USER=root-local-dev
65
- `,
66
- },
67
- ]);
68
-
69
- process.env.NODE_ENV = 'production';
70
-
71
- loadEnv(path.join(fixture, 'environment'));
72
-
73
- expect(process.env.DB_HOST).toBe('localhost');
74
-
75
- expect(process.env.DB_USER).toBe('root-local-dev');
76
-
77
- expect(process.env.DB_PASS).toBe('root');
78
-
79
- delete process.env.NODE_ENV;
80
- });
81
-
82
- test(`support dotenv-expand`, () => {
83
- createFixtures('expand', [
84
- {
85
- name: '.env',
86
- content: `DB_HOST=localhost
87
- DB_USER=\${DB_HOST}001
88
- DB_PASS=root
89
- `,
90
- },
91
- ]);
92
- loadEnv(path.join(fixture, 'expand'));
93
-
94
- expect(process.env.DB_HOST).toBe('localhost');
95
-
96
- expect(process.env.DB_USER).toBe('localhost001');
97
-
98
- expect(process.env.DB_PASS).toBe('root');
99
- });
100
- });
@@ -1,29 +0,0 @@
1
- import path from 'path';
2
- import { loadPlugins } from '@/loadPlugins';
3
-
4
- describe('load plugins', () => {
5
- test('should load user plugin successfully', () => {
6
- const fixture = path.resolve(
7
- __dirname,
8
- './fixtures/load-plugin/user-plugins',
9
- );
10
-
11
- const plugins = loadPlugins(fixture, [
12
- { cli: path.join(fixture, './test-plugin-a.js') },
13
- { server: './test-plugin-b' },
14
- ]);
15
-
16
- expect(plugins).toEqual([
17
- { cli: { name: 'a' } },
18
- { server: { name: 'b' } },
19
- ]);
20
- });
21
-
22
- test(`should throw error when plugin not found `, () => {
23
- const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
24
-
25
- expect(() => {
26
- loadPlugins(fixture, [{ cli: './test-plugin-a' }, { cli: './plugin-b' }]);
27
- }).toThrowError(/^Can not find plugin /);
28
- });
29
- });