stack/configs/tsup/js-library.ts
Zai Shi 5265327176
Fix package build with explicit index.js imports (#677)
<!--

Make sure you've read the CONTRIBUTING.md guidelines:
https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md

-->

<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Fixes ESM import issue by adding a plugin for implicit `index.js` and
renames interface files for consistency.
> 
>   - **Behavior**:
> - Adds `fixImportExtensions` plugin in `configs/tsup/js-library.ts` to
handle ESM imports with implicit `index.js`.
>   - **Renames**:
> - Renames `adminInterface.ts` to `admin-interface.ts` and updates
imports in `index.ts` and `admin-app-impl.ts`.
> - Renames `clientInterface.ts` to `client-interface.ts` and updates
imports in `server-interface.ts`.
> - Renames `serverInterface.ts` to `server-interface.ts` and updates
imports in `adminInterface.ts`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=stack-auth%2Fstack-auth&utm_source=github&utm_medium=referral)<sup>
for 1fcd668be4. You can
[customize](https://app.ellipsis.dev/stack-auth/settings/summaries) this
summary. It will automatically update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
2025-05-15 23:55:59 +02:00

62 lines
2.0 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import { defineConfig } from 'tsup';
import { createBasePlugin } from './plugins';
const customNoExternal = new Set([
"oauth4webapi",
]);
// https://github.com/egoist/tsup/issues/953
const fixImportExtensions = (extension: string = ".js") => ({
name: "fix-import-extensions",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
const filePath = path.join(args.resolveDir, args.path);
let resolvedPath;
if (fs.existsSync(filePath + ".ts") || fs.existsSync(filePath + ".tsx")) {
resolvedPath = args.path + extension;
} else if (fs.existsSync(path.join(filePath, `index.ts`)) || fs.existsSync(path.join(filePath, `index.tsx`))) {
resolvedPath = args.path.endsWith("/") ? args.path + "index" + extension : args.path + "/index" + extension;
}
return { path: resolvedPath ?? args.path, external: true };
}
});
},
});
export default function createJsLibraryTsupConfig(options: { barrelFile: boolean }) {
return defineConfig({
entryPoints: ['src/**/*.(ts|tsx|js|jsx)'],
sourcemap: true,
clean: false,
noExternal: [...customNoExternal],
dts: options.barrelFile ? 'src/index.ts' : true, // we only generate types for the barrel file because it drastically decreases the memory needed for tsup https://github.com/egoist/tsup/issues/920#issuecomment-2454732254
outDir: 'dist',
format: ['esm', 'cjs'],
legacyOutput: true,
esbuildPlugins: [
fixImportExtensions(),
createBasePlugin({}),
{
name: 'stackframe: force most files to be external',
setup(build) {
build.onResolve({ filter: /^.*$/m }, async (args) => {
if (args.kind === "entry-point" || customNoExternal.has(args.path)) {
return undefined;
}
return {
external: true,
};
});
},
}
],
});
}