@modern-js/module-tools 1.4.2 → 1.4.5

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 (71) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/dist/js/modern/cli/build.js +2 -2
  3. package/dist/js/modern/cli/dev.js +2 -2
  4. package/dist/js/modern/commands/build.js +4 -5
  5. package/dist/js/modern/commands/dev.js +6 -7
  6. package/dist/js/modern/features/build/build-platform.js +4 -5
  7. package/dist/js/modern/features/build/build-watch.js +6 -8
  8. package/dist/js/modern/features/build/build.js +7 -11
  9. package/dist/js/modern/features/build/index.js +5 -5
  10. package/dist/js/modern/features/build/utils.js +9 -10
  11. package/dist/js/modern/features/dev/index.js +10 -9
  12. package/dist/js/modern/hooks/build.js +20 -0
  13. package/dist/js/modern/hooks/dev.js +11 -0
  14. package/dist/js/modern/hooks/index.js +14 -0
  15. package/dist/js/modern/index.js +37 -37
  16. package/dist/js/modern/tasks/build-source-code.js +1 -2
  17. package/dist/js/modern/tasks/build-style.js +2 -3
  18. package/dist/js/modern/tasks/build-watch-style.js +3 -6
  19. package/dist/js/modern/tasks/copy-assets.js +1 -2
  20. package/dist/js/modern/tasks/generator-dts/index.js +8 -11
  21. package/dist/js/modern/tasks/generator-dts/utils.js +11 -3
  22. package/dist/js/modern/utils/babel.js +2 -19
  23. package/dist/js/modern/utils/copy.js +1 -2
  24. package/dist/js/modern/utils/logger.js +1 -2
  25. package/dist/js/node/cli/build.js +2 -2
  26. package/dist/js/node/cli/dev.js +2 -2
  27. package/dist/js/node/commands/build.js +4 -6
  28. package/dist/js/node/commands/dev.js +6 -8
  29. package/dist/js/node/features/build/build-platform.js +5 -7
  30. package/dist/js/node/features/build/build-watch.js +6 -10
  31. package/dist/js/node/features/build/build.js +6 -13
  32. package/dist/js/node/features/build/index.js +5 -5
  33. package/dist/js/node/features/build/utils.js +9 -11
  34. package/dist/js/node/features/dev/index.js +11 -14
  35. package/dist/js/node/hooks/build.js +37 -0
  36. package/dist/js/node/hooks/dev.js +25 -0
  37. package/dist/js/node/hooks/index.js +39 -0
  38. package/dist/js/node/index.js +50 -42
  39. package/dist/js/node/tasks/build-source-code.js +3 -3
  40. package/dist/js/node/tasks/build-style.js +2 -4
  41. package/dist/js/node/tasks/build-watch-style.js +3 -7
  42. package/dist/js/node/tasks/copy-assets.js +1 -3
  43. package/dist/js/node/tasks/generator-dts/index.js +8 -16
  44. package/dist/js/node/tasks/generator-dts/utils.js +18 -8
  45. package/dist/js/node/utils/babel.js +1 -18
  46. package/dist/js/node/utils/copy.js +1 -2
  47. package/dist/js/node/utils/logger.js +5 -7
  48. package/dist/types/cli/build.d.ts +3 -2
  49. package/dist/types/cli/dev.d.ts +3 -2
  50. package/dist/types/cli/new.d.ts +1 -1
  51. package/dist/types/commands/build.d.ts +2 -1
  52. package/dist/types/commands/dev.d.ts +2 -1
  53. package/dist/types/features/build/build-platform.d.ts +2 -1
  54. package/dist/types/features/build/build-watch.d.ts +2 -2
  55. package/dist/types/features/build/build.d.ts +2 -2
  56. package/dist/types/features/build/index.d.ts +2 -2
  57. package/dist/types/features/build/utils.d.ts +4 -3
  58. package/dist/types/features/dev/index.d.ts +4 -3
  59. package/dist/types/hooks/build.d.ts +46 -0
  60. package/dist/types/hooks/dev.d.ts +18 -0
  61. package/dist/types/hooks/index.d.ts +26 -0
  62. package/dist/types/index.d.ts +3 -19
  63. package/dist/types/tasks/generator-dts/utils.d.ts +2 -1
  64. package/dist/types/utils/babel.d.ts +2 -3
  65. package/jest.config.js +0 -3
  66. package/package.json +19 -22
  67. package/tests/dev-cli.test.ts +43 -9
  68. package/tests/dev-command.test.ts +36 -10
  69. package/tests/dev-feature.test.ts +45 -13
  70. package/tests/generate-dts.test.ts +9 -0
  71. package/tests/generator-dts.test.ts +0 -11
