@modern-js/core 1.7.0 → 1.8.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 (39) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/js/modern/config/types/electron.js +1 -0
  3. package/dist/js/modern/context.js +1 -1
  4. package/dist/js/modern/index.js +2 -1
  5. package/dist/js/modern/loadPlugins.js +9 -4
  6. package/dist/js/modern/utils/commander.js +1 -1
  7. package/dist/js/node/config/types/electron.js +5 -0
  8. package/dist/js/node/context.js +1 -1
  9. package/dist/js/node/index.js +2 -1
  10. package/dist/js/node/loadPlugins.js +9 -4
  11. package/dist/types/config/types/electron.d.ts +13 -0
  12. package/dist/types/config/types/index.d.ts +14 -1
  13. package/dist/types/index.d.ts +9 -1
  14. package/dist/types/loadPlugins.d.ts +3 -0
  15. package/dist/types/manager.d.ts +8 -7
  16. package/dist/types/utils/commander.d.ts +2 -6
  17. package/package.json +7 -6
  18. package/tests/.eslintrc.js +0 -6
  19. package/tests/btsm.test.ts +0 -20
  20. package/tests/config.test.ts +0 -146
  21. package/tests/context.test.ts +0 -85
  22. package/tests/fixtures/index-test/modern.server-runtime.config.js +0 -0
  23. package/tests/fixtures/index-test/package.json +0 -3
  24. package/tests/fixtures/load-plugin/not-found/package.json +0 -3
  25. package/tests/fixtures/load-plugin/not-found/test-plugin-a.js +0 -1
  26. package/tests/fixtures/load-plugin/user-plugins/package.json +0 -3
  27. package/tests/fixtures/load-plugin/user-plugins/test-plugin-a.js +0 -1
  28. package/tests/fixtures/load-plugin/user-plugins/test-plugin-b.js +0 -3
  29. package/tests/fixtures/load-plugin/user-plugins/test-plugin-c.js +0 -3
  30. package/tests/index.test.ts +0 -83
  31. package/tests/initWatcher.test.ts +0 -63
  32. package/tests/loadEnv.test.ts +0 -173
  33. package/tests/loadPlugin.test.ts +0 -116
  34. package/tests/mergeConfig.test.ts +0 -97
  35. package/tests/pluginAPI.test.ts +0 -19
  36. package/tests/repeatKeyWarning.test.ts +0 -68
  37. package/tests/schema.test.ts +0 -107
  38. package/tests/tsconfig.json +0 -11
  39. package/tests/utils.test.ts +0 -7
