diff --git a/packages/config/src/vite/index.d.ts b/packages/config/src/vite/index.d.ts index ed9270d..4139e9d 100644 --- a/packages/config/src/vite/index.d.ts +++ b/packages/config/src/vite/index.d.ts @@ -9,6 +9,8 @@ export type Options = { exclude?: Array /** 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` */ diff --git a/packages/config/src/vite/index.js b/packages/config/src/vite/index.js index 9ac4ee1..3737ae5 100644 --- a/packages/config/src/vite/index.js +++ b/packages/config/src/vite/index.js @@ -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: [ @@ -60,27 +61,29 @@ 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, @@ -88,7 +91,7 @@ export const tanstackViteConfig = (options) => { 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'