Skip to content

Commit

Permalink
(optim/fix): only emit type declarations once
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 committed Apr 20, 2020
1 parent 5c73483 commit 6929300
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/createBuildConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
Expand Down
7 changes: 6 additions & 1 deletion src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const errorCodeOpts = {
let shebang: any = {};

export async function createRollupConfig(
opts: TsdxOptions
opts: TsdxOptions,
outputNum: number
): Promise<RollupOptions> {
const findAndRecordErrorCodes = await extractErrors({
...errorCodeOpts,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 5 additions & 18 deletions src/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6929300

Please sign in to comment.