@@ -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
- });
@@ -1,116 +0,0 @@
1
- import path from 'path';
2
- import { CliPlugin } from '../src';
3
- import { loadPlugins, getAppPlugins } from '../src/loadPlugins';
4
-
5
- describe('load plugins', () => {
6
- test('getAppPlugins', () => {
7
- const appDirectory = path.resolve(
8
- __dirname,
9
- './fixtures/load-plugin/user-plugins',
10
- );
11
- const plugins = getAppPlugins(appDirectory, ['foo' as any], {
12
- x: {
13
- cli: 'x',
14
- forced: true,
15
- } as any,
16
- });
17
- expect(plugins).toEqual([{ cli: 'x', forced: true }, 'foo']);
18
- });
19
-
20
- test('should load user plugin successfully', () => {
21
- const fixture = path.resolve(
22
- __dirname,
23
- './fixtures/load-plugin/user-plugins',
24
- );
25
-
26
- const plugins = loadPlugins(fixture, {
27
- plugins: [
28
- { cli: path.join(fixture, './test-plugin-a.js') },
29
- { server: './test-plugin-b' },
30
- ],
31
- });
32
-
33
- expect(plugins).toEqual([
34
- {
35
- cli: {
36
- name: 'a',
37
- },
38
- },
39
- {
40
- server: './test-plugin-b',
41
- serverPkg: './test-plugin-b',
42
- },
43
- ]);
44
- });
45
-
46
- test('should pass options to Plugin', () => {
47
- const fixture = path.resolve(
48
- __dirname,
49
- './fixtures/load-plugin/user-plugins',
50
- );
51
-
52
- const plugins = loadPlugins(fixture, {
53
- plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
54
- });
55
-
56
- expect(plugins[0].cli!.name).toEqual('c');
57
- expect(plugins[1].cli!.name).toEqual('c2');
58
- });
59
-
60
- test('should load user string plugin successfully', () => {
61
- const fixture = path.resolve(
62
- __dirname,
63
- './fixtures/load-plugin/user-plugins',
64
- );
65
-
66
- const plugins = loadPlugins(fixture, {
67
- plugins: [path.join(fixture, './test-plugin-a.js') as any],
68
- });
69
-
70
- expect(plugins).toEqual([
71
- {
72
- cli: {
73
- name: 'a',
74
- },
75
- },
76
- ]);
77
- });
78
-
79
- test(`should throw error when plugin not found `, () => {
80
- const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
81
-
82
- expect(() => {
83
- loadPlugins(fixture, {
84
- plugins: [{ cli: './test-plugin-a' }, { cli: './plugin-b' }],
85
- });
86
- }).toThrowError(/^Can not find plugin /);
87
- });
88
-
89
- test(`should load new plugin array correctly`, () => {
90
- const appDirectory = path.resolve(
91
- __dirname,
92
- './fixtures/load-plugin/user-plugins',
93
- );
94
- const plugin = (): CliPlugin => ({
95
- name: 'foo',
96
- });
97
- const userConfig = { plugins: [plugin()] };
98
- const loadedPlugins = loadPlugins(appDirectory, userConfig);
99
-
100
- expect(loadedPlugins[0].cli?.name).toEqual('foo');
101
- });
102
-
103
- test(`should load new plugin object correctly`, () => {
104
- const appDirectory = path.resolve(
105
- __dirname,
106
- './fixtures/load-plugin/user-plugins',
107
- );
108
- const plugin = (): CliPlugin => ({
109
- name: 'foo',
110
- });
111
- const userConfig = { plugins: { cli: [plugin()] } };
112
- const loadedPlugins = loadPlugins(appDirectory, userConfig);
113
-
114
- expect(loadedPlugins[0].cli?.name).toEqual('foo');
115
- });
116
- });
@@ -1,97 +0,0 @@
1
- import { mergeConfig } from '../src/config/mergeConfig';
2
-
3
- describe('load plugins', () => {
4
- test('should replace property deeply', () => {
5
- expect(
6
- mergeConfig([
7
- {
8
- source: { disableDefaultEntries: false },
9
- output: { polyfill: 'usage' },
10
- },
11
- {
12
- source: { disableDefaultEntries: true },
13
- output: { polyfill: 'entry' },
14
- },
15
- ]),
16
- ).toEqual({
17
- source: { disableDefaultEntries: true },
18
- output: { polyfill: 'entry' },
19
- });
20
- });
21
-
22
- test(`should set value when property value is not undefined `, () => {
23
- expect(
24
- mergeConfig([
25
- { source: { entries: { app: './App.tsx' } } },
26
- { source: { entries: { app2: './src/App2.tsx' } } },
27
- { server: { baseUrl: './a' } },
28
- ]),
29
- ).toEqual({
30
- source: {
31
- entries: {
32
- app: './App.tsx',
33
- app2: './src/App2.tsx',
34
- },
35
- },
36
- server: { baseUrl: './a' },
37
- });
38
- });
39
-
40
- test(`should ignore undefined property`, () => {
41
- const config = mergeConfig([
42
- { source: { entries: { app: './App.tsx' } } },
43
- { source: { entries: undefined } },
44
- { tools: { webpack: () => ({}) } },
45
- { tools: { webpack: undefined } },
46
- ]);
47
- expect(config.source).toEqual({
48
- entries: {
49
- app: './App.tsx',
50
- },
51
- });
52
- expect(Array.isArray(config.tools.webpack)).toBe(true);
53
- expect(config.tools.webpack?.length).toBe(1);
54
- });
55
-
56
- test(`should merge array value`, () => {
57
- expect(
58
- mergeConfig([
59
- { source: { envVars: ['a', 'b'] } },
60
- { source: { globalVars: { A: '1' } } },
61
- {
62
- source: {
63
- disableDefaultEntries: true,
64
- envVars: ['c', 'd'],
65
- globalVars: { B: '2' },
66
- },
67
- },
68
- ]),
69
- ).toEqual({
70
- source: {
71
- disableDefaultEntries: true,
72
- envVars: ['a', 'b', 'c', 'd'],
73
- globalVars: {
74
- A: '1',
75
- B: '2',
76
- },
77
- },
78
- });
79
- });
80
-
81
- test(`should merge function and object value`, () => {
82
- const config = mergeConfig([
83
- { source: { alias: { a: 'b' } } },
84
- { source: { alias: () => ({ c: 'd' }) } },
85
- { tools: { webpack: () => ({}) } },
86
- { tools: { webpack: { name: 'test' } } },
87
- { tools: { webpack: () => ({}) } },
88
- ]);
89
- expect(Array.isArray(config.source.alias)).toBe(true);
90
- expect(config?.source?.alias?.length).toBe(2);
91
- expect(typeof (config.source.alias as Array<any>)[1]).toBe('function');
92
- expect(Array.isArray(config.tools.webpack)).toBe(true);
93
- expect(config.tools.webpack?.length).toBe(3);
94
- expect(typeof (config.tools.webpack as Array<any>)[0]).toBe('function');
95
- expect(typeof (config.tools.webpack as Array<any>)[2]).toBe('function');
96
- });
97
- });
@@ -1,19 +0,0 @@
1
- import { CliPlugin, manager } from '../src';
2
-
3
- describe('pluginAPI', () => {
4
- it('api.setAppContext', done => {
5
- const plugin = (): CliPlugin => ({
6
- setup(api) {
7
- api.setAppContext({
8
- ...api.useAppContext(),
9
- packageName: 'foo',
10
- });
11
-
12
- expect(api.useAppContext().packageName).toEqual('foo');
13
- done();
14
- },
15
- });
16
-
17
- manager.clone().usePlugin(plugin).init();
18
- });
19
- });
@@ -1,68 +0,0 @@
1
- import { repeatKeyWarning } from '../src/utils/repeatKeyWarning';
2
- import { UserConfig } from '../src/config';
3
-
4
- jest.spyOn(process, 'exit').mockImplementation();
5
-
6
- describe('check repeated keys in user config', () => {
7
- test(`should exit with error`, () => {
8
- expect(() => {
9
- repeatKeyWarning(
10
- {
11
- type: 'object',
12
- properties: {
13
- a: {
14
- type: 'object',
15
- properties: {
16
- b: {
17
- type: 'object',
18
- properties: { c: { type: ['boolean', 'object'] } },
19
- },
20
- },
21
- },
22
- },
23
- },
24
- { a: { b: { c: true } } } as UserConfig,
25
- { a: { b: { c: false } } } as UserConfig,
26
- );
27
- }).toThrowError(
28
- 'The same configuration a.b.c exists in modern.config.js and package.json.',
29
- );
30
- });
31
-
32
- test(`should exit succcessfully`, () => {
33
- expect(
34
- repeatKeyWarning(
35
- {
36
- type: 'object',
37
- properties: {
38
- a: {
39
- type: 'object',
40
- properties: { b: { type: 'string' } },
41
- },
42
- },
43
- c: {
44
- type: 'object',
45
- proeperties: {
46
- d: {
47
- type: 'object',
48
- properties: { e: { type: 'string' } },
49
- },
50
- f: { type: 'string' },
51
- },
52
- },
53
- },
54
- {
55
- a: { b: 'name' },
56
- c: { d: { e: 's' } },
57
- } as UserConfig,
58
- {
59
- a: {},
60
- c: {
61
- d: {},
62
- f: 'ss',
63
- },
64
- } as UserConfig,
65
- ),
66
- ).toBeUndefined();
67
- });
68
- });