@@ -1,8 +1,7 @@
1
- import { TransformOptions } from '@babel/core';
2
- import { NormalizedConfig } from '@modern-js/core';
1
+ import type { NormalizedConfig } from '@modern-js/core';
3
2
  import { IPackageModeValue } from '../types';
4
3
  export declare const getFinalAlias: any;
5
4
  export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: Pick<IPackageModeValue, 'syntax' | 'type'> & {
6
5
  sourceAbsDir: string;
7
6
  tsconfigPath: string;
8
- }) => TransformOptions;
7
+ }) => import("@modern-js/core").TransformOptions;
package/jest.config.js CHANGED
@@ -1,10 +1,7 @@
1
1
  const sharedConfig = require('@scripts/jest-config');
2
2
 
3
- console.info(sharedConfig.coveragePathIgnorePatterns);
4
-
5
3
  /** @type {import('@jest/types').Config.InitialOptions} */
6
4
  module.exports = {
7
- // eslint-disable-next-line node/no-unsupported-features/es-syntax
8
5
  ...sharedConfig,
9
6
  rootDir: __dirname,
10
7
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.2",
14
+ "version": "1.4.5",
15
15
  "bin": {
16
16
  "modern": "./bin/modern.js"
17
17
  },
@@ -40,39 +40,38 @@
40
40
  },
41
41
  "typesVersions": {
42
42
  "*": {
43
+ "types": [
44
+ "./lib/types.d.ts"
45
+ ],
43
46
  "type": [
44
47
  "./lib/types.d.ts"
45
48
  ]
46
49
  }
47
50
  },
