@modern-js/module-tools 1.4.0 → 1.4.3

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 (57) hide show
  1. package/CHANGELOG.md +63 -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 +5 -6
  5. package/dist/js/modern/commands/dev.js +6 -7
  6. package/dist/js/modern/features/build/build-platform.js +3 -3
  7. package/dist/js/modern/features/build/build-watch.js +5 -6
  8. package/dist/js/modern/features/build/build.js +5 -6
  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 +9 -7
  12. package/dist/js/modern/index.js +37 -37
  13. package/dist/js/modern/schema/output.js +0 -5
  14. package/dist/js/modern/tasks/build-source-code.js +2 -1
  15. package/dist/js/modern/tasks/build-watch-source-code.js +2 -1
  16. package/dist/js/modern/tasks/generator-dts/index.js +144 -0
  17. package/dist/js/modern/tasks/generator-dts/utils.js +82 -0
  18. package/dist/js/modern/utils/babel.js +10 -3
  19. package/dist/js/node/cli/build.js +2 -2
  20. package/dist/js/node/cli/dev.js +2 -2
  21. package/dist/js/node/commands/build.js +5 -7
  22. package/dist/js/node/commands/dev.js +6 -8
  23. package/dist/js/node/features/build/build-platform.js +3 -4
  24. package/dist/js/node/features/build/build-watch.js +5 -7
  25. package/dist/js/node/features/build/build.js +5 -7
  26. package/dist/js/node/features/build/index.js +5 -5
  27. package/dist/js/node/features/build/utils.js +9 -11
  28. package/dist/js/node/features/dev/index.js +9 -8
  29. package/dist/js/node/index.js +50 -42
  30. package/dist/js/node/schema/output.js +0 -5
  31. package/dist/js/node/tasks/build-source-code.js +2 -1
  32. package/dist/js/node/tasks/build-watch-source-code.js +2 -1
  33. package/dist/js/node/tasks/generator-dts/index.js +158 -0
  34. package/dist/js/node/tasks/{generator-dts.js → generator-dts/utils.js} +11 -151
  35. package/dist/js/node/utils/babel.js +9 -2
  36. package/dist/types/cli/build.d.ts +2 -1
  37. package/dist/types/cli/dev.d.ts +2 -1
  38. package/dist/types/commands/build.d.ts +2 -1
  39. package/dist/types/commands/dev.d.ts +2 -1
  40. package/dist/types/features/build/build-platform.d.ts +2 -1
  41. package/dist/types/features/build/build-watch.d.ts +2 -2
  42. package/dist/types/features/build/build.d.ts +2 -2
  43. package/dist/types/features/build/index.d.ts +2 -2
  44. package/dist/types/features/build/utils.d.ts +4 -3
  45. package/dist/types/features/dev/index.d.ts +4 -3
  46. package/dist/types/index.d.ts +3 -19
  47. package/dist/types/tasks/{generator-dts.d.ts → generator-dts/index.d.ts} +0 -0
  48. package/dist/types/tasks/generator-dts/utils.d.ts +23 -0
  49. package/jest.config.js +2 -0
  50. package/lib/types.d.ts +92 -0
  51. package/package.json +12 -13
  52. package/tests/dev-cli.test.ts +42 -8
  53. package/tests/dev-command.test.ts +36 -10
  54. package/tests/dev-feature.test.ts +45 -13
  55. package/tests/generator-dts-utils.test.ts +10 -0
  56. package/tests/generator-dts.test.ts +48 -0
  57. package/dist/js/modern/tasks/generator-dts.js +0 -226
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.0",
14
+ "version": "1.4.3",
15
15
  "bin": {
16
16
  "modern": "./bin/modern.js"
17
17
  },
@@ -46,26 +46,25 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
+ "@modern-js/core": "^1.6.0",
49
50
  "@babel/generator": "^7.15.0",
50
51
  "@babel/parser": "^7.15.2",
51
52
  "@babel/runtime": "^7",
52
53
  "@babel/traverse": "^7.15.0",
53
- "@babel/types": "^7.15.0",
54
- "@modern-js/babel-compiler": "^1.2.1",
54
+ "@babel/types": "^7.17.0",
55
+ "@modern-js/babel-compiler": "^1.2.2",
55
56
  "@modern-js/babel-preset-module": "^1.3.1",
56
- "@modern-js/core": "^1.4.2",
57
57
  "@modern-js/css-config": "^1.2.2",
