Optimize .d.ts generation; prepare for sub modules (#7954)

* Optimize .d.ts generation; prepare for sub modules

* Fix no emit
This commit is contained in:
Leslie Leigh (李的序)
2020-12-12 16:50:35 +08:00
committed by GitHub
parent 9602aae029
commit e00a0da0ac
5 changed files with 129 additions and 61 deletions
+1
View File
@@ -80,6 +80,7 @@ gulp.task('build-declarations', async () => {
await fs.emptyDir(outDir);
return require('./scripts/generate-declarations/generate-declarations.js').generate({
outDir,
withIndex: true,
});
});
+27 -21
View File
@@ -1412,7 +1412,7 @@
},
"@cocos/babel-plugin-dynamic-import-vars": {
"version": "1.0.2",
"resolved": "https://registry.npm.taobao.org/@cocos/babel-plugin-dynamic-import-vars/download/@cocos/babel-plugin-dynamic-import-vars-1.0.2.tgz",
"resolved": "https://registry.npm.taobao.org/@cocos/babel-plugin-dynamic-import-vars/download/@cocos/babel-plugin-dynamic-import-vars-1.0.2.tgz?cache=0&sync_timestamp=1604048375532&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40cocos%2Fbabel-plugin-dynamic-import-vars%2Fdownload%2F%40cocos%2Fbabel-plugin-dynamic-import-vars-1.0.2.tgz",
"integrity": "sha1-chaboK/r3jjHTS02cU95rWZBT5g=",
"dev": true,
"requires": {
@@ -1479,7 +1479,7 @@
},
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-4.3.0.tgz?cache=0&sync_timestamp=1606792371412&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-4.3.0.tgz",
"resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-4.3.0.tgz?cache=0&sync_timestamp=1601839226460&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-4.3.0.tgz",
"integrity": "sha1-7dgDYornHATIWuegkG7a00tkiTc=",
"dev": true,
"requires": {
@@ -1629,7 +1629,7 @@
},
"yargs": {
"version": "15.4.1",
"resolved": "https://registry.npm.taobao.org/yargs/download/yargs-15.4.1.tgz?cache=0&sync_timestamp=1607207963779&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs%2Fdownload%2Fyargs-15.4.1.tgz",
"resolved": "https://registry.npm.taobao.org/yargs/download/yargs-15.4.1.tgz?cache=0&sync_timestamp=1605465911528&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs%2Fdownload%2Fyargs-15.4.1.tgz",
"integrity": "sha1-DYehbeAa7p2L7Cv7909nhRcw9Pg=",
"dev": true,
"requires": {
@@ -2111,7 +2111,7 @@
},
"@rollup/plugin-json": {
"version": "4.1.0",
"resolved": "https://registry.npm.taobao.org/@rollup/plugin-json/download/@rollup/plugin-json-4.1.0.tgz",
"resolved": "https://registry.npm.taobao.org/@rollup/plugin-json/download/@rollup/plugin-json-4.1.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40rollup%2Fplugin-json%2Fdownload%2F%40rollup%2Fplugin-json-4.1.0.tgz",
"integrity": "sha1-VOCYZ65pY8WThE2L16nHGClElvM=",
"dev": true,
"requires": {
@@ -2120,7 +2120,7 @@
},
"@rollup/plugin-node-resolve": {
"version": "7.1.3",
"resolved": "https://registry.npm.taobao.org/@rollup/plugin-node-resolve/download/@rollup/plugin-node-resolve-7.1.3.tgz?cache=0&sync_timestamp=1606766485874&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40rollup%2Fplugin-node-resolve%2Fdownload%2F%40rollup%2Fplugin-node-resolve-7.1.3.tgz",
"resolved": "https://registry.npm.taobao.org/@rollup/plugin-node-resolve/download/@rollup/plugin-node-resolve-7.1.3.tgz?cache=0&sync_timestamp=1603765888985&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40rollup%2Fplugin-node-resolve%2Fdownload%2F%40rollup%2Fplugin-node-resolve-7.1.3.tgz",
"integrity": "sha1-gN44Tt+9e/yRARZJEPhgeBUaPso=",
"dev": true,
"requires": {
@@ -2270,7 +2270,7 @@
},
"@types/resolve": {
"version": "0.0.8",
"resolved": "https://registry.npm.taobao.org/@types/resolve/download/@types/resolve-0.0.8.tgz",
"resolved": "https://registry.npm.taobao.org/@types/resolve/download/@types/resolve-0.0.8.tgz?cache=0&sync_timestamp=1596840738717&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fresolve%2Fdownload%2F%40types%2Fresolve-0.0.8.tgz",
"integrity": "sha1-8mB00jjgJlnjI84aE9BB7uKA4ZQ=",
"dev": true,
"requires": {
@@ -9906,7 +9906,7 @@
},
"rollup": {
"version": "2.26.6",
"resolved": "https://registry.npm.taobao.org/rollup/download/rollup-2.26.6.tgz?cache=0&sync_timestamp=1607231848363&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frollup%2Fdownload%2Frollup-2.26.6.tgz",
"resolved": "https://registry.npm.taobao.org/rollup/download/rollup-2.26.6.tgz",
"integrity": "sha1-C0YMHaIkxq8SoelIooxROqEfK5M=",
"dev": true,
"requires": {
@@ -9933,7 +9933,7 @@
},
"rollup-plugin-terser": {
"version": "5.3.1",
"resolved": "https://registry.npm.taobao.org/rollup-plugin-terser/download/rollup-plugin-terser-5.3.1.tgz?cache=0&sync_timestamp=1600350654805&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frollup-plugin-terser%2Fdownload%2Frollup-plugin-terser-5.3.1.tgz",
"resolved": "https://registry.npm.taobao.org/rollup-plugin-terser/download/rollup-plugin-terser-5.3.1.tgz?cache=0&sync_timestamp=1599267952414&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Frollup-plugin-terser%2Fdownload%2Frollup-plugin-terser-5.3.1.tgz",
"integrity": "sha1-jGUAYsIqhCbGQmhUiVdGO/mBtBM=",
"dev": true,
"requires": {
@@ -9966,7 +9966,7 @@
},
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-4.3.0.tgz?cache=0&sync_timestamp=1606792371412&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-4.3.0.tgz",
"resolved": "https://registry.npm.taobao.org/ansi-styles/download/ansi-styles-4.3.0.tgz?cache=0&sync_timestamp=1601839226460&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fansi-styles%2Fdownload%2Fansi-styles-4.3.0.tgz",
"integrity": "sha1-7dgDYornHATIWuegkG7a00tkiTc=",
"dev": true,
"optional": true,
@@ -10067,7 +10067,7 @@
},
"y18n": {
"version": "5.0.5",
"resolved": "https://registry.npm.taobao.org/y18n/download/y18n-5.0.5.tgz?cache=0&sync_timestamp=1606778028917&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fy18n%2Fdownload%2Fy18n-5.0.5.tgz",
"resolved": "https://registry.npm.taobao.org/y18n/download/y18n-5.0.5.tgz?cache=0&sync_timestamp=1603639294538&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fy18n%2Fdownload%2Fy18n-5.0.5.tgz",
"integrity": "sha1-h2nsCNA7HqLfJQCs71YXQ7u5qxg=",
"dev": true,
"optional": true
@@ -10921,7 +10921,7 @@
},
"terser": {
"version": "4.8.0",
"resolved": "https://registry.npm.taobao.org/terser/download/terser-4.8.0.tgz?cache=0&sync_timestamp=1606494026148&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fterser%2Fdownload%2Fterser-4.8.0.tgz",
"resolved": "https://registry.npm.taobao.org/terser/download/terser-4.8.0.tgz?cache=0&sync_timestamp=1603374108186&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fterser%2Fdownload%2Fterser-4.8.0.tgz",
"integrity": "sha1-YwVjQ9fHC7KfOvZlhlpG/gOg3xc=",
"dev": true,
"requires": {
@@ -11063,13 +11063,13 @@
"dev": true
},
"tfig": {
"version": "2.0.10",
"resolved": "https://registry.npm.taobao.org/tfig/download/tfig-2.0.10.tgz",
"integrity": "sha1-viO0EHzwaf4uAQvP2SwYLHSl6PE=",
"version": "3.0.0",
"resolved": "https://registry.npm.taobao.org/tfig/download/tfig-3.0.0.tgz",
"integrity": "sha1-jwl9yWvKN26yS8HF0NUaMRsIENw=",
"dev": true,
"requires": {
"fs-extra": "^7.0.1",
"typescript": "^4.0.5",
"typescript": "^4.1.2",
"yargs": "^13.2.2"
},
"dependencies": {
@@ -11081,7 +11081,7 @@
},
"cliui": {
"version": "5.0.0",
"resolved": "https://registry.npm.taobao.org/cliui/download/cliui-5.0.0.tgz?cache=0&sync_timestamp=1602861367442&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcliui%2Fdownload%2Fcliui-5.0.0.tgz",
"resolved": "https://registry.npm.taobao.org/cliui/download/cliui-5.0.0.tgz",
"integrity": "sha1-3u/P2y6AB4SqNPRvoI4GhRx7u8U=",
"dev": true,
"requires": {
@@ -11156,6 +11156,12 @@
"ansi-regex": "^4.1.0"
}
},
"typescript": {
"version": "4.1.3",
"resolved": "https://registry.npm.taobao.org/typescript/download/typescript-4.1.3.tgz?cache=0&sync_timestamp=1607757001042&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-4.1.3.tgz",
"integrity": "sha1-UZ1YK9lMugz4k0x9joRn5HP1O7c=",
"dev": true
},
"which-module": {
"version": "2.0.0",
"resolved": "https://registry.npm.taobao.org/which-module/download/which-module-2.0.0.tgz",
@@ -11174,14 +11180,14 @@
}
},
"y18n": {
"version": "4.0.0",
"resolved": "https://registry.npm.taobao.org/y18n/download/y18n-4.0.0.tgz?cache=0&sync_timestamp=1603639294538&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fy18n%2Fdownload%2Fy18n-4.0.0.tgz",
"integrity": "sha1-le+U+F7MgdAHwmThkKEg8KPIVms=",
"version": "4.0.1",
"resolved": "https://registry.npm.taobao.org/y18n/download/y18n-4.0.1.tgz?cache=0&sync_timestamp=1606778028917&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fy18n%2Fdownload%2Fy18n-4.0.1.tgz",
"integrity": "sha1-jbK4PDHF11CZu4kLI/MJSJHiR9Q=",
"dev": true
},
"yargs": {
"version": "13.3.2",
"resolved": "https://registry.npm.taobao.org/yargs/download/yargs-13.3.2.tgz?cache=0&sync_timestamp=1602805577427&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs%2Fdownload%2Fyargs-13.3.2.tgz",
"resolved": "https://registry.npm.taobao.org/yargs/download/yargs-13.3.2.tgz?cache=0&sync_timestamp=1607207963779&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs%2Fdownload%2Fyargs-13.3.2.tgz",
"integrity": "sha1-rX/+/sGqWVZayRX4Lcyzipwxot0=",
"dev": true,
"requires": {
@@ -11199,7 +11205,7 @@
},
"yargs-parser": {
"version": "13.1.2",
"resolved": "https://registry.npm.taobao.org/yargs-parser/download/yargs-parser-13.1.2.tgz?cache=0&sync_timestamp=1602861397132&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs-parser%2Fdownload%2Fyargs-parser-13.1.2.tgz",
"resolved": "https://registry.npm.taobao.org/yargs-parser/download/yargs-parser-13.1.2.tgz?cache=0&sync_timestamp=1604886709178&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fyargs-parser%2Fdownload%2Fyargs-parser-13.1.2.tgz",
"integrity": "sha1-Ew8JcC667vJlDVTObj5XBvek+zg=",
"dev": true,
"requires": {
+1 -1
View File
@@ -45,7 +45,7 @@
"jest": "^24.8.0",
"spectorjs": "^0.9.15",
"stats.js": "^0.17.0",
"tfig": "^2.0.10",
"tfig": "^3.0.0",
"ts-jest": "^26.3.0",
"typedoc": "^0.17.7",
"typedoc-plugin-external-module-name": "^4.0.3",
@@ -4,8 +4,20 @@
const { generate } = require('./generate-declarations');
const fs = require('fs-extra');
const outDir = './bin/.declarations';
(async () => {
const outDir = './bin/.declarations';
(async () => {
const options = {
outDir,
};
if (!process.argv.some((arg) => arg.startsWith('--with'))) {
options.withIndex = true;
options.withExports = false;
options.withEditorExports = false;
} else {
options.withIndex = !!process.argv.includes('--with-index');
options.withExports = !!process.argv.includes('--with-exports');
options.withEditorExports = !!process.argv.includes('--with-editor-exports');
}
await fs.emptyDir(outDir);
await generate({ outDir });
await generate(options);
})();
@@ -13,9 +13,9 @@ const gift = require('tfig');
const tsConfigDir = join(__dirname, '..', '..');
const tsConfigPath = join(tsConfigDir, 'tsconfig.json');
async function getEngineEntries (engine) {
async function getDomainExports(engine, exportsRoot, prefix, excludes) {
const result = {};
const entryRootDir = join(engine, 'exports');
const entryRootDir = join(engine, exportsRoot);
const entryFileNames = await readdir(entryRootDir);
for (const entryFileName of entryFileNames) {
const entryExtName = extname(entryFileName);
@@ -23,22 +23,43 @@ async function getEngineEntries (engine) {
continue;
}
const entryBaseNameNoExt = basename(entryFileName, entryExtName);
const entryName = `cc.${entryBaseNameNoExt}`;
result[entryName] = `exports/${entryBaseNameNoExt}`;
if (excludes && excludes.includes(entryBaseNameNoExt)) {
continue;
}
const entryName = `${prefix}${entryBaseNameNoExt}`;
result[entryName] = `${exportsRoot.split(/[\\\/]/g).join('/')}/${entryBaseNameNoExt}`;
}
return result;
}
async function getEngineEntries (engine) {
return await getDomainExports(engine, 'exports', 'cc/', ['wait-for-ammo-instantiation']);
}
async function getEditorExportEntries(engine) {
return await getDomainExports(engine, join('editor', 'exports'), 'cc/editor/exports/');
}
async function generate (options) {
console.log(`Typescript version: ${ts.version}`);
const { outDir } = options;
const {
outDir,
withIndex,
withExports,
withEditorExports,
} = options;
ensureDirSync(outDir);
console.debug(`With index: ${withIndex}`);
console.debug(`With exports: ${withExports}`);
console.debug(`With editor exports: ${withEditorExports}`);
const unbundledOutFile = join(outDir, `cc-before-rollup.js`);
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(
tsConfigPath, {
declaration: true,
noEmit: false,
emitDeclarationOnly: true,
outFile: unbundledOutFile,
outDir: undefined,
@@ -76,7 +97,17 @@ async function generate (options) {
console.log(`Generating...`);
const program = ts.createProgram(parsedCommandLine.fileNames, parsedCommandLine.options);
const engineRoot = join(__dirname, '..', '..');
const entryMap = await getEngineEntries(engineRoot);
const editorExportEntries = await getEditorExportEntries(engineRoot);
let fileNames = parsedCommandLine.fileNames;
if (withEditorExports) {
fileNames = fileNames.concat(Object.values(editorExportEntries).map((e) => join(engineRoot, e)));
}
const program = ts.createProgram(fileNames, parsedCommandLine.options);
const emitResult = program.emit(
undefined, // targetSourceFile
undefined, // writeFile
@@ -126,50 +157,68 @@ async function generate (options) {
copyFileSync(file, destPath);
});
const entryMap = await getEngineEntries(join(__dirname, '..', '..'));
const entries = Object.keys(entryMap);
const giftInputs = [ tscOutputDtsFile ];
// The "cc" module, contents like:
// ```
// declare module "cc" {
// export * from "exports/base";
// }
// ```
const ccDtsFile = join(dirName, 'virtual-cc.d.ts');
await (async () => {
const ccModules = entries.slice().map((extern) => entryMap[extern]);
const code = `declare module "cc" {\n${ccModules.map((moduleId) => ` export * from "${moduleId}";`).join('\n')}\n}`;
/** @type {Record<string, string>} */
const giftEntries = { };
const cleanupFiles = [ tscOutputDtsFile ];
if (withExports) {
Object.assign(giftEntries, entryMap);
}
if (withEditorExports) {
Object.assign(giftEntries, editorExportEntries);
}
if (withIndex && !withExports) {
giftEntries['cc'] = 'cc';
const ccDtsFile = join(dirName, 'virtual-cc.d.ts');
giftInputs.push(ccDtsFile);
cleanupFiles.push(ccDtsFile);
const code = generateModuleSourceCc(Object.values(entryMap));
await fs.writeFile(ccDtsFile, code, { encoding: 'utf8' });
})();
}
console.log(`Bundling...`);
try {
const giftInputPath = tscOutputDtsFile;
const giftOutputPath = join(dirName,'cc.d.ts' );
const indexOutputPath = join(dirName,'cc.d.ts');
const giftResult = gift.bundle({
input: [giftInputPath, ccDtsFile],
output: giftOutputPath,
input: giftInputs,
name: 'cc',
rootModule: 'index',
entries: {
'cc': 'cc',
},
entries: giftEntries,
groups: [
{ test: /^cc\/editor.*$/, path: join(dirName,'cc.editor.d.ts') },
{ test: /^cc\/.*$/, path: join(dirName,'index.d.ts') },
{ test: /^cc.*$/, path: indexOutputPath },
],
});
if (giftResult.error !== gift.GiftErrors.Ok) {
console.error(`Failed to bundle declaration files because of gift error: ${gift.GiftErrors[giftResult.error]}.`);
return false;
await Promise.all(giftResult.groups.map(async (group) => {
await fs.outputFile(group.path, group.code, { encoding: 'utf8' });
}));
if (withIndex && withExports) {
await fs.outputFile(
indexOutputPath,
generateModuleSourceCc(Object.keys(entryMap)),
{ encoding: 'utf8' });
}
writeFileSync(giftOutputPath, giftResult.code);
} catch (error) {
console.error(error)
return false;
} finally {
await Promise.all(([
tscOutputDtsFile,
ccDtsFile,
].map(async (file) => fs.unlink(file))));
await Promise.all((cleanupFiles.map(async (file) => fs.unlink(file))));
}
return true;
}
function generateModuleSourceCc(requests) {
return `declare module "cc" {\n${requests.map((moduleId) => ` export * from "${moduleId}";`).join('\n')}\n}`
}
module.exports = { generate };