Skip to content
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: 2 additions & 0 deletions packages/config/src/vite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type Options = {
exclude?: Array<string>
/** Directory where build output will be placed, e.g. `./dist` */
outDir?: string
/** Generate CJS output, defaults to `true` */
cjs?: boolean
/** Optional path to a custom tsconfig file, defaults to `./tsconfig.json` */
tsconfigPath?: string
/** Additional dependencies to externalize if not detected by `vite-plugin-externalize-deps` */
Expand Down
47 changes: 25 additions & 22 deletions packages/config/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function ensureImportFileExtension({ content, extension }) {
*/
export const tanstackViteConfig = (options) => {
const outDir = options.outDir ?? 'dist'
const cjs = options.cjs ?? true

return defineConfig({
plugins: [
Expand Down Expand Up @@ -60,35 +61,37 @@ export const tanstackViteConfig = (options) => {
}
},
}),
dts({
outDir: `${outDir}/cjs`,
entryRoot: options.srcDir,
include: options.srcDir,
exclude: options.exclude,
tsconfigPath: options.tsconfigPath,
compilerOptions: {
module: 1, // CommonJS
declarationMap: false,
},
beforeWriteFile: (filePath, content) => ({
filePath: filePath.replace('.d.ts', '.d.cts'),
content: ensureImportFileExtension({ content, extension: 'cjs' }),
}),
afterDiagnostic: (diagnostics) => {
if (diagnostics.length > 0) {
console.error('Please fix the above type errors')
process.exit(1)
}
},
}),
cjs
? dts({
outDir: `${outDir}/cjs`,
entryRoot: options.srcDir,
include: options.srcDir,
exclude: options.exclude,
tsconfigPath: options.tsconfigPath,
compilerOptions: {
module: 1, // CommonJS
declarationMap: false,
},
beforeWriteFile: (filePath, content) => ({
filePath: filePath.replace('.d.ts', '.d.cts'),
content: ensureImportFileExtension({ content, extension: 'cjs' }),
}),
afterDiagnostic: (diagnostics) => {
if (diagnostics.length > 0) {
console.error('Please fix the above type errors')
process.exit(1)
}
},
})
: undefined,
],
build: {
outDir,
minify: false,
sourcemap: true,
lib: {
entry: options.entry,
formats: ['es', 'cjs'],
formats: cjs ? ['es', 'cjs'] : ['es'],
fileName: (format) => {
if (format === 'cjs') return 'cjs/[name].cjs'
return 'esm/[name].js'
Expand Down