Skip to content

Commit

Permalink
feat(vite): add option to disable CJS output (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins authored Nov 5, 2024
1 parent 7c35162 commit a1ec1c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
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

0 comments on commit a1ec1c7

Please sign in to comment.