48
51
  "dependencies": {
49
- "@modern-js/core": "^1.4.4",
50
52
  "@babel/generator": "^7.15.0",
51
53
  "@babel/parser": "^7.15.2",
52
54
  "@babel/runtime": "^7",
53
55
  "@babel/traverse": "^7.15.0",
54
- "@babel/types": "^7.15.0",
56
+ "@babel/types": "^7.17.0",
55
57
  "@modern-js/babel-compiler": "^1.2.2",
56
58
  "@modern-js/babel-preset-module": "^1.3.1",
57
- "@modern-js/css-config": "^1.2.2",
59
+ "@modern-js/core": "^1.7.0",
60
+ "@modern-js/css-config": "^1.2.3",
58
61
  "@modern-js/i18n-cli-language-detector": "^1.2.1",
59
- "@modern-js/module-tools-hooks": "^1.2.2",
60
- "@modern-js/new-action": "^1.3.3",
61
- "@modern-js/plugin-analyze": "^1.3.3",
62
- "@modern-js/plugin-changeset": "^1.2.2",
63
- "@modern-js/plugin-fast-refresh": "^1.2.1",
64
- "@modern-js/plugin-i18n": "^1.2.1",
65
- "@modern-js/style-compiler": "^1.2.2",
66
- "@modern-js/utils": "^1.3.3",
67
- "chalk": "^4.1.2",
62
+ "@modern-js/new-action": "^1.3.5",
63
+ "@modern-js/plugin": "^1.3.2",
64
+ "@modern-js/plugin-analyze": "^1.3.6",
65
+ "@modern-js/plugin-changeset": "^1.2.4",
66
+ "@modern-js/plugin-fast-refresh": "^1.2.3",
67
+ "@modern-js/plugin-i18n": "^1.2.3",
68
+ "@modern-js/style-compiler": "^1.2.3",
69
+ "@modern-js/utils": "^1.4.0",
68
70
  "dotenv": "^10.0.0",
69
- "execa": "^5.1.1",
70
71
  "fast-glob": "^3.2.5",
71
- "glob": "^7.1.7",
72
72
  "globby": "^11",
73
73
  "inquirer": "^8.1.5",
74
74
  "json5": "^2.2.0",
75
- "lodash.merge": "^4.6.2",
76
75
  "normalize-path": "^3.0.0",
77
76
  "p-map": "^4",
78
77
  "process.argv": "^0.6.0",
@@ -81,22 +80,20 @@
81
80
  "devDependencies": {
82
81
  "@babel/preset-typescript": "^7.15.0",
83
82
  "@modern-js/babel-chain": "^1.2.1",
83
+ "@scripts/build": "0.0.0",
84
+ "@scripts/jest-config": "0.0.0",
84
85
  "@types/babel__core": "^7.1.15",
85
86
  "@types/babel__generator": "^7.6.3",
86
87
  "@types/babel__traverse": "^7.14.2",
87
88
  "@types/glob": "^7.1.4",
88
89
  "@types/inquirer": "^8.1.1",
89
90
  "@types/jest": "^26",
90
- "@types/lodash.merge": "^4.6.6",
91
91
  "@types/node": "^14",
92
92
  "@types/normalize-path": "^3.0.0",
93
- "commander": "^8.1.0",
94
- "typescript": "^4",
95
- "@scripts/build": "0.0.0",
96
- "jest": "^27",
97
93
  "@types/react": "^17",
98
94
  "@types/react-dom": "^17",
99
- "@scripts/jest-config": "0.0.0"
95
+ "jest": "^27",
96
+ "typescript": "^4"
100
97
  },
101
98
  "sideEffects": false,
102
99
  "modernConfig": {
@@ -1,4 +1,5 @@
1
- import { program } from 'commander';
1
+ import { manager } from '@modern-js/core';
2
+ import { program } from '@modern-js/utils';
2
3
  import { devCli } from '../src/cli/dev';
3
4
 
4
5
  const mockCommandDev = jest.fn();
@@ -11,15 +12,48 @@ describe('dev cli subCmd', () => {
11
12
  dev: mockCommandDev,
12
13
  }));
13
14
  });
