From db62b1924a503974e6a674710c09616ac10ad2d0 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 19 Apr 2020 23:23:36 -0400 Subject: [PATCH 1/2] (optim/fix): only emit type declarations once - every other emission is just a duplicate -- no need to spend compute time to make duplicates - even for the upcoming multi-entry, the same types are emitted for each entry as the compiler just emits types for everything in the `tsconfig` `include` - fixes a long-standing bug with the deprecated moveTypes() function that would occassionally cause types to not be properly output - this was actually due to moveTypes() being run multiple times in parallel (as well as the types themselves being emitted multiple times in parallel) - hence the EEXIST and ENOENT filesystem errors being thrown, as that directory was being changed multiple times in parallel - race conditions, fun! - now they're only emitted once and only moved once, so no problems! - also fixes a bug with an initial version of multi-entry where if an entry in a subdir of src/ were added, e.g. src/foo/bar, the entire tree of type declarations would get output into dist/foo/src/*.d.ts - alternatively, could call `moveTypes()` with an arg for each entry, but there's no need since all declarations get produced the first time around anyway - similar bug occurred with an initial version of `--preserveModules` that used separate format directories --- src/createBuildConfigs.ts | 4 ++-- src/createRollupConfig.ts | 7 ++++++- src/deprecated.ts | 23 +++++------------------ src/index.ts | 4 +++- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/createBuildConfigs.ts b/src/createBuildConfigs.ts index b53bdee54..261c4f755 100644 --- a/src/createBuildConfigs.ts +++ b/src/createBuildConfigs.ts @@ -35,9 +35,9 @@ export async function createBuildConfigs( ); return await Promise.all( - allInputs.map(async (options: TsdxOptions) => { + allInputs.map(async (options: TsdxOptions, index: number) => { // pass the full rollup config to tsdx.config.js override - const config = await createRollupConfig(options); + const config = await createRollupConfig(options, index); return tsdxConfig.rollup(config, options); }) ); diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index eedc7dd5b..a06f5df24 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -24,7 +24,8 @@ const errorCodeOpts = { let shebang: any = {}; export async function createRollupConfig( - opts: TsdxOptions + opts: TsdxOptions, + outputNum: number ): Promise { const findAndRecordErrorCodes = await extractErrors({ ...errorCodeOpts, @@ -170,6 +171,10 @@ export async function createRollupConfig( compilerOptions: { // TS -> esnext, then leave the rest to babel-preset-env target: 'esnext', + // don't output declarations more than once + ...(outputNum > 0 + ? { declaration: false, declarationMap: false } + : {}), }, }, check: !opts.transpileOnly, diff --git a/src/deprecated.ts b/src/deprecated.ts index 275caadb8..41319cdcc 100644 --- a/src/deprecated.ts +++ b/src/deprecated.ts @@ -21,27 +21,14 @@ export async function moveTypes() { '[tsdx]: Your rootDir is currently set to "./". Please change your ' + 'rootDir to "./src".\n' + 'TSDX has deprecated setting tsconfig.compilerOptions.rootDir to ' + - '"./" as it caused buggy output for declarationMaps and occassionally ' + - 'for type declarations themselves.\n' + + '"./" as it caused buggy output for declarationMaps and more.\n' + 'You may also need to change your include to remove "test", which also ' + 'caused declarations to be unnecessarily created for test files.' ); - try { - // Move the typescript types to the base of the ./dist folder - await fs.copy(appDistSrc, paths.appDist, { - overwrite: true, - }); - } catch (err) { - // ignore errors about the destination dir already existing or files not - // existing as those always occur for some reason, re-throw any other - // unexpected failures - // NOTE: these errors mean that sometimes files don't get moved properly, - // meaning that it's buggy / unreliable (see console.warn above) - if (err.code !== 'EEXIST' && err.code !== 'ENOENT') { - throw err; - } - } - + // Move the typescript types to the base of the ./dist folder + await fs.copy(appDistSrc, paths.appDist, { + overwrite: true, + }); await fs.remove(appDistSrc); } diff --git a/src/index.ts b/src/index.ts index b5d8254d6..bcc53c17f 100755 --- a/src/index.ts +++ b/src/index.ts @@ -401,11 +401,13 @@ prog async (inputOptions: RollupOptions & { output: OutputOptions }) => { let bundle = await rollup(inputOptions); await bundle.write(inputOptions.output); - await deprecated.moveTypes(); } ) .catch((e: any) => { throw e; + }) + .then(async () => { + await deprecated.moveTypes(); }); logger(promise, 'Building modules'); await promise; From 714e4bd45ef276ab4941515a037727f9cac4f9d6 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sun, 19 Apr 2020 23:43:20 -0400 Subject: [PATCH 2/2] (optim): no need for separate cacheRoot per format - similarly to the previous commit, it's unnecessary and less performant to have a separate cacheRoot per format - each TS compilation occurs before the format is changed anyway, so at that point in the process, format is irrelevant - builds can now re-use cache from other formats, which resulted in a perf boost during testing --- src/createRollupConfig.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index a06f5df24..abf5623c4 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -146,7 +146,6 @@ export async function createRollupConfig( }, typescript({ typescript: ts, - cacheRoot: `./node_modules/.cache/tsdx/${opts.format}/`, tsconfig: opts.tsconfig, tsconfigDefaults: { exclude: [