@modern-js/core 1.2.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/dist/js/modern/cli.js +29 -0
- package/dist/js/modern/config/index.js +11 -5
- package/dist/js/modern/context.js +24 -16
- package/dist/js/modern/index.js +19 -10
- package/dist/js/modern/loadPlugins.js +16 -2
- package/dist/js/node/cli.js +35 -0
- package/dist/js/node/config/index.js +11 -5
- package/dist/js/node/context.js +24 -16
- package/dist/js/node/index.js +28 -37
- package/dist/js/node/loadPlugins.js +17 -1
- package/dist/types/cli.d.ts +1 -0
- package/dist/types/config/index.d.ts +14 -13
- package/dist/types/context.d.ts +6 -1
- package/dist/types/index.d.ts +13 -7
- package/dist/types/loadPlugins.d.ts +5 -0
- package/jest.config.js +8 -0
- package/modern.config.js +0 -7
- package/package.json +17 -10
- package/tests/btsm.test.ts +20 -0
- package/tests/config.test.ts +137 -0
- package/tests/context.test.ts +63 -0
- package/tests/fixtures/index-test/package.json +3 -0
- package/tests/index.test.ts +74 -0
- package/tests/loadEnv.test.ts +1 -1
- package/tests/loadPlugin.test.ts +36 -1
- package/tests/mergeConfig.test.ts +1 -1
- package/tests/repeatKeyWarning.test.ts +2 -2
- package/tests/schema.test.ts +1 -1
- package/tests/tsconfig.json +1 -3
- package/tests/utils.test.ts +8 -0
- package/tsconfig.json +1 -3
- package/src/config/defaults.ts +0 -101
- package/src/config/index.ts +0 -297
- package/src/config/mergeConfig.ts +0 -69
- package/src/config/schema/deploy.ts +0 -17
- package/src/config/schema/index.ts +0 -116
- package/src/config/schema/output.ts +0 -65
- package/src/config/schema/server.ts +0 -106
- package/src/config/schema/source.ts +0 -34
- package/src/config/schema/tools.ts +0 -15
- package/src/context.ts +0 -46
- package/src/index.ts +0 -277
- package/src/initWatcher.ts +0 -77
- package/src/loadEnv.ts +0 -23
- package/src/loadPlugins.ts +0 -91
- package/src/types.d.ts +0 -0
- package/src/utils/commander.ts +0 -22
- package/src/utils/repeatKeyWarning.ts +0 -29
package/dist/types/index.d.ts
CHANGED
|
@@ -2,11 +2,10 @@ import { INTERNAL_PLUGINS } from '@modern-js/utils';
|
|
|
2
2
|
import { ParallelWorkflow, AsyncWorkflow, Progresses2Runners, AsyncWaterfall } from '@modern-js/plugin';
|
|
3
3
|
import type { Hooks } from '@modern-js/types';
|
|
4
4
|
import { Command } from './utils/commander';
|
|
5
|
-
import { defineConfig, loadUserConfig, UserConfig, ToolsConfig } from './config';
|
|
6
5
|
import { AppContext, ConfigContext, IAppContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './context';
|
|
7
6
|
import { NormalizedConfig } from './config/mergeConfig';
|
|
8
7
|
export type { Hooks };
|
|
9
|
-
export
|
|
8
|
+
export * from './config';
|
|
10
9
|
export * from '@modern-js/plugin';
|
|
11
10
|
export * from '@modern-js/plugin/node';
|
|
12
11
|
export declare type HooksRunner = Progresses2Runners<{
|
|
@@ -88,18 +87,25 @@ export declare const createPlugin: (initializer: import("@modern-js/plugin").Asy
|
|
|
88
87
|
beforeExit: AsyncWorkflow<void, void>;
|
|
89
88
|
} & import("@modern-js/plugin").ClearDraftProgress<Hooks>>;
|
|
90
89
|
export declare const usePlugins: (plugins: string[]) => void;
|
|
91
|
-
export {
|
|
92
|
-
export type { NormalizedConfig, IAppContext
|
|
93
|
-
declare const initAppDir: () => Promise<string>;
|
|
90
|
+
export { AppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, ConfigContext };
|
|
91
|
+
export type { NormalizedConfig, IAppContext };
|
|
92
|
+
declare const initAppDir: (cwd?: string | undefined) => Promise<string>;
|
|
94
93
|
export interface CoreOptions {
|
|
95
94
|
configFile?: string;
|
|
95
|
+
packageJsonConfig?: string;
|
|
96
96
|
plugins?: typeof INTERNAL_PLUGINS;
|
|
97
|
-
beforeUsePlugins
|
|
97
|
+
beforeUsePlugins?: (plugins: any, config: any) => {
|
|
98
98
|
cli: any;
|
|
99
99
|
cliPath: any;
|
|
100
100
|
server: any;
|
|
101
101
|
serverPath: any;
|
|
102
102
|
}[];
|
|
103
|
+
options?: {
|
|
104
|
+
srcDir?: string;
|
|
105
|
+
distDir?: string;
|
|
106
|
+
sharedDir?: string;
|
|
107
|
+
internalDir?: string;
|
|
108
|
+
};
|
|
103
109
|
}
|
|
104
110
|
export declare const cli: {
|
|
105
111
|
init: (argv?: string[], options?: CoreOptions | undefined) => Promise<{
|
|
@@ -110,4 +116,4 @@ export declare const cli: {
|
|
|
110
116
|
run: (argv: string[], options?: CoreOptions | undefined) => Promise<void>;
|
|
111
117
|
restart: () => Promise<void>;
|
|
112
118
|
};
|
|
113
|
-
export {
|
|
119
|
+
export { initAppDir, initAppContext };
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { INTERNAL_PLUGINS } from '@modern-js/utils';
|
|
1
2
|
export interface PluginConfigItem {
|
|
2
3
|
cli?: string;
|
|
3
4
|
server?: string;
|
|
4
5
|
}
|
|
5
6
|
export declare type PluginConfig = Array<PluginConfigItem>;
|
|
7
|
+
export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): {
|
|
8
|
+
cli?: string | undefined;
|
|
9
|
+
server?: string | undefined;
|
|
10
|
+
}[];
|
|
6
11
|
/**
|
|
7
12
|
* Load internal plugins which in @modern-js scope and user's custom plugins.
|
|
8
13
|
* @param appDirectory - Application root directory.
|
package/jest.config.js
ADDED
package/modern.config.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2
|
|
14
|
+
"version": "1.3.2",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -20,12 +20,16 @@
|
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
22
|
"node": {
|
|
23
|
+
"jsnext:source": "./src/index.ts",
|
|
23
24
|
"import": "./dist/js/modern/index.js",
|
|
24
25
|
"require": "./dist/js/node/index.js"
|
|
25
26
|
},
|
|
26
27
|
"default": "./dist/js/treeshaking/index.js"
|
|
27
28
|
},
|
|
28
|
-
"./bin":
|
|
29
|
+
"./bin": {
|
|
30
|
+
"jsnext:source": "./src/cli.ts",
|
|
31
|
+
"default": "./bin/modern-js.js"
|
|
32
|
+
}
|
|
29
33
|
},
|
|
30
34
|
"typesVersions": {
|
|
31
35
|
"*": {
|
|
@@ -38,9 +42,9 @@
|
|
|
38
42
|
"dependencies": {
|
|
39
43
|
"@babel/code-frame": "^7.14.5",
|
|
40
44
|
"@babel/runtime": "^7",
|
|
41
|
-
"@modern-js/load-config": "^1.
|
|
42
|
-
"@modern-js/plugin": "^1.1
|
|
43
|
-
"@modern-js/utils": "^1.
|
|
45
|
+
"@modern-js/load-config": "^1.2.1",
|
|
46
|
+
"@modern-js/plugin": "^1.2.1",
|
|
47
|
+
"@modern-js/utils": "^1.2.2",
|
|
44
48
|
"address": "^1.1.2",
|
|
45
49
|
"ajv": "^8.6.2",
|
|
46
50
|
"ajv-keywords": "^5.0.0",
|
|
@@ -56,8 +60,9 @@
|
|
|
56
60
|
"v8-compile-cache": "^2.3.0"
|
|
57
61
|
},
|
|
58
62
|
"devDependencies": {
|
|
63
|
+
"btsm": "2.2.2",
|
|
59
64
|
"@types/babel__code-frame": "^7.0.3",
|
|
60
|
-
"@modern-js/types": "^1.1
|
|
65
|
+
"@modern-js/types": "^1.2.1",
|
|
61
66
|
"@types/jest": "^26",
|
|
62
67
|
"@types/lodash.clonedeep": "^4.5.6",
|
|
63
68
|
"@types/lodash.mergewith": "^4.6.6",
|
|
@@ -66,8 +71,9 @@
|
|
|
66
71
|
"@types/react-dom": "^17",
|
|
67
72
|
"@types/signale": "^1.4.2",
|
|
68
73
|
"typescript": "^4",
|
|
69
|
-
"
|
|
70
|
-
"@
|
|
74
|
+
"jest": "^27",
|
|
75
|
+
"@scripts/build": "0.0.0",
|
|
76
|
+
"@scripts/jest-config": "0.0.0"
|
|
71
77
|
},
|
|
72
78
|
"sideEffects": false,
|
|
73
79
|
"modernConfig": {
|
|
@@ -77,13 +83,14 @@
|
|
|
77
83
|
},
|
|
78
84
|
"publishConfig": {
|
|
79
85
|
"registry": "https://registry.npmjs.org/",
|
|
80
|
-
"access": "public"
|
|
86
|
+
"access": "public",
|
|
87
|
+
"types": "./dist/types/index.d.ts"
|
|
81
88
|
},
|
|
82
89
|
"scripts": {
|
|
83
90
|
"new": "modern new",
|
|
84
91
|
"build": "modern build",
|
|
85
92
|
"dev": "modern build --watch",
|
|
86
|
-
"test": "
|
|
93
|
+
"test": "jest"
|
|
87
94
|
},
|
|
88
95
|
"readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
|
|
89
96
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
// import os from 'os';
|
|
3
|
+
import { isDev, getPort } from '@modern-js/utils';
|
|
4
|
+
import { resolveConfig } 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
|
+
// type TEST = Parameters<typeof resolveConfig>;
|
|
137
|
+
// type TypeC = TEST[1];
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { initAppContext } from '../src/context';
|
|
3
|
+
|
|
4
|
+
describe('context', () => {
|
|
5
|
+
it('initAppContext', () => {
|
|
6
|
+
const appDirectory = path.resolve(
|
|
7
|
+
__dirname,
|
|
8
|
+
'./fixtures/load-plugin/user-plugins',
|
|
9
|
+
);
|
|
10
|
+
const appContext = initAppContext(appDirectory, [], false);
|
|
11
|
+
expect(appContext).toEqual({
|
|
12
|
+
appDirectory,
|
|
13
|
+
configFile: false,
|
|
14
|
+
ip: expect.any(String),
|
|
15
|
+
port: 0,
|
|
16
|
+
packageName: expect.any(String),
|
|
17
|
+
srcDirectory: expect.any(String),
|
|
18
|
+
distDirectory: expect.any(String),
|
|
19
|
+
sharedDirectory: expect.any(String),
|
|
20
|
+
nodeModulesDirectory: expect.any(String),
|
|
21
|
+
internalDirectory: expect.any(String),
|
|
22
|
+
plugins: [],
|
|
23
|
+
htmlTemplates: {},
|
|
24
|
+
serverRoutes: [],
|
|
25
|
+
entrypoints: [],
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('custom AppContext', () => {
|
|
30
|
+
const appDirectory = path.resolve(
|
|
31
|
+
__dirname,
|
|
32
|
+
'./fixtures/load-plugin/user-plugins',
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const customOptions = {
|
|
36
|
+
srcDir: 'source',
|
|
37
|
+
distDir: 'dist',
|
|
38
|
+
sharedDir: 'myShared',
|
|
39
|
+
internalDir: 'myInternal',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const appContext = initAppContext(appDirectory, [], false, customOptions);
|
|
43
|
+
expect(appContext).toEqual({
|
|
44
|
+
appDirectory,
|
|
45
|
+
configFile: false,
|
|
46
|
+
ip: expect.any(String),
|
|
47
|
+
port: 0,
|
|
48
|
+
packageName: 'user-plugins',
|
|
49
|
+
srcDirectory: path.resolve(appDirectory, './source'),
|
|
50
|
+
distDirectory: 'dist',
|
|
51
|
+
sharedDirectory: path.resolve(appDirectory, './myShared'),
|
|
52
|
+
nodeModulesDirectory: expect.any(String),
|
|
53
|
+
internalDirectory: path.resolve(
|
|
54
|
+
appDirectory,
|
|
55
|
+
'./node_modules/myInternal',
|
|
56
|
+
),
|
|
57
|
+
plugins: [],
|
|
58
|
+
htmlTemplates: {},
|
|
59
|
+
serverRoutes: [],
|
|
60
|
+
entrypoints: [],
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { cli } 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
|
+
const options = {
|
|
66
|
+
beforeUsePlugins: jest.fn(),
|
|
67
|
+
};
|
|
68
|
+
options.beforeUsePlugins.mockImplementation((plugins, _) => plugins);
|
|
69
|
+
await cli.init(['dev'], options);
|
|
70
|
+
expect(loadEnv).toHaveBeenCalledWith(cwd);
|
|
71
|
+
expect(options.beforeUsePlugins).toHaveBeenCalledWith([], {});
|
|
72
|
+
// TODO: add more test cases
|
|
73
|
+
});
|
|
74
|
+
});
|
package/tests/loadEnv.test.ts
CHANGED
package/tests/loadPlugin.test.ts
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { loadPlugins } from '
|
|
2
|
+
import { loadPlugins, getAppPlugins } from '../src/loadPlugins';
|
|
3
3
|
|
|
4
4
|
describe('load plugins', () => {
|
|
5
|
+
test('getAppPlugins', () => {
|
|
6
|
+
const appDirectory = path.resolve(
|
|
7
|
+
__dirname,
|
|
8
|
+
'./fixtures/load-plugin/user-plugins',
|
|
9
|
+
);
|
|
10
|
+
const plugins = getAppPlugins(appDirectory, ['foo' as any], {
|
|
11
|
+
x: {
|
|
12
|
+
cli: 'x',
|
|
13
|
+
forced: true,
|
|
14
|
+
} as any,
|
|
15
|
+
});
|
|
16
|
+
expect(plugins).toEqual([{ cli: 'x', forced: true }, 'foo']);
|
|
17
|
+
});
|
|
18
|
+
|
|
5
19
|
test('should load user plugin successfully', () => {
|
|
6
20
|
const fixture = path.resolve(
|
|
7
21
|
__dirname,
|
|
@@ -31,6 +45,27 @@ describe('load plugins', () => {
|
|
|
31
45
|
]);
|
|
32
46
|
});
|
|
33
47
|
|
|
48
|
+
test('should load user string plugin successfully', () => {
|
|
49
|
+
const fixture = path.resolve(
|
|
50
|
+
__dirname,
|
|
51
|
+
'./fixtures/load-plugin/user-plugins',
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const plugins = loadPlugins(fixture, [
|
|
55
|
+
path.join(fixture, './test-plugin-a.js') as any,
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
expect(plugins).toEqual([
|
|
59
|
+
{
|
|
60
|
+
cli: {
|
|
61
|
+
name: 'a',
|
|
62
|
+
pluginPath: path.join(fixture, './test-plugin-a.js'),
|
|
63
|
+
},
|
|
64
|
+
cliPath: path.join(fixture, './test-plugin-a.js'),
|
|
65
|
+
},
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
34
69
|
test(`should throw error when plugin not found `, () => {
|
|
35
70
|
const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
|
|
36
71
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { repeatKeyWarning } from '
|
|
2
|
-
import { UserConfig } from '
|
|
1
|
+
import { repeatKeyWarning } from '../src/utils/repeatKeyWarning';
|
|
2
|
+
import { UserConfig } from '../src/config';
|
|
3
3
|
|
|
4
4
|
jest.spyOn(process, 'exit').mockImplementation();
|
|
5
5
|
|
package/tests/schema.test.ts
CHANGED
package/tests/tsconfig.json
CHANGED