14
- it('should be storybook with "dev storybook"', () => {
15
- devCli(program);
16
- program.parse(['', '', 'dev', 'storybook']);
17
- expect(mockCommandDev.mock.calls[0][1]).toBe('storybook');
15
+ it('should be storybook with "dev storybook"', async () => {
16
+ const mockAPI = {
17
+ useAppContext: jest.fn((): any => ({
18
+ existSrc: false,
19
+ distDirectory: '',
20
+ })),
21
+ useResolvedConfigContext: jest.fn(),
22
+ useHookRunners: (): any => ({}),
23
+ };
24
+ const cloned = manager.clone(mockAPI);
25
+ cloned.usePlugin({
26
+ setup(api) {
27
+ devCli(program, api);
28
+ program.parse(['', '', 'dev', 'storybook']);
29
+ expect(mockCommandDev.mock.calls[0][2]).toBe('storybook');
30
+ },
31
+ });
32
+ await cloned.init();
18
33
  });
19
34
 
20
- it('should be undefined with "dev"', () => {
21
- devCli(program);
22
- program.parse(['', '', 'dev']);
23
- expect(mockCommandDev.mock.calls[0][1]).toBe(undefined);
35
+ it('should be undefined with "dev"', async () => {
36
+ const mockBeforeBuild = jest.fn();
37
+ const mockAfterBuild = jest.fn();
38
+ const mockAPI = {
39
+ useAppContext: jest.fn((): any => ({
40
+ existSrc: false,
41
+ distDirectory: '',
42
+ })),
43
+ useResolvedConfigContext: jest.fn(),
44
+ useHookRunners: (): any => ({
45
+ afterBuild: mockAfterBuild,
46
+ beforeBuild: mockBeforeBuild,
47
+ }),
48
+ };
49
+ const cloned = manager.clone(mockAPI);
50
+ cloned.usePlugin({
51
+ setup(api) {
52
+ devCli(program, api);
53
+ program.parse(['', '', 'dev']);
54
+ expect(mockCommandDev.mock.calls[0][2]).toBe(undefined);
55
+ },
56
+ });
57
+ await cloned.init();
24
58
  });
25
59
  });
@@ -1,3 +1,4 @@
1
+ import { manager } from '@modern-js/core';
1
2
  import { dev } from '../src/commands/dev';
2
3
 
3
4
  const mockRunSubCmd = jest.fn();
@@ -6,11 +7,6 @@ jest.mock('../src/features/dev', () => ({
6
7
  runSubCmd: mockRunSubCmd,
7
8
  devStorybook: jest.fn(),
8
9
  }));
9
- jest.mock('@modern-js/core', () => ({
10
- __esModule: true,
11
- useAppContext: jest.fn(() => ({ appDirectory: '' })),
12
- useResolvedConfigContext: jest.fn(),
13
- }));
14
10
 
15
11
  jest.mock('dotenv', () => ({
16
12
  __esModule: true,
@@ -32,13 +28,43 @@ describe('dev command with subCmd', () => {
32
28
  jest.clearAllMocks();
33
29
  });
34
30
  it('should call runSubCmd with storybook param', async () => {
35
- await dev({ tsconfig: 'tsconfig.json' }, 'storybook');
36
- expect(mockRunSubCmd.mock.calls.length).toBe(1);
37
- expect(mockRunSubCmd.mock.calls[0][0]).toBe('storybook');
31
+ const mockAPI = {
32
+ useAppContext: jest.fn((): any => ({
33
+ existSrc: false,
34
+ distDirectory: '',
35
+ appDirectory: '',
36
+ })),
37
+ useResolvedConfigContext: jest.fn(),
38
+ };
39
+
40
+ const cloned = manager.clone(mockAPI);
41
+ cloned.usePlugin({
42
+ async setup(api) {
43
+ await dev(api, { tsconfig: 'tsconfig.json' }, 'storybook');
44
+ expect(mockRunSubCmd.mock.calls.length).toBe(1);
45
+ expect(mockRunSubCmd.mock.calls[0][1]).toBe('storybook');
46
+ },
47
+ });
48
+ await cloned.init();
38
49
  });
39
50
 
40
51
  it('should not call runSubCmd with nothing param', async () => {
41
- await dev({ tsconfig: 'tsconfig.json' });
42
- expect(mockRunSubCmd.mock.calls.length).toBe(0);
52
+ const mockAPI = {
53
+ useAppContext: jest.fn((): any => ({
54
+ existSrc: false,
55
+ distDirectory: '',
56
+ appDirectory: '',
57
+ })),
58
+ useResolvedConfigContext: jest.fn(),
59
+ };
60
+
61
+ const cloned = manager.clone(mockAPI);
62
+ cloned.usePlugin({
63
+ async setup(api) {
64
+ await dev(api, { tsconfig: 'tsconfig.json' });
65
+ expect(mockRunSubCmd.mock.calls.length).toBe(0);
66
+ },
67
+ });
68
+ await cloned.init();
43
69
  });
44
70
  });
@@ -1,14 +1,16 @@
1
+ import { manager } from '@modern-js/core';
1
2
  import { runSubCmd } from '../src/features/dev';
2
3
 
3
4
  const mockModuleToolsMenu = jest.fn();
4
5
  const mockDevMeta = jest.fn();
5
6
  const exit = jest.spyOn(process, 'exit').mockImplementation();
6
- jest.mock('@modern-js/core', () => ({
7
- __esModule: true,
8
- mountHook: jest.fn(() => ({
7
+ const mockAPI = {
8
+ useAppContext: jest.fn((): any => ({})),
9
+ useResolvedConfigContext: jest.fn(),
10
+ useHookRunners: (): any => ({
9
11
  moduleToolsMenu: mockModuleToolsMenu,
10
- })),
11
- }));
12
+ }),
13
+ };
12
14
 
13
15
  describe('dev feature with subCmd', () => {
14
16
  beforeEach(() => {
@@ -18,29 +20,59 @@ describe('dev feature with subCmd', () => {
18
20
  mockModuleToolsMenu.mockReturnValue([
19
21
  { value: 'storybook', runTask: mockDevMeta },
20
22
  ]);
21
- await runSubCmd('storybook', { isTsProject: true, appDirectory: '' });
22
- expect(mockDevMeta.mock.calls.length).toBe(1);
23
+ const cloned = manager.clone(mockAPI);
24
+ cloned.usePlugin({
25
+ async setup(api) {
26
+ await runSubCmd(api, 'storybook', {
27
+ isTsProject: true,
28
+ appDirectory: '',
29
+ });
30
+ expect(mockDevMeta.mock.calls.length).toBe(1);
31
+ },
32
+ });
33
+ await cloned.init();
23
34
  });
24
35
 
25
36
  it('should run task with "storybook" params when storybook plugin not exist', async () => {
26
37
  mockModuleToolsMenu.mockReturnValue([]);
27
- await runSubCmd('storybook', { isTsProject: true, appDirectory: '' });
28
- expect(exit).toHaveBeenCalled();
38
+ const cloned = manager.clone(mockAPI);
39
+ cloned.usePlugin({
40
+ async setup(api) {
41
+ await runSubCmd(api, 'storybook', {
42
+ isTsProject: true,
43
+ appDirectory: '',
44
+ });
45
+ expect(exit).toHaveBeenCalled();
46
+ },
47
+ });
48
+ await cloned.init();
29
49
  });
30
50
 
31
51
  it('should run task with alias name "story" params when storybook plugin exist', async () => {
32
52
  mockModuleToolsMenu.mockReturnValue([
33
53
  { value: 'storybook', aliasValues: ['story'], runTask: mockDevMeta },
34
54
  ]);
35
- await runSubCmd('story', { isTsProject: true, appDirectory: '' });
36
- expect(mockDevMeta.mock.calls.length).toBe(1);
55
+ const cloned = manager.clone(mockAPI);
56
+ cloned.usePlugin({
57
+ async setup(api) {
58
+ await runSubCmd(api, 'story', { isTsProject: true, appDirectory: '' });
59
+ expect(mockDevMeta.mock.calls.length).toBe(1);
60
+ },
61
+ });
62
+ await cloned.init();
37
63
  });
38
64
 
39
65
  it('should run task with alias name "story1" params when storybook plugin exist', async () => {
40
66
  mockModuleToolsMenu.mockReturnValue([
41
67
  { value: 'storybook', aliasValues: ['story'], runTask: mockDevMeta },
42
68
  ]);
43
- await runSubCmd('story1', { isTsProject: true, appDirectory: '' });
44
- expect(mockDevMeta.mock.calls.length).toBe(0);
69
+ const cloned = manager.clone(mockAPI);
70
+ cloned.usePlugin({
71
+ async setup(api) {
72
+ await runSubCmd(api, 'story1', { isTsProject: true, appDirectory: '' });
73
+ expect(mockDevMeta.mock.calls.length).toBe(0);
74
+ },
75
+ });
76
+ await cloned.init();
45
77
  });
46
78
  });
@@ -0,0 +1,9 @@
1
+ import { getTscBinPath } from '../src/tasks/generator-dts/utils';
2
+
3
+ describe('generate dts', () => {
4
+ test('should throw error when tsc bin is not exist', () => {
5
+ expect(() => {
6
+ getTscBinPath('/foo');
7
+ }).toThrowError();
8
+ });
9
+ });
@@ -9,17 +9,6 @@ jest.mock('@modern-js/core', () => ({
9
9
  },
10
10
  }));
11
11
  jest.mock('process.argv', () => () => (o: any) => ({ ...o, tsCheck: false }));
12
- jest.mock('execa', () => () => {
13
- // eslint-disable-next-line prefer-promise-reject-errors
14
- const fn = Promise.reject('error');
15
- (fn as any).stdout = {
16
- on: () => 0,
17
- };
18
- (fn as any).stderr = {
19
- on: () => 0,
20
- };
21
- return fn;
22
- });
23
12
  jest.mock('../src/tasks/generator-dts/utils');
24
13
  jest.mock('@modern-js/utils', () => {
25
14
  const originalModule = jest.requireActual('@modern-js/utils');