@modern-js/module-tools 1.3.1 → 1.4.1
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 +52 -0
- package/dist/js/modern/features/dev/index.js +1 -1
- package/dist/js/modern/schema/output.js +0 -5
- package/dist/js/modern/tasks/generator-dts/index.js +144 -0
- package/dist/js/modern/tasks/generator-dts/utils.js +82 -0
- package/dist/js/node/features/dev/index.js +1 -1
- package/dist/js/node/schema/output.js +0 -5
- package/dist/js/node/tasks/generator-dts/index.js +158 -0
- package/dist/js/node/tasks/{generator-dts.js → generator-dts/utils.js} +11 -151
- package/dist/types/index.d.ts +2 -0
- package/dist/types/tasks/{generator-dts.d.ts → generator-dts/index.d.ts} +0 -0
- package/dist/types/tasks/generator-dts/utils.d.ts +23 -0
- package/jest.config.js +2 -0
- package/lib/types.d.ts +0 -10
- package/package.json +13 -11
- package/tests/dev-feature.test.ts +17 -1
- package/tests/generator-dts-utils.test.ts +10 -0
- package/tests/generator-dts.test.ts +48 -0
- package/dist/js/modern/tasks/generator-dts.js +0 -226
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# @modern-js/module-tools
|
|
2
2
|
|
|
3
|
+
## 1.4.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 02ff289d: Modify the type of error text and display log messages
|
|
8
|
+
- 54786e58: add ts check
|
|
9
|
+
- 6668a1bf: feat: upgrade @modern-js/codesmith-api-app version
|
|
10
|
+
- fab92861: fix: @modern-js/core phantom dep
|
|
11
|
+
- Updated dependencies [deeaa602]
|
|
12
|
+
- Updated dependencies [54786e58]
|
|
13
|
+
- Updated dependencies [6668a1bf]
|
|
14
|
+
- Updated dependencies [6668a1bf]
|
|
15
|
+
- Updated dependencies [fab92861]
|
|
16
|
+
- @modern-js/plugin-analyze@1.3.3
|
|
17
|
+
- @modern-js/utils@1.3.2
|
|
18
|
+
- @modern-js/core@1.4.3
|
|
19
|
+
- @modern-js/new-action@1.3.2
|
|
20
|
+
- @modern-js/plugin-changeset@1.2.2
|
|
21
|
+
- @modern-js/module-tools-hooks@1.2.2
|
|
22
|
+
|
|
23
|
+
## 1.4.0
|
|
24
|
+
|
|
25
|
+
### Minor Changes
|
|
26
|
+
|
|
27
|
+
- 67503500: add alais subCmd
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Updated dependencies [118da5b4]
|
|
32
|
+
- Updated dependencies [b376c8d6]
|
|
33
|
+
- Updated dependencies [e62c4efd]
|
|
34
|
+
- Updated dependencies [6891e4c2]
|
|
35
|
+
- Updated dependencies [e2a8233f]
|
|
36
|
+
- @modern-js/css-config@1.2.2
|
|
37
|
+
- @modern-js/style-compiler@1.2.2
|
|
38
|
+
- @modern-js/core@1.4.2
|
|
39
|
+
- @modern-js/plugin-analyze@1.3.2
|
|
40
|
+
|
|
41
|
+
## 1.3.2
|
|
42
|
+
|
|
43
|
+
### Patch Changes
|
|
44
|
+
|
|
45
|
+
- c78400c7: fix: remove stylus support
|
|
46
|
+
- Updated dependencies [b7a9eeba]
|
|
47
|
+
- Updated dependencies [53aca274]
|
|
48
|
+
- Updated dependencies [78279953]
|
|
49
|
+
- Updated dependencies [e116ace5]
|
|
50
|
+
- Updated dependencies [4d72edea]
|
|
51
|
+
- @modern-js/plugin-analyze@1.3.1
|
|
52
|
+
- @modern-js/core@1.4.1
|
|
53
|
+
- @modern-js/utils@1.3.1
|
|
54
|
+
|
|
3
55
|
## 1.3.1
|
|
4
56
|
|
|
5
57
|
### Patch Changes
|
|
@@ -41,7 +41,7 @@ export const devStorybook = async config => {
|
|
|
41
41
|
};
|
|
42
42
|
export const runSubCmd = async (subCmd, config) => {
|
|
43
43
|
const metas = await core.mountHook().moduleToolsMenu(undefined);
|
|
44
|
-
const devMeta = metas.find(meta => meta.value === subCmd);
|
|
44
|
+
const devMeta = metas.find(meta => meta.value === subCmd || Array.isArray(meta.aliasValues) && meta.aliasValues.includes(subCmd));
|
|
45
45
|
|
|
46
46
|
if (devMeta) {
|
|
47
47
|
await devMeta.runTask(config);
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { Import, fs } from '@modern-js/utils';
|
|
3
|
+
const core = Import.lazy('@modern-js/core', require);
|
|
4
|
+
const execa = Import.lazy('execa', require);
|
|
5
|
+
const JSON5 = Import.lazy('json5', require);
|
|
6
|
+
const argv = Import.lazy('process.argv', require);
|
|
7
|
+
const utils = Import.lazy('./utils', require);
|
|
8
|
+
let removeTsconfigPath = '';
|
|
9
|
+
|
|
10
|
+
const getProjectTsconfig = tsconfigPath => {
|
|
11
|
+
if (!tsconfigPath || !fs.existsSync(tsconfigPath)) {
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return JSON5.parse(fs.readFileSync(tsconfigPath, 'utf-8'));
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const resolveLog = (childProgress, {
|
|
19
|
+
tsCheck: _tsCheck = false,
|
|
20
|
+
watch: _watch = false
|
|
21
|
+
} = {}) => {
|
|
22
|
+
var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* tsc 所有的log信息都是从stdout data 事件中获取
|
|
26
|
+
* 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
|
|
27
|
+
* watch 模式下,则使用这里的信息
|
|
28
|
+
*/
|
|
29
|
+
(_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
|
|
30
|
+
if (!_tsCheck) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (_watch) {
|
|
35
|
+
console.info(data.toString());
|
|
36
|
+
}
|
|
37
|
+
}); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
|
|
38
|
+
|
|
39
|
+
(_childProgress$stdout2 = childProgress.stdout) === null || _childProgress$stdout2 === void 0 ? void 0 : _childProgress$stdout2.on('error', error => {
|
|
40
|
+
console.error(error.message);
|
|
41
|
+
});
|
|
42
|
+
(_childProgress$stderr = childProgress.stderr) === null || _childProgress$stderr === void 0 ? void 0 : _childProgress$stderr.on('data', chunk => {
|
|
43
|
+
console.error(chunk.toString());
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const generatorDts = async (_, config) => {
|
|
48
|
+
const {
|
|
49
|
+
tsconfigPath,
|
|
50
|
+
distDir,
|
|
51
|
+
sourceDirName = 'src',
|
|
52
|
+
projectData: {
|
|
53
|
+
appDirectory
|
|
54
|
+
},
|
|
55
|
+
tsCheck = false,
|
|
56
|
+
watch = false
|
|
57
|
+
} = config;
|
|
58
|
+
const userTsconfig = getProjectTsconfig(tsconfigPath);
|
|
59
|
+
const willDeleteTsconfigPath = utils.generatorTsConfig(userTsconfig, {
|
|
60
|
+
appDirectory,
|
|
61
|
+
distDir,
|
|
62
|
+
sourceDir: sourceDirName
|
|
63
|
+
});
|
|
64
|
+
removeTsconfigPath = willDeleteTsconfigPath;
|
|
65
|
+
const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
|
|
66
|
+
const watchParams = watch ? ['-w'] : [];
|
|
67
|
+
const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
|
|
68
|
+
stdio: 'pipe',
|
|
69
|
+
cwd: appDirectory
|
|
70
|
+
});
|
|
71
|
+
resolveLog(childProgress, {
|
|
72
|
+
tsCheck,
|
|
73
|
+
watch
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await childProgress;
|
|
78
|
+
console.info('[Tsc Compiler]: Successfully');
|
|
79
|
+
} catch (e) {
|
|
80
|
+
if (!tsCheck) {
|
|
81
|
+
console.info(`There are some type warnings, which can be checked by configuring 'output.disableTsChecker = false'`);
|
|
82
|
+
} else {
|
|
83
|
+
const isRecord = input => typeof input === 'object'; // 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if (isRecord(e)) {
|
|
87
|
+
console.error(e.stdout);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fs.removeSync(willDeleteTsconfigPath);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const taskMain = async ({
|
|
96
|
+
modernConfig
|
|
97
|
+
}) => {
|
|
98
|
+
const processArgv = argv(process.argv.slice(2));
|
|
99
|
+
const config = processArgv({
|
|
100
|
+
appDirectory: process.cwd(),
|
|
101
|
+
srcDir: 'src',
|
|
102
|
+
distDir: 'dist/types',
|
|
103
|
+
tsconfigPath: './tsconfig.json',
|
|
104
|
+
sourceDirName: 'src'
|
|
105
|
+
});
|
|
106
|
+
const option = {
|
|
107
|
+
srcDir: config.srcDir,
|
|
108
|
+
distDir: config.distDir,
|
|
109
|
+
projectData: {
|
|
110
|
+
appDirectory: config.appDirectory
|
|
111
|
+
},
|
|
112
|
+
tsconfigPath: config.tsconfigPath,
|
|
113
|
+
watch: config.watch,
|
|
114
|
+
tsCheck: config.tsCheck,
|
|
115
|
+
sourceDirName: config.sourceDirName
|
|
116
|
+
};
|
|
117
|
+
await generatorDts(modernConfig, option); // // TODO: watch 模式下无法转换
|
|
118
|
+
|
|
119
|
+
utils.resolveAlias(modernConfig, option);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
(async () => {
|
|
123
|
+
let options;
|
|
124
|
+
|
|
125
|
+
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
126
|
+
({
|
|
127
|
+
options
|
|
128
|
+
} = require(process.env.CORE_INIT_OPTION_FILE));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const {
|
|
132
|
+
resolved
|
|
133
|
+
} = await core.cli.init([], options);
|
|
134
|
+
await core.manager.run(async () => {
|
|
135
|
+
try {
|
|
136
|
+
await taskMain({
|
|
137
|
+
modernConfig: resolved
|
|
138
|
+
});
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(e.message);
|
|
141
|
+
fs.removeSync(removeTsconfigPath);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
})();
|
|
@@ -0,0 +1,82 @@
|
|
|
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 { Import, fs } from '@modern-js/utils';
|
|
9
|
+
const glob = Import.lazy('glob', require);
|
|
10
|
+
const babel = Import.lazy('../../utils/babel', require);
|
|
11
|
+
const tsPathsTransform = Import.lazy('../../utils/tspaths-transform', require);
|
|
12
|
+
const constants = Import.lazy('../constants', require);
|
|
13
|
+
const deepMerge = Import.lazy('lodash.merge', require);
|
|
14
|
+
export const generatorTsConfig = (projectTsconfig, {
|
|
15
|
+
appDirectory,
|
|
16
|
+
distDir,
|
|
17
|
+
sourceDir: _sourceDir = 'src'
|
|
18
|
+
}) => {
|
|
19
|
+
var _projectTsconfig$comp, _projectTsconfig$comp2, _projectTsconfig$incl, _projectTsconfig$excl;
|
|
20
|
+
|
|
21
|
+
const tempPath = path.resolve(appDirectory, './node_modules');
|
|
22
|
+
const resolvePath = path.relative(tempPath, appDirectory); // const rootDir = projectTsconfig.compilerOptions?.rootDir
|
|
23
|
+
// ? path.join(resolvePath, projectTsconfig.compilerOptions?.rootDir)
|
|
24
|
+
// : resolvePath;
|
|
25
|
+
// 目前限制 rootDir 的路径为 sourceDir
|
|
26
|
+
|
|
27
|
+
const rootDir = path.join(resolvePath, _sourceDir);
|
|
28
|
+
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']
|
|
29
|
+
|
|
30
|
+
const include = (_projectTsconfig$incl = projectTsconfig.include) === null || _projectTsconfig$incl === void 0 ? void 0 : _projectTsconfig$incl.map(includePath => path.join(resolvePath, includePath));
|
|
31
|
+
const exclude = (_projectTsconfig$excl = projectTsconfig.exclude) === null || _projectTsconfig$excl === void 0 ? void 0 : _projectTsconfig$excl.map(excludePath => path.join(resolvePath, excludePath));
|
|
32
|
+
const resetConfig = {
|
|
33
|
+
compilerOptions: {
|
|
34
|
+
rootDir,
|
|
35
|
+
baseUrl,
|
|
36
|
+
// Ensure that .d.ts files are created by tsc, but not .js files
|
|
37
|
+
declaration: true,
|
|
38
|
+
emitDeclarationOnly: true,
|
|
39
|
+
outDir: distDir
|
|
40
|
+
},
|
|
41
|
+
include,
|
|
42
|
+
exclude
|
|
43
|
+
};
|
|
44
|
+
const recommendOption = {
|
|
45
|
+
// Ensure that Babel can safely transpile files in the TypeScript project
|
|
46
|
+
compilerOptions: {
|
|
47
|
+
isolatedModules: true
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const tempTsconfigPath = path.join(tempPath, constants.tempTsconfigName);
|
|
51
|
+
fs.ensureFileSync(tempTsconfigPath);
|
|
52
|
+
fs.writeJSONSync(tempTsconfigPath, deepMerge(recommendOption, projectTsconfig, // 此处是必须要覆盖用户默认配置
|
|
53
|
+
resetConfig));
|
|
54
|
+
return tempTsconfigPath;
|
|
55
|
+
};
|
|
56
|
+
export const resolveAlias = (modernConfig, config, watchFilenames = []) => {
|
|
57
|
+
const {
|
|
58
|
+
output
|
|
59
|
+
} = modernConfig;
|
|
60
|
+
const defaultPaths = {
|
|
61
|
+
'@': ['./src']
|
|
62
|
+
};
|
|
63
|
+
const dtsDistPath = `${config.distDir}/**/*.d.ts`;
|
|
64
|
+
const dtsFilenames = watchFilenames.length > 0 ? watchFilenames : glob.sync(dtsDistPath, {
|
|
65
|
+
absolute: true
|
|
66
|
+
});
|
|
67
|
+
const alias = babel.getFinalAlias(modernConfig, {
|
|
68
|
+
appDirectory: config.projectData.appDirectory,
|
|
69
|
+
tsconfigPath: config.tsconfigPath || path.join(config.projectData.appDirectory, './tsconfig.json'),
|
|
70
|
+
sourceAbsDir: config.distDir
|
|
71
|
+
});
|
|
72
|
+
const mergedPaths = alias.isTsPath ? alias.paths || {} : _objectSpread(_objectSpread({}, defaultPaths), alias.paths || {});
|
|
73
|
+
const result = tsPathsTransform.transformDtsAlias({
|
|
74
|
+
filenames: dtsFilenames,
|
|
75
|
+
baseUrl: path.join(config.projectData.appDirectory, output.path || 'dist'),
|
|
76
|
+
paths: mergedPaths
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
for (const r of result) {
|
|
80
|
+
fs.writeFileSync(r.path, r.content);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
@@ -61,7 +61,7 @@ exports.devStorybook = devStorybook;
|
|
|
61
61
|
|
|
62
62
|
const runSubCmd = async (subCmd, config) => {
|
|
63
63
|
const metas = await core.mountHook().moduleToolsMenu(undefined);
|
|
64
|
-
const devMeta = metas.find(meta => meta.value === subCmd);
|
|
64
|
+
const devMeta = metas.find(meta => meta.value === subCmd || Array.isArray(meta.aliasValues) && meta.aliasValues.includes(subCmd));
|
|
65
65
|
|
|
66
66
|
if (devMeta) {
|
|
67
67
|
await devMeta.runTask(config);
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var path = _interopRequireWildcard(require("path"));
|
|
4
|
+
|
|
5
|
+
var _utils = require("@modern-js/utils");
|
|
6
|
+
|
|
7
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
8
|
+
|
|
9
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
10
|
+
|
|
11
|
+
const core = _utils.Import.lazy('@modern-js/core', require);
|
|
12
|
+
|
|
13
|
+
const execa = _utils.Import.lazy('execa', require);
|
|
14
|
+
|
|
15
|
+
const JSON5 = _utils.Import.lazy('json5', require);
|
|
16
|
+
|
|
17
|
+
const argv = _utils.Import.lazy('process.argv', require);
|
|
18
|
+
|
|
19
|
+
const utils = _utils.Import.lazy('./utils', require);
|
|
20
|
+
|
|
21
|
+
let removeTsconfigPath = '';
|
|
22
|
+
|
|
23
|
+
const getProjectTsconfig = tsconfigPath => {
|
|
24
|
+
if (!tsconfigPath || !_utils.fs.existsSync(tsconfigPath)) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return JSON5.parse(_utils.fs.readFileSync(tsconfigPath, 'utf-8'));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const resolveLog = (childProgress, {
|
|
32
|
+
tsCheck: _tsCheck = false,
|
|
33
|
+
watch: _watch = false
|
|
34
|
+
} = {}) => {
|
|
35
|
+
var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* tsc 所有的log信息都是从stdout data 事件中获取
|
|
39
|
+
* 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
|
|
40
|
+
* watch 模式下,则使用这里的信息
|
|
41
|
+
*/
|
|
42
|
+
(_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
|
|
43
|
+
if (!_tsCheck) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (_watch) {
|
|
48
|
+
console.info(data.toString());
|
|
49
|
+
}
|
|
50
|
+
}); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
|
|
51
|
+
|
|
52
|
+
(_childProgress$stdout2 = childProgress.stdout) === null || _childProgress$stdout2 === void 0 ? void 0 : _childProgress$stdout2.on('error', error => {
|
|
53
|
+
console.error(error.message);
|
|
54
|
+
});
|
|
55
|
+
(_childProgress$stderr = childProgress.stderr) === null || _childProgress$stderr === void 0 ? void 0 : _childProgress$stderr.on('data', chunk => {
|
|
56
|
+
console.error(chunk.toString());
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const generatorDts = async (_, config) => {
|
|
61
|
+
const {
|
|
62
|
+
tsconfigPath,
|
|
63
|
+
distDir,
|
|
64
|
+
sourceDirName = 'src',
|
|
65
|
+
projectData: {
|
|
66
|
+
appDirectory
|
|
67
|
+
},
|
|
68
|
+
tsCheck = false,
|
|
69
|
+
watch = false
|
|
70
|
+
} = config;
|
|
71
|
+
const userTsconfig = getProjectTsconfig(tsconfigPath);
|
|
72
|
+
const willDeleteTsconfigPath = utils.generatorTsConfig(userTsconfig, {
|
|
73
|
+
appDirectory,
|
|
74
|
+
distDir,
|
|
75
|
+
sourceDir: sourceDirName
|
|
76
|
+
});
|
|
77
|
+
removeTsconfigPath = willDeleteTsconfigPath;
|
|
78
|
+
const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
|
|
79
|
+
const watchParams = watch ? ['-w'] : [];
|
|
80
|
+
const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
|
|
81
|
+
stdio: 'pipe',
|
|
82
|
+
cwd: appDirectory
|
|
83
|
+
});
|
|
84
|
+
resolveLog(childProgress, {
|
|
85
|
+
tsCheck,
|
|
86
|
+
watch
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
await childProgress;
|
|
91
|
+
console.info('[Tsc Compiler]: Successfully');
|
|
92
|
+
} catch (e) {
|
|
93
|
+
if (!tsCheck) {
|
|
94
|
+
console.info(`There are some type warnings, which can be checked by configuring 'output.disableTsChecker = false'`);
|
|
95
|
+
} else {
|
|
96
|
+
const isRecord = input => typeof input === 'object'; // 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if (isRecord(e)) {
|
|
100
|
+
console.error(e.stdout);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_utils.fs.removeSync(willDeleteTsconfigPath);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const taskMain = async ({
|
|
109
|
+
modernConfig
|
|
110
|
+
}) => {
|
|
111
|
+
const processArgv = argv(process.argv.slice(2));
|
|
112
|
+
const config = processArgv({
|
|
113
|
+
appDirectory: process.cwd(),
|
|
114
|
+
srcDir: 'src',
|
|
115
|
+
distDir: 'dist/types',
|
|
116
|
+
tsconfigPath: './tsconfig.json',
|
|
117
|
+
sourceDirName: 'src'
|
|
118
|
+
});
|
|
119
|
+
const option = {
|
|
120
|
+
srcDir: config.srcDir,
|
|
121
|
+
distDir: config.distDir,
|
|
122
|
+
projectData: {
|
|
123
|
+
appDirectory: config.appDirectory
|
|
124
|
+
},
|
|
125
|
+
tsconfigPath: config.tsconfigPath,
|
|
126
|
+
watch: config.watch,
|
|
127
|
+
tsCheck: config.tsCheck,
|
|
128
|
+
sourceDirName: config.sourceDirName
|
|
129
|
+
};
|
|
130
|
+
await generatorDts(modernConfig, option); // // TODO: watch 模式下无法转换
|
|
131
|
+
|
|
132
|
+
utils.resolveAlias(modernConfig, option);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
(async () => {
|
|
136
|
+
let options;
|
|
137
|
+
|
|
138
|
+
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
139
|
+
({
|
|
140
|
+
options
|
|
141
|
+
} = require(process.env.CORE_INIT_OPTION_FILE));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const {
|
|
145
|
+
resolved
|
|
146
|
+
} = await core.cli.init([], options);
|
|
147
|
+
await core.manager.run(async () => {
|
|
148
|
+
try {
|
|
149
|
+
await taskMain({
|
|
150
|
+
modernConfig: resolved
|
|
151
|
+
});
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.error(e.message);
|
|
154
|
+
|
|
155
|
+
_utils.fs.removeSync(removeTsconfigPath);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
})();
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.resolveAlias = exports.generatorTsConfig = void 0;
|
|
4
7
|
|
|
5
|
-
var
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
6
9
|
|
|
7
10
|
var _utils = require("@modern-js/utils");
|
|
8
11
|
|
|
@@ -16,26 +19,16 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
16
19
|
|
|
17
20
|
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; }
|
|
18
21
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const babel = _utils.Import.lazy('../utils/babel', require);
|
|
22
|
-
|
|
23
|
-
const constants = _utils.Import.lazy('./constants', require);
|
|
24
|
-
|
|
25
|
-
const core = _utils.Import.lazy('@modern-js/core', require);
|
|
22
|
+
const glob = _utils.Import.lazy('glob', require);
|
|
26
23
|
|
|
27
|
-
const
|
|
24
|
+
const babel = _utils.Import.lazy('../../utils/babel', require);
|
|
28
25
|
|
|
29
|
-
const
|
|
26
|
+
const tsPathsTransform = _utils.Import.lazy('../../utils/tspaths-transform', require);
|
|
30
27
|
|
|
31
|
-
const
|
|
28
|
+
const constants = _utils.Import.lazy('../constants', require);
|
|
32
29
|
|
|
33
30
|
const deepMerge = _utils.Import.lazy('lodash.merge', require);
|
|
34
31
|
|
|
35
|
-
const glob = _utils.Import.lazy('glob', require);
|
|
36
|
-
|
|
37
|
-
let removeTsconfigPath = '';
|
|
38
|
-
|
|
39
32
|
const generatorTsConfig = (projectTsconfig, {
|
|
40
33
|
appDirectory,
|
|
41
34
|
distDir,
|
|
@@ -82,90 +75,7 @@ const generatorTsConfig = (projectTsconfig, {
|
|
|
82
75
|
return tempTsconfigPath;
|
|
83
76
|
};
|
|
84
77
|
|
|
85
|
-
|
|
86
|
-
if (!tsconfigPath || !_utils.fs.existsSync(tsconfigPath)) {
|
|
87
|
-
return {};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return JSON5.parse(_utils.fs.readFileSync(tsconfigPath, 'utf-8'));
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const resolveLog = (childProgress, {
|
|
94
|
-
tsCheck: _tsCheck = false,
|
|
95
|
-
watch: _watch = false
|
|
96
|
-
} = {}) => {
|
|
97
|
-
var _childProgress$stdout, _childProgress$stdout2, _childProgress$stderr;
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* tsc 所有的log信息都是从stdout data 事件中获取
|
|
101
|
-
* 正常模式下,如果有报错信息,交给 resolveLog 后面的逻辑来处理
|
|
102
|
-
* watch 模式下,则使用这里的信息
|
|
103
|
-
*/
|
|
104
|
-
(_childProgress$stdout = childProgress.stdout) === null || _childProgress$stdout === void 0 ? void 0 : _childProgress$stdout.on('data', data => {
|
|
105
|
-
if (!_tsCheck) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (_watch) {
|
|
110
|
-
console.info(data.toString());
|
|
111
|
-
}
|
|
112
|
-
}); // 正常以下内容都不会触发,因为tsc 不会产生以下类型的log信息,不过防止意外情况
|
|
113
|
-
|
|
114
|
-
(_childProgress$stdout2 = childProgress.stdout) === null || _childProgress$stdout2 === void 0 ? void 0 : _childProgress$stdout2.on('error', error => {
|
|
115
|
-
console.error(error.message);
|
|
116
|
-
});
|
|
117
|
-
(_childProgress$stderr = childProgress.stderr) === null || _childProgress$stderr === void 0 ? void 0 : _childProgress$stderr.on('data', chunk => {
|
|
118
|
-
console.error(chunk.toString());
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const generatorDts = async (_, config) => {
|
|
123
|
-
const {
|
|
124
|
-
tsconfigPath,
|
|
125
|
-
distDir,
|
|
126
|
-
sourceDirName = 'src',
|
|
127
|
-
projectData: {
|
|
128
|
-
appDirectory
|
|
129
|
-
},
|
|
130
|
-
tsCheck = false,
|
|
131
|
-
watch = false
|
|
132
|
-
} = config;
|
|
133
|
-
const userTsconfig = getProjectTsconfig(tsconfigPath);
|
|
134
|
-
const willDeleteTsconfigPath = generatorTsConfig(userTsconfig, {
|
|
135
|
-
appDirectory,
|
|
136
|
-
distDir,
|
|
137
|
-
sourceDir: sourceDirName
|
|
138
|
-
});
|
|
139
|
-
removeTsconfigPath = willDeleteTsconfigPath;
|
|
140
|
-
const tscBinFile = path.join(appDirectory, './node_modules/.bin/tsc');
|
|
141
|
-
const watchParams = watch ? ['-w'] : [];
|
|
142
|
-
const childProgress = execa(tscBinFile, ['-p', willDeleteTsconfigPath, ...watchParams], {
|
|
143
|
-
stdio: 'pipe',
|
|
144
|
-
cwd: appDirectory
|
|
145
|
-
});
|
|
146
|
-
resolveLog(childProgress, {
|
|
147
|
-
tsCheck,
|
|
148
|
-
watch
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
try {
|
|
152
|
-
await childProgress;
|
|
153
|
-
console.info('[Tsc Compiler]: Successfully');
|
|
154
|
-
} catch (e) {
|
|
155
|
-
if (!tsCheck) {
|
|
156
|
-
console.warn(`There are some type issues, which can be checked by configuring 'source.enableTsChecker = true' ${os.EOL} in modern.config.js`);
|
|
157
|
-
} else {
|
|
158
|
-
const isRecord = input => typeof input === 'object'; // 通过使用 execa,可以将tsc 的 data 类型的报错信息变为异常错误信息
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (isRecord(e)) {
|
|
162
|
-
console.error(e.stdout);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
_utils.fs.removeSync(willDeleteTsconfigPath);
|
|
168
|
-
};
|
|
78
|
+
exports.generatorTsConfig = generatorTsConfig;
|
|
169
79
|
|
|
170
80
|
const resolveAlias = (modernConfig, config, watchFilenames = []) => {
|
|
171
81
|
const {
|
|
@@ -195,54 +105,4 @@ const resolveAlias = (modernConfig, config, watchFilenames = []) => {
|
|
|
195
105
|
}
|
|
196
106
|
};
|
|
197
107
|
|
|
198
|
-
|
|
199
|
-
modernConfig
|
|
200
|
-
}) => {
|
|
201
|
-
const processArgv = argv(process.argv.slice(2));
|
|
202
|
-
const config = processArgv({
|
|
203
|
-
appDirectory: process.cwd(),
|
|
204
|
-
srcDir: 'src',
|
|
205
|
-
distDir: 'dist/types',
|
|
206
|
-
tsconfigPath: './tsconfig.json',
|
|
207
|
-
sourceDirName: 'src'
|
|
208
|
-
});
|
|
209
|
-
const option = {
|
|
210
|
-
srcDir: config.srcDir,
|
|
211
|
-
distDir: config.distDir,
|
|
212
|
-
projectData: {
|
|
213
|
-
appDirectory: config.appDirectory
|
|
214
|
-
},
|
|
215
|
-
tsconfigPath: config.tsconfigPath,
|
|
216
|
-
watch: config.watch,
|
|
217
|
-
tsCheck: config.tsCheck,
|
|
218
|
-
sourceDirName: config.sourceDirName
|
|
219
|
-
};
|
|
220
|
-
await generatorDts(modernConfig, option); // TODO: watch 模式下无法转换
|
|
221
|
-
|
|
222
|
-
resolveAlias(modernConfig, option);
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
(async () => {
|
|
226
|
-
let options;
|
|
227
|
-
|
|
228
|
-
if (process.env.CORE_INIT_OPTION_FILE) {
|
|
229
|
-
({
|
|
230
|
-
options
|
|
231
|
-
} = require(process.env.CORE_INIT_OPTION_FILE));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const {
|
|
235
|
-
resolved
|
|
236
|
-
} = await core.cli.init([], options);
|
|
237
|
-
await core.manager.run(async () => {
|
|
238
|
-
try {
|
|
239
|
-
await taskMain({
|
|
240
|
-
modernConfig: resolved
|
|
241
|
-
});
|
|
242
|
-
} catch (e) {
|
|
243
|
-
console.error(e.message);
|
|
244
|
-
|
|
245
|
-
_utils.fs.removeSync(removeTsconfigPath);
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
})();
|
|
108
|
+
exports.resolveAlias = resolveAlias;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,8 +13,10 @@ declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@m
|
|
|
13
13
|
watchFiles: import("@modern-js/core").ParallelWorkflow<void, unknown>;
|
|
14
14
|
fileChange: import("@modern-js/core").AsyncWorkflow<{
|
|
15
15
|
filename: string;
|
|
16
|
+
eventType: "add" | "unlink" | "change";
|
|
16
17
|
}, void>;
|
|
17
18
|
beforeExit: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
19
|
+
beforeRestart: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
18
20
|
} & import("@modern-js/core").ClearDraftProgress<import("@modern-js/core").Hooks>>>>;
|
|
19
21
|
|
|
20
22
|
export default _default;
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NormalizedConfig } from '@modern-js/core';
|
|
2
|
+
import type { ITsconfig } from '../../types';
|
|
3
|
+
export interface IGeneratorConfig {
|
|
4
|
+
sourceDirName?: string;
|
|
5
|
+
srcDir: string;
|
|
6
|
+
distDir: string;
|
|
7
|
+
projectData: {
|
|
8
|
+
appDirectory: string;
|
|
9
|
+
};
|
|
10
|
+
tsconfigPath?: string;
|
|
11
|
+
tsCheck?: boolean;
|
|
12
|
+
watch?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const generatorTsConfig: (projectTsconfig: ITsconfig, {
|
|
15
|
+
appDirectory,
|
|
16
|
+
distDir,
|
|
17
|
+
sourceDir
|
|
18
|
+
}: {
|
|
19
|
+
appDirectory: string;
|
|
20
|
+
distDir: string;
|
|
21
|
+
sourceDir?: string | undefined;
|
|
22
|
+
}) => string;
|
|
23
|
+
export declare const resolveAlias: (modernConfig: NormalizedConfig, config: IGeneratorConfig, watchFilenames?: string[]) => void;
|
package/jest.config.js
CHANGED
package/lib/types.d.ts
CHANGED
|
@@ -70,11 +70,6 @@ declare module '*.less' {
|
|
|
70
70
|
export default classes;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
declare module '*.styl' {
|
|
74
|
-
const classes: { readonly [key: string]: string };
|
|
75
|
-
export default classes;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
73
|
declare module '*.sass' {
|
|
79
74
|
const classes: { readonly [key: string]: string };
|
|
80
75
|
export default classes;
|
|
@@ -95,11 +90,6 @@ declare module '*.module.less' {
|
|
|
95
90
|
export default classes;
|
|
96
91
|
}
|
|
97
92
|
|
|
98
|
-
declare module '*.module.styl' {
|
|
99
|
-
const classes: { readonly [key: string]: string };
|
|
100
|
-
export default classes;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
93
|
declare module '*.module.sass' {
|
|
104
94
|
const classes: { readonly [key: string]: string };
|
|
105
95
|
export default classes;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.4.1",
|
|
15
15
|
"bin": {
|
|
16
16
|
"modern": "./bin/modern.js"
|
|
17
17
|
},
|
|
@@ -53,17 +53,16 @@
|
|
|
53
53
|
"@babel/types": "^7.15.0",
|
|
54
54
|
"@modern-js/babel-compiler": "^1.2.1",
|
|
55
55
|
"@modern-js/babel-preset-module": "^1.3.1",
|
|
56
|
-
"@modern-js/
|
|
57
|
-
"@modern-js/css-config": "^1.2.1",
|
|
56
|
+
"@modern-js/css-config": "^1.2.2",
|
|
58
57
|
"@modern-js/i18n-cli-language-detector": "^1.2.1",
|
|
59
|
-
"@modern-js/module-tools-hooks": "^1.2.
|
|
60
|
-
"@modern-js/new-action": "^1.3.
|
|
61
|
-
"@modern-js/plugin-analyze": "^1.
|
|
62
|
-
"@modern-js/plugin-changeset": "^1.2.
|
|
58
|
+
"@modern-js/module-tools-hooks": "^1.2.2",
|
|
59
|
+
"@modern-js/new-action": "^1.3.2",
|
|
60
|
+
"@modern-js/plugin-analyze": "^1.3.3",
|
|
61
|
+
"@modern-js/plugin-changeset": "^1.2.2",
|
|
63
62
|
"@modern-js/plugin-fast-refresh": "^1.2.1",
|
|
64
63
|
"@modern-js/plugin-i18n": "^1.2.1",
|
|
65
|
-
"@modern-js/style-compiler": "^1.2.
|
|
66
|
-
"@modern-js/utils": "^1.
|
|
64
|
+
"@modern-js/style-compiler": "^1.2.2",
|
|
65
|
+
"@modern-js/utils": "^1.3.2",
|
|
67
66
|
"chalk": "^4.1.2",
|
|
68
67
|
"chokidar": "^3.5.2",
|
|
69
68
|
"dotenv": "^10.0.0",
|
|
@@ -80,7 +79,11 @@
|
|
|
80
79
|
"signale": "^1.4.0",
|
|
81
80
|
"tsconfig-paths": "^3.10.1"
|
|
82
81
|
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@modern-js/core": "^1.4.3"
|
|
84
|
+
},
|
|
83
85
|
"devDependencies": {
|
|
86
|
+
"@modern-js/core": "^1.4.3",
|
|
84
87
|
"@babel/preset-typescript": "^7.15.0",
|
|
85
88
|
"@modern-js/babel-chain": "^1.2.1",
|
|
86
89
|
"@types/babel__core": "^7.1.15",
|
|
@@ -107,8 +110,7 @@
|
|
|
107
110
|
},
|
|
108
111
|
"publishConfig": {
|
|
109
112
|
"registry": "https://registry.npmjs.org/",
|
|
110
|
-
"access": "public"
|
|
111
|
-
"types": "./dist/types/index.d.ts"
|
|
113
|
+
"access": "public"
|
|
112
114
|
},
|
|
113
115
|
"scripts": {
|
|
114
116
|
"new": "modern new",
|
|
@@ -12,7 +12,7 @@ jest.mock('@modern-js/core', () => ({
|
|
|
12
12
|
|
|
13
13
|
describe('dev feature with subCmd', () => {
|
|
14
14
|
beforeEach(() => {
|
|
15
|
-
|
|
15
|
+
jest.clearAllMocks();
|
|
16
16
|
});
|
|
17
17
|
it('should run task with "storybook" params when storybook plugin exist', async () => {
|
|
18
18
|
mockModuleToolsMenu.mockReturnValue([
|
|
@@ -27,4 +27,20 @@ describe('dev feature with subCmd', () => {
|
|
|
27
27
|
await runSubCmd('storybook', { isTsProject: true, appDirectory: '' });
|
|
28
28
|
expect(exit).toHaveBeenCalled();
|
|
29
29
|
});
|
|
30
|
+
|
|
31
|
+
it('should run task with alias name "story" params when storybook plugin exist', async () => {
|
|
32
|
+
mockModuleToolsMenu.mockReturnValue([
|
|
33
|
+
{ value: 'storybook', aliasValues: ['story'], runTask: mockDevMeta },
|
|
34
|
+
]);
|
|
35
|
+
await runSubCmd('story', { isTsProject: true, appDirectory: '' });
|
|
36
|
+
expect(mockDevMeta.mock.calls.length).toBe(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should run task with alias name "story1" params when storybook plugin exist', async () => {
|
|
40
|
+
mockModuleToolsMenu.mockReturnValue([
|
|
41
|
+
{ value: 'storybook', aliasValues: ['story'], runTask: mockDevMeta },
|
|
42
|
+
]);
|
|
43
|
+
await runSubCmd('story1', { isTsProject: true, appDirectory: '' });
|
|
44
|
+
expect(mockDevMeta.mock.calls.length).toBe(0);
|
|
45
|
+
});
|
|
30
46
|
});
|
|
@@ -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
|
-
})();
|