Skip to content

Commit

Permalink
fix: generate typescript definitions in one pass
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Nov 13, 2023
1 parent dbd0526 commit a40de96
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions src/assets/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
64 changes: 19 additions & 45 deletions src/modules/Bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,56 +328,30 @@ class Bundler {
) {
const processedTDFiles = new Set();

const options = {
declaration: true,
emitDeclarationOnly: true,
};

log(chalk.gray(`Generating ${format} typescript definition files...\n`));

// write to filesytem and index
return new Promise((resolve, reject) => {
let rejected = false;

// run with try catch block
const run = (callback) => {
try {
callback();
} catch (ex) {
rejected = true;
reject(ex);
}
};

// write to filesytem and index
const onEmit = (filename, content) =>
run(() => {
const out = path.join(outFolder, filename.split(this.src)[1]);
processedTDFiles.add(out);
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, content);
});

for (let i = 0; i < fileModules.length; i++) {
const fileModule = fileModules[i];
if (rejected) {
break;
}

const tsdOut = path.join(
outFolder,
fileModule.locationRelativeToSrc,
fileModule.baseName + '.d.ts'
);

if (processedTDFiles.has(tsdOut)) {
continue;
}

run(() => {
const program = ts.createProgram([fileModule.location], {
declaration: true,
emitDeclarationOnly: true,
});
const host = ts.createCompilerHost(options);

program.emit(undefined, onEmit, undefined, true);
});
}
host.writeFile = (filename: string, contents: string) => {
const out = path.join(outFolder, filename.split(this.src)[1]);
processedTDFiles.add(out);
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, contents);
};

const program = ts.createProgram(
fileModules.map((current) => current.location),
options,
host
);
program.emit();
resolve(true);
});
}
Expand Down

0 comments on commit a40de96

Please sign in to comment.