2024-12-03 09:55:37 +03:00
import type { ConfigEnv , UserConfig } from 'vite'
2024-08-21 11:05:07 +03:00
/// <reference types="vitest" />
2024-11-16 16:00:12 +03:00
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
2024-08-21 11:05:07 +03:00
2024-12-03 09:55:37 +03:00
import { fileURLToPath } from 'node:url'
2024-11-16 16:00:12 +03:00
import { fumanBuild } from '@fuman/build/vite'
2024-08-21 11:05:07 +03:00
import { nodeExternals } from 'rollup-plugin-node-externals'
import dts from 'vite-plugin-dts'
const rootDir = fileURLToPath ( new URL ( '..' , import . meta . url ) )
export default async ( env : ConfigEnv ) : Promise < UserConfig > = > {
if ( env . command !== 'build' ) {
throw new Error ( 'This config is only for building' )
}
2025-01-01 18:45:57 +03:00
const packageJson = JSON . parse ( readFileSync ( join ( process . cwd ( ) , 'package.json' ) , 'utf8' ) )
2024-08-21 11:05:07 +03:00
const CJS_DEPRECATION_WARNING = `
if ( typeof globalThis !== 'undefined' && ! globalThis . _MTCUTE_CJS_DEPRECATION_WARNED ) {
globalThis . _MTCUTE_CJS_DEPRECATION_WARNED = true
2025-01-01 18:45:57 +03:00
console . warn ( "[${packageJson.name}] CommonJS support is deprecated and will be removed in 0.25.0. Please consider switching to ESM, it's " + ( new Date ( ) ) . getFullYear ( ) + " already." )
2024-08-21 11:05:07 +03:00
console . warn ( "[${packageJson.name}] Learn more about switching to ESM: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c" )
}
` .trim()
return {
build : {
rollupOptions : {
plugins : [
{
2024-11-16 16:00:12 +03:00
name : 'mtcute-cjs-deprecated' ,
2024-08-21 11:05:07 +03:00
renderChunk ( code , chunk , options ) {
if ( options . format !== 'cjs' ) return null
return ` ${ CJS_DEPRECATION_WARNING } \ n ${ code } `
} ,
} ,
] ,
2024-11-20 19:43:32 +03:00
output : {
// re-exported namespaces can't be tree-shaken when bundled
// see: https://github.com/rollup/rollup/issues/5161
preserveModules : true ,
} ,
2024-08-21 11:05:07 +03:00
} ,
minify : false ,
outDir : 'dist' ,
emptyOutDir : true ,
2024-11-20 19:43:32 +03:00
target : 'esnext' ,
2024-08-21 11:05:07 +03:00
} ,
plugins : [
2024-11-16 16:00:12 +03:00
nodeExternals ( {
builtinsPrefix : 'ignore' ,
} ) ,
fumanBuild ( {
root : rootDir ,
autoSideEffectsFalse : true ,
2024-11-20 19:43:32 +03:00
insertTypesEntry : true ,
2024-11-16 16:00:12 +03:00
} ) ,
2024-08-21 11:05:07 +03:00
dts ( {
// broken; see https://github.com/qmhc/vite-plugin-dts/issues/321, https://github.com/microsoft/rushstack/issues/3557
// rollupTypes: true,
} ) ,
] ,
}
}