Skip to content

Commit a1ec1c7

Browse files
feat(vite): add option to disable CJS output (#175)
1 parent 7c35162 commit a1ec1c7

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

packages/config/src/vite/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type Options = {
99
exclude?: Array<string>
1010
/** Directory where build output will be placed, e.g. `./dist` */
1111
outDir?: string
12+
/** Generate CJS output, defaults to `true` */
13+
cjs?: boolean
1214
/** Optional path to a custom tsconfig file, defaults to `./tsconfig.json` */
1315
tsconfigPath?: string
1416
/** Additional dependencies to externalize if not detected by `vite-plugin-externalize-deps` */

packages/config/src/vite/index.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function ensureImportFileExtension({ content, extension }) {
3131
*/
3232
export const tanstackViteConfig = (options) => {
3333
const outDir = options.outDir ?? 'dist'
34+
const cjs = options.cjs ?? true
3435

3536
return defineConfig({
3637
plugins: [
@@ -60,35 +61,37 @@ export const tanstackViteConfig = (options) => {
6061
}
6162
},
6263
}),
63-
dts({
64-
outDir: `${outDir}/cjs`,
65-
entryRoot: options.srcDir,
66-
include: options.srcDir,
67-
exclude: options.exclude,
68-
tsconfigPath: options.tsconfigPath,
69-
compilerOptions: {
70-
module: 1, // CommonJS
71-
declarationMap: false,
72-
},
73-
beforeWriteFile: (filePath, content) => ({
74-
filePath: filePath.replace('.d.ts', '.d.cts'),
75-
content: ensureImportFileExtension({ content, extension: 'cjs' }),
76-
}),
77-
afterDiagnostic: (diagnostics) => {
78-
if (diagnostics.length > 0) {
79-
console.error('Please fix the above type errors')
80-
process.exit(1)
81-
}
82-
},
83-
}),
64+
cjs
65+
? dts({
66+
outDir: `${outDir}/cjs`,
67+
entryRoot: options.srcDir,
68+
include: options.srcDir,
69+
exclude: options.exclude,
70+
tsconfigPath: options.tsconfigPath,
71+
compilerOptions: {
72+
module: 1, // CommonJS
73+
declarationMap: false,
74+
},
75+
beforeWriteFile: (filePath, content) => ({
76+
filePath: filePath.replace('.d.ts', '.d.cts'),
77+
content: ensureImportFileExtension({ content, extension: 'cjs' }),
78+
}),
79+
afterDiagnostic: (diagnostics) => {
80+
if (diagnostics.length > 0) {
81+
console.error('Please fix the above type errors')
82+
process.exit(1)
83+
}
84+
},
85+
})
86+
: undefined,
8487
],
8588
build: {
8689
outDir,
8790
minify: false,
8891
sourcemap: true,
8992
lib: {
9093
entry: options.entry,
91-
formats: ['es', 'cjs'],
94+
formats: cjs ? ['es', 'cjs'] : ['es'],
9295
fileName: (format) => {
9396
if (format === 'cjs') return 'cjs/[name].cjs'
9497
return 'esm/[name].js'

0 commit comments

Comments
 (0)