Skip to content

Commit 6929300

Browse files
committed
(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
1 parent 5c73483 commit 6929300

4 files changed

Lines changed: 16 additions & 22 deletions

File tree

src/createBuildConfigs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export async function createBuildConfigs(
3535
);
3636

3737
return await Promise.all(
38-
allInputs.map(async (options: TsdxOptions) => {
38+
allInputs.map(async (options: TsdxOptions, index: number) => {
3939
// pass the full rollup config to tsdx.config.js override
40-
const config = await createRollupConfig(options);
40+
const config = await createRollupConfig(options, index);
4141
return tsdxConfig.rollup(config, options);
4242
})
4343
);

src/createRollupConfig.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const errorCodeOpts = {
2424
let shebang: any = {};
2525

2626
export async function createRollupConfig(
27-
opts: TsdxOptions
27+
opts: TsdxOptions,
28+
outputNum: number
2829
): Promise<RollupOptions> {
2930
const findAndRecordErrorCodes = await extractErrors({
3031
...errorCodeOpts,
@@ -170,6 +171,10 @@ export async function createRollupConfig(
170171
compilerOptions: {
171172
// TS -> esnext, then leave the rest to babel-preset-env
172173
target: 'esnext',
174+
// don't output declarations more than once
175+
...(outputNum > 0
176+
? { declaration: false, declarationMap: false }
177+
: {}),
173178
},
174179
},
175180
check: !opts.transpileOnly,

src/deprecated.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,14 @@ export async function moveTypes() {
2121
'[tsdx]: Your rootDir is currently set to "./". Please change your ' +
2222
'rootDir to "./src".\n' +
2323
'TSDX has deprecated setting tsconfig.compilerOptions.rootDir to ' +
24-
'"./" as it caused buggy output for declarationMaps and occassionally ' +
25-
'for type declarations themselves.\n' +
24+
'"./" as it caused buggy output for declarationMaps and more.\n' +
2625
'You may also need to change your include to remove "test", which also ' +
2726
'caused declarations to be unnecessarily created for test files.'
2827
);
2928

30-
try {
31-
// Move the typescript types to the base of the ./dist folder
32-
await fs.copy(appDistSrc, paths.appDist, {
33-
overwrite: true,
34-
});
35-
} catch (err) {
36-
// ignore errors about the destination dir already existing or files not
37-
// existing as those always occur for some reason, re-throw any other
38-
// unexpected failures
39-
// NOTE: these errors mean that sometimes files don't get moved properly,
40-
// meaning that it's buggy / unreliable (see console.warn above)
41-
if (err.code !== 'EEXIST' && err.code !== 'ENOENT') {
42-
throw err;
43-
}
44-
}
45-
29+
// Move the typescript types to the base of the ./dist folder
30+
await fs.copy(appDistSrc, paths.appDist, {
31+
overwrite: true,
32+
});
4633
await fs.remove(appDistSrc);
4734
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,13 @@ prog
401401
async (inputOptions: RollupOptions & { output: OutputOptions }) => {
402402
let bundle = await rollup(inputOptions);
403403
await bundle.write(inputOptions.output);
404-
await deprecated.moveTypes();
405404
}
406405
)
407406
.catch((e: any) => {
408407
throw e;
408+
})
409+
.then(async () => {
410+
await deprecated.moveTypes();
409411
});
410412
logger(promise, 'Building modules');
411413
await promise;

0 commit comments

Comments
 (0)