|
|
import { defineConfig, mergeConfig } from 'vitest/config' |
|
|
import { externalizeDeps } from 'vite-plugin-externalize-deps' |
|
|
import tsconfigPaths from 'vite-tsconfig-paths' |
|
|
import dts from 'vite-plugin-dts' |
|
|
import packageJson from './package.json' |
|
|
import type { Options } from '@tanstack/config/vite' |
|
|
|
|
|
function ensureImportFileExtension({ |
|
|
content, |
|
|
extension, |
|
|
}: { |
|
|
content: string |
|
|
extension: string |
|
|
}) { |
|
|
|
|
|
content = content.replace( |
|
|
/(im|ex)port\s[\w{}/*\s,]+from\s['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm, |
|
|
`$&.${extension}`, |
|
|
) |
|
|
|
|
|
|
|
|
content = content.replace( |
|
|
/import\(['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm, |
|
|
`$&.${extension}`, |
|
|
) |
|
|
return content |
|
|
} |
|
|
|
|
|
const config = defineConfig({ |
|
|
|
|
|
resolve: { |
|
|
conditions: ['@tanstack/custom-condition'], |
|
|
}, |
|
|
environments: { |
|
|
ssr: { |
|
|
resolve: { |
|
|
conditions: ['@tanstack/custom-condition'], |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
test: { |
|
|
name: packageJson.name, |
|
|
dir: './src', |
|
|
watch: false, |
|
|
environment: 'jsdom', |
|
|
setupFiles: ['src/test-setup.ts'], |
|
|
coverage: { enabled: true, provider: 'istanbul', include: ['src/**/*'] }, |
|
|
typecheck: { enabled: true }, |
|
|
globals: true, |
|
|
restoreMocks: true, |
|
|
}, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const tanstackViteConfig = (options: Options) => { |
|
|
const outDir = options.outDir ?? 'dist' |
|
|
const cjs = options.cjs ?? true |
|
|
|
|
|
return defineConfig({ |
|
|
plugins: [ |
|
|
externalizeDeps({ include: options.externalDeps ?? [] }), |
|
|
tsconfigPaths({ |
|
|
projects: options.tsconfigPath ? [options.tsconfigPath] : undefined, |
|
|
}), |
|
|
dts({ |
|
|
outDir, |
|
|
entryRoot: options.srcDir, |
|
|
include: options.srcDir, |
|
|
exclude: options.exclude, |
|
|
tsconfigPath: options.tsconfigPath, |
|
|
compilerOptions: { |
|
|
module: 99, |
|
|
declarationMap: false, |
|
|
}, |
|
|
beforeWriteFile: (filePath, content) => { |
|
|
return { |
|
|
filePath, |
|
|
content: ensureImportFileExtension({ content, extension: 'js' }), |
|
|
} |
|
|
}, |
|
|
afterDiagnostic: (diagnostics) => { |
|
|
if (diagnostics.length > 0) { |
|
|
console.error('Please fix the above type errors') |
|
|
process.exit(1) |
|
|
} |
|
|
}, |
|
|
}), |
|
|
], |
|
|
build: { |
|
|
outDir, |
|
|
minify: false, |
|
|
sourcemap: true, |
|
|
lib: { |
|
|
entry: options.entry, |
|
|
formats: cjs ? ['es', 'cjs'] : ['es'], |
|
|
fileName: () => '[name].mjs', |
|
|
}, |
|
|
rollupOptions: { |
|
|
output: { |
|
|
preserveModules: true, |
|
|
preserveModulesRoot: 'src', |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
} |
|
|
|
|
|
export default mergeConfig( |
|
|
config, |
|
|
tanstackViteConfig({ |
|
|
cjs: false, |
|
|
entry: ['./src/index.ts'], |
|
|
exclude: ['./src/__tests__'], |
|
|
srcDir: './src', |
|
|
tsconfigPath: './tsconfig.prod.json', |
|
|
}), |
|
|
) |
|
|
|