Skip to content

Commit

Permalink
fix: improve directory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Jan 5, 2023
1 parent 955e0ad commit c75d108
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
48 changes: 31 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Plugin } from "vite";
import fs from "fs";
import path from "path";

export interface PluginOptions {
tsConfigPath?: string;
Expand All @@ -8,18 +9,21 @@ export interface PluginOptions {
const NAME = "vite-plugin-custom-tsconfig";
const TSCONFIG_PATH = "tsconfig.json";
const BANNER = `// GENERATED BY ${NAME} \n`;
const DELETE_TIP = `Please delete it or remove ${NAME} from your Vite config`;

const tsConfigExists = () => {
return fs.existsSync(TSCONFIG_PATH);
const resolveFromRoot = (root: string, file: string) => {
return path.resolve(root, file);
};

const tsConfigHasBanner = () => {
const tsconfigContent = fs.readFileSync(TSCONFIG_PATH, "utf8");
const tsConfigHasBanner = (tsconfig: string) => {
const tsconfigContent = fs.readFileSync(tsconfig, "utf8");

return tsconfigContent.startsWith(BANNER.trim());
};

const customTsConfigPlugin = (options?: PluginOptions): Plugin => {
let root: string;

const resolvedOptions: Required<PluginOptions> = {
tsConfigPath: "tsconfig.build.json",
...options,
Expand All @@ -28,33 +32,43 @@ const customTsConfigPlugin = (options?: PluginOptions): Plugin => {
return {
name: NAME,

config() {
if (tsConfigExists() && !tsConfigHasBanner()) {
throw new Error(
"tsconfig.json already exists. Please delete it or remove vite-plugin-custom-tsconfig from your Vite config"
);
config(config) {
root ??= config.root ?? process.cwd();

const tsconfig = resolveFromRoot(root, TSCONFIG_PATH);

if (fs.existsSync(tsconfig) && !tsConfigHasBanner(tsconfig)) {
throw new Error(`${TSCONFIG_PATH} already exists. ${DELETE_TIP}`);
}

const customTsConfigContent = fs.readFileSync(
resolvedOptions.tsConfigPath,
"utf8"
const customTsConfig = resolveFromRoot(
root,
resolvedOptions.tsConfigPath
);

fs.writeFileSync(TSCONFIG_PATH, BANNER + customTsConfigContent);
if (!fs.existsSync(customTsConfig)) {
throw new Error(`${resolvedOptions.tsConfigPath} does not exist.`);
}

const customTsConfigContent = fs.readFileSync(customTsConfig, "utf8");

fs.writeFileSync(tsconfig, BANNER + customTsConfigContent);
},

closeBundle() {
if (!tsConfigExists()) {
const tsconfig = resolveFromRoot(root, TSCONFIG_PATH);

if (!fs.existsSync(tsconfig)) {
return;
}

if (!tsConfigHasBanner()) {
if (!tsConfigHasBanner(tsconfig)) {
throw new Error(
"tsconfig.json does not contain the expected banner. Please delete it or remove vite-plugin-custom-tsconfig from your Vite config"
`${TSCONFIG_PATH} does not contain the expected banner. ${DELETE_TIP}`
);
}

fs.rmSync("tsconfig.json", { force: true });
fs.rmSync(tsconfig);
},
};
};
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
}
},
format: ["cjs", "esm"],
minify: true,
// minify: true,
outDir: "lib",
target: "node14",
});

0 comments on commit c75d108

Please sign in to comment.