stack/configs/tsup/plugins.ts
Zai Shi d9e2dae4c6
Some checks failed
all-good: Did all the other checks pass? / all-good (push) Has been cancelled
Ensure Prisma migrations are in sync with the schema / check_prisma_migrations (22.x) (push) Has been cancelled
Docker Emulator Test / docker (push) Has been cancelled
Docker Server Build and Push / Docker Build and Push Server (push) Has been cancelled
Docker Server Test / docker (push) Has been cancelled
Runs E2E API Tests / build (22.x) (push) Has been cancelled
Lint & build / lint_and_build (latest) (push) Has been cancelled
Preview Docs / run (push) Has been cancelled
Dev Environment Test / restart-dev-and-test (push) Has been cancelled
Run setup tests / setup-tests (push) Has been cancelled
TOC Generator / TOC Generator (push) Has been cancelled
Config DB migration step 2 (#629)
Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
Co-authored-by: moritz <moritsch@student.ethz.ch>
2025-04-29 14:52:45 -07:00

37 lines
1.3 KiB
TypeScript

import type { Plugin } from "esbuild";
import fs from 'fs';
import path from "path";
export const createBasePlugin = (options: {}): Plugin => {
const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
return {
name: 'stackframe tsup plugin (private)',
setup(build) {
build.onEnd(result => {
const sourceFiles = result.outputFiles?.filter(file => !file.path.endsWith('.map')) ?? [];
for (const file of sourceFiles) {
let newText = file.text;
// make sure "use client" is at the top of the file
const matchUseClient = /[\s\n\r]*(^|\n|\r|;)\s*['"]use\s+client['"]\s*(\n|\r|;)/im;
if (matchUseClient.test(file.text)) {
newText = `"use client";\n${file.text}`;
}
file.contents = new TextEncoder().encode(newText);
}
});
build.onLoad({ filter: /\.(jsx?|tsx?)$/ }, async (args) => {
let contents = await fs.promises.readFile(args.path, 'utf8');
contents = contents.replace(/STACK_COMPILE_TIME_CLIENT_PACKAGE_VERSION_SENTINEL/g, `js ${packageJson.name}@${packageJson.version}`);
contents = contents.replace(/import\.meta\.vitest/g, 'undefined');
return {
contents,
loader: path.extname(args.path).slice(1) as 'js' | 'jsx' | 'ts' | 'tsx'
};
});
},
}
}