58
58
  "@modern-js/i18n-cli-language-detector": "^1.2.1",
59
- "@modern-js/module-tools-hooks": "^1.2.1",
60
- "@modern-js/new-action": "^1.3.1",
61
- "@modern-js/plugin-analyze": "^1.3.2",
62
- "@modern-js/plugin-changeset": "^1.2.1",
63
- "@modern-js/plugin-fast-refresh": "^1.2.1",
59
+ "@modern-js/module-tools-hooks": "^1.2.3",
60
+ "@modern-js/new-action": "^1.3.3",
61
+ "@modern-js/plugin-analyze": "^1.3.4",
62
+ "@modern-js/plugin-changeset": "^1.2.3",
63
+ "@modern-js/plugin-fast-refresh": "^1.2.2",
64
64
  "@modern-js/plugin-i18n": "^1.2.1",
65
65
  "@modern-js/style-compiler": "^1.2.2",
66
- "@modern-js/utils": "^1.3.1",
66
+ "@modern-js/utils": "^1.3.6",
67
67
  "chalk": "^4.1.2",
68
- "chokidar": "^3.5.2",
69
68
  "dotenv": "^10.0.0",
70
69
  "execa": "^5.1.1",
71
70
  "fast-glob": "^3.2.5",
@@ -77,7 +76,6 @@
77
76
  "normalize-path": "^3.0.0",
78
77
  "p-map": "^4",
79
78
  "process.argv": "^0.6.0",
80
- "signale": "^1.4.0",
81
79
  "tsconfig-paths": "^3.10.1"
82
80
  },
83
81
  "devDependencies": {
@@ -92,11 +90,12 @@
92
90
  "@types/lodash.merge": "^4.6.6",
93
91
  "@types/node": "^14",
94
92
  "@types/normalize-path": "^3.0.0",
95
- "@types/signale": "^1.4.2",
96
93
  "commander": "^8.1.0",
97
94
  "typescript": "^4",
98
95
  "@scripts/build": "0.0.0",
99
96
  "jest": "^27",
97
+ "@types/react": "^17",
98
+ "@types/react-dom": "^17",
100
99
  "@scripts/jest-config": "0.0.0"
101
100
  },
102
101
  "sideEffects": false,
@@ -1,3 +1,4 @@
1
+ import { manager } from '@modern-js/core';
1
2
  import { program } from 'commander';
2
3
  import { devCli } from '../src/cli/dev';
3
4
 
