Skip to content

fix: Fix rollup plugin output directory in Typescript (box/box-codegen#678) #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "8f5e41b", "specHash": "6782a0d", "version": "1.13.1" }
{ "engineHash": "41feeaa", "specHash": "6782a0d", "version": "1.13.1" }
30 changes: 30 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import json from '@rollup/plugin-json';
import fs from 'fs';
import path from 'path';

function getBrowserConfig() {
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
Expand Down Expand Up @@ -45,6 +46,32 @@ function replaceFiles(options = {}) {
};
}

function cleanupTypeScript(options = {}) {
const { tsconfig } = options;
const tsconfigFile = JSON.parse(fs.readFileSync(tsconfig, 'utf-8'));
let tsOutputDir;

return {
name: 'cleanup-typescript',
generateBundle(outputOptions) {
const outputDir =
outputOptions.dir ||
(outputOptions.file
? outputOptions.file.substring(0, outputOptions.file.lastIndexOf('/'))
: 'dist');
tsOutputDir = path.resolve(
outputDir,
tsconfigFile.compilerOptions.outDir,
);
},
closeBundle() {
if (tsOutputDir) {
fs.rmSync(tsOutputDir, { recursive: true, force: true });
}
},
};
}

export default {
input: 'src/index.ts',
output: {
Expand All @@ -67,5 +94,8 @@ export default {
tsconfig: './tsconfig.rollup.json',
}), // Compiles TypeScript
json(), // Converts JSON files to ES6 modules
cleanupTypeScript({
tsconfig: './tsconfig.rollup.json',
}),
],
};
2 changes: 1 addition & 1 deletion tsconfig.rollup.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "ESNext",
"declaration": true,
"sourceMap": true,
"outDir": "lib",
"outDir": "lib-rollup",
"isolatedModules": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand Down
Loading