@@ -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,10 @@
1
+ import * as path from 'path';
2
+ import { generatorTsConfig } from '../src/tasks/generator-dts/utils';
3
+
4
+ describe('test generator dts utils', () => {
5
+ it('test generatorTsConfig', () => {
6
+ const appDir = path.join(__dirname, './fixtures/tsconfig');
7
+ const ret = generatorTsConfig({}, { appDirectory: appDir, distDir: '' });
8
+ expect(ret).toContain('tsconfig.temp.json');
9
+ });
10
+ });
@@ -0,0 +1,48 @@
1
+ jest.mock('@modern-js/core', () => ({
2
+ manager: {
3
+ run: async (fn: any) => {
4
+ await fn();
5
+ },
6
+ },
7
+ cli: {
8
+ init: () => ({}),
9
+ },
10
+ }));
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
+ jest.mock('../src/tasks/generator-dts/utils');
24
+ jest.mock('@modern-js/utils', () => {
25
+ const originalModule = jest.requireActual('@modern-js/utils');
26
+ return {
27
+ __esModule: true, // Use it when dealing with esModules
28
+ ...originalModule,
29
+ fs: {
30
+ ...originalModule.fs,
31
+ removeSync: jest.fn(),
32
+ },
33
+ };
34
+ });
35
+ process.argv = [];
36
+
37
+ describe('generator dts test', () => {
38
+ it('test tsCheck is true', () => {
39
+ console.info = jest.fn(str => {
40
+ expect(str).toBe(
41
+ "There are some type warnings, which can be checked by configuring 'output.disableTsChecker = false'",
42
+ );
43
+ });
44
+ require('../src/tasks/generator-dts');
45
+ });
46
+ });
47
+
48
+ export {};
@@ -1,226 +0,0 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
-
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
-
5
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
-
7
- import * as path from 'path';
8
- import * as os from 'os';
9
- import { Import, fs } from '@modern-js/utils';
10
- const tsPathsTransform = Import.lazy('../utils/tspaths-transform', require);
11
- const babel = Import.lazy('../utils/babel', require);
12
- const constants = Import.lazy('./constants', require);
13
- const core = Import.lazy('@modern-js/core', require);
14
- const execa = Import.lazy('execa', require);
15
- const JSON5 = Import.lazy('json5', require);
16
- const argv = Import.lazy('process.argv', require);
17
- const deepMerge = Import.lazy('lodash.merge', require);
18
- const glob = Import.lazy('glob', require);
19
- let removeTsconfigPath = '';
20
-
21
- const generatorTsConfig = (projectTsconfig, {
22
- appDirectory,
23
- distDir,
24
- sourceDir: _sourceDir = 'src'
25
- }) => {
26
- var _projectTsconfig$comp, _projectTsconfig$comp2, _projectTsconfig$incl, _projectTsconfig$excl;
27
-
28
- const tempPath = path.resolve(appDirectory, './node_modules');
29
- const resolvePath = path.relative(tempPath, appDirectory); // const rootDir = projectTsconfig.compilerOptions?.rootDir
30
- // ? path.join(resolvePath, projectTsconfig.compilerOptions?.rootDir)
31
- // : resolvePath;
32
- // 目前限制 rootDir 的路径为 sourceDir
33
-
34
- const rootDir = path.join(resolvePath, _sourceDir);
35
- const baseUrl = (_projectTsconfig$comp = projectTsconfig.compilerOptions) !== null && _projectTsconfig$comp !== void 0 && _projectTsconfig$comp.baseUrl ? path.join(appDirectory, (_projectTsconfig$comp2 = projectTsconfig.compilerOptions) === null || _projectTsconfig$comp2 === void 0 ? void 0 : _projectTsconfig$comp2.baseUrl) : appDirectory; // if include = ['src'], final include should be ['../src']
36
-
37
- const include = (_projectTsconfig$incl = projectTsconfig.include) === null || _projectTsconfig$incl === void 0 ? void 0 : _projectTsconfig$incl.map(includePath => path.join(resolvePath, includePath));
38
- const exclude = (_projectTsconfig$excl = projectTsconfig.exclude) === null || _projectTsconfig$excl === void 0 ? void 0 : _projectTsconfig$excl.map(excludePath => path.join(resolvePath, excludePath));
39
- const resetConfig = {
40
- compilerOptions: {
41
- rootDir,
42
- baseUrl,
43
- // Ensure that .d.ts files are created by tsc, but not .js files
44
- declaration: true,
45
- emitDeclarationOnly: true,
46
- outDir: distDir
47
- },
48
- include,
49
- exclude
50
- };
51
- const recommendOption = {
52
- // Ensure that Babel can safely transpile files in the TypeScript project
53
- compilerOptions: {
54
- isolatedModules: true
55
- }
56
- };
57
- const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
58
- fs.ensureFileSync(tempTsconfigPath);
59
- fs.writeJSONSync(tempTsconfigPath, deepMerge(recommendOption, projectTsconfig, // 此处是必须要覆盖用户默认配置
60
- resetConfig));
61
- return tempTsconfigPath;
62
- };
63
-
64
- const getProjectTsconfig = tsconfigPath => {
65
- if (!tsconfigPath || !fs.existsSync(tsconfigPath)) {
66
- return {};
67
- }
68
-
69
- return JSON5.parse(fs.readFileSync(tsconfigPath, 'utf-8'));
70
- };
71
-
72
- const resolveLog = (childProgress, {
73
- tsCheck: _tsCheck = false,
74
- watch: _watch = false
75
- } = {}) => {
76
- var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
77
-
78
- /**
79
- * tsc 所有的log信息都是从stdout data 事件中获取
80
- * 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
81
- * watch 模式下,则使用这里的信息
82
- */
83
- (_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
84
- if (!_tsCheck) {
85
- return;
86
- }
87
-
88
- if (_watch) {
89
- console.info(data.toString());
90
- }
91
- }); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
92
-
93
- (_childProgress$stdout2 = childProgress.stdout) === null || _childProgress$stdout2 === void 0 ? void 0 : _childProgress$stdout2.on('error', error => {
94
- console.error(error.message);
95
- });
96
- (_childProgress$stderr = childProgress.stderr) === null || _childProgress$stderr === void 0 ? void 0 : _childProgress$stderr.on('data', chunk => {
97
- console.error(chunk.toString());
98
- });
99
- };
100
-
101
- const generatorDts = async (_, config) => {
102
- const {
103
- tsconfigPath,
104
- distDir,
105
- sourceDirName = 'src',
106
- projectData: {
107
- appDirectory
108
- },
109
- tsCheck = false,
110
- watch = false
111
- } = config;
112
- const userTsconfig = getProjectTsconfig(tsconfigPath);
113
- const willDeleteTsconfigPath = generatorTsConfig(userTsconfig, {
114
- appDirectory,
115
- distDir,
116
- sourceDir: sourceDirName
117
- });
118
- removeTsconfigPath = willDeleteTsconfigPath;
119
- const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
120
- const watchParams = watch ? ['-w'] : [];
121
- const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
122
- stdio: 'pipe',
123
- cwd: appDirectory
124
- });
125
- resolveLog(childProgress, {
126
- tsCheck,
127
- watch
128
- });
129
-
130
- try {
131
- await childProgress;
132
- console.info('[Tsc Compiler]: Successfully');
133
- } catch (e) {
134
- if (!tsCheck) {
135
- console.warn(`There are some type issues, which can be checked by configuring 'source.enableTsChecker = true' ${os.EOL} in modern.config.js`);
136
- } else {
137
- const isRecord = input => typeof input === 'object'; // 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
138
-
139
-
140
- if (isRecord(e)) {
141
- console.error(e.stdout);
142
- }
143
- }
144
- }
145
-
146
- fs.removeSync(willDeleteTsconfigPath);
147
- };
148
-
149
- const resolveAlias = (modernConfig, config, watchFilenames = []) => {
150
- const {
151
- output
152
- } = modernConfig;
153
- const defaultPaths = {
154
- '@': ['./src']
155
- };
156
- const dtsDistPath = `${config.distDir}/**/*.d.ts`;
157
- const dtsFilenames = watchFilenames.length > 0 ? watchFilenames : glob.sync(dtsDistPath, {
158
- absolute: true
159
- });
160
- const alias = babel.getFinalAlias(modernConfig, {
161
- appDirectory: config.projectData.appDirectory,
162
- tsconfigPath: config.tsconfigPath || path.join(config.projectData.appDirectory, './tsconfig.json'),
163
- sourceAbsDir: config.distDir
164
- });
165
- const mergedPaths = alias.isTsPath ? alias.paths || {} : _objectSpread(_objectSpread({}, defaultPaths), alias.paths || {});
166
- const result = tsPathsTransform.transformDtsAlias({
167
- filenames: dtsFilenames,
168
- baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
169
- paths: mergedPaths
170
- });
171
-
172
- for (const r of result) {
173
- fs.writeFileSync(r.path, r.content);
174
- }
175
- };
176
-
177
- const taskMain = async ({
178
- modernConfig
179
- }) => {
180
- const processArgv = argv(process.argv.slice(2));
181
- const config = processArgv({
182
- appDirectory: process.cwd(),
183
- srcDir: 'src',
184
- distDir: 'dist/types',
185
- tsconfigPath: './tsconfig.json',
186
- sourceDirName: 'src'
187
- });
188
- const option = {
189
- srcDir: config.srcDir,
190
- distDir: config.distDir,
191
- projectData: {
192
- appDirectory: config.appDirectory
193
- },
194
- tsconfigPath: config.tsconfigPath,
195
- watch: config.watch,
196
- tsCheck: config.tsCheck,
197
- sourceDirName: config.sourceDirName
198
- };
199
- await generatorDts(modernConfig, option); // TODO: watch 模式下无法转换
200
-
201
- resolveAlias(modernConfig, option);
202
- };
203
-
204
- (async () => {
205
- let options;
206
-
207
- if (process.env.CORE_INIT_OPTION_FILE) {
208
- ({
209
- options
210
- } = require(process.env.CORE_INIT_OPTION_FILE));
211
- }
212
-
213
- const {
214
- resolved
215
- } = await core.cli.init([], options);
216
- await core.manager.run(async () => {
217
- try {
218
- await taskMain({
219
- modernConfig: resolved
220
- });
221
- } catch (e) {
222
- console.error(e.message);
223
- fs.removeSync(removeTsconfigPath);
224
- }
225
- });
226
- })();