2024-08-13 04:53:07 +03:00
|
|
|
import { resolve } from 'node:path'
|
|
|
|
|
2024-03-18 01:49:55 +03:00
|
|
|
import { defineConfig } from 'vite'
|
2024-08-13 04:53:07 +03:00
|
|
|
|
2024-12-03 09:55:37 +03:00
|
|
|
import { collectTestEntrypoints } from './vite-utils/collect-test-entrypoints'
|
2024-03-18 01:49:55 +03:00
|
|
|
import { fixupCjs } from './vite-utils/fixup-cjs'
|
|
|
|
import { testSetup } from './vite-utils/test-setup-plugin'
|
2024-02-20 00:46:09 +03:00
|
|
|
|
2024-04-25 05:25:56 +03:00
|
|
|
const POLYFILLS = resolve(__dirname, 'vite-utils/polyfills-bun.ts')
|
2024-03-18 01:49:55 +03:00
|
|
|
|
|
|
|
export default defineConfig({
|
|
|
|
build: {
|
|
|
|
lib: {
|
2024-08-13 04:53:07 +03:00
|
|
|
entry: process.env.ENTRYPOINT
|
|
|
|
? [process.env.ENTRYPOINT]
|
|
|
|
: collectTestEntrypoints({
|
|
|
|
// https://github.com/oven-sh/bun/issues/4145 prevents us from using vitest directly
|
|
|
|
// so we have to use bun's native test runner
|
|
|
|
skipPackages: ['create-bot', 'crypto-node'],
|
|
|
|
// bun:test doesn't support certain features of vitest, so we'll skip them for now
|
|
|
|
// https://github.com/oven-sh/bun/issues/1825
|
|
|
|
skipTests: [
|
2024-12-03 05:36:50 +03:00
|
|
|
// uses timers
|
2024-08-13 04:53:07 +03:00
|
|
|
'core/src/network/config-manager.test.ts',
|
|
|
|
'core/src/network/persistent-connection.test.ts',
|
2024-12-03 05:36:50 +03:00
|
|
|
// use fixtures
|
|
|
|
'convert/src/tdesktop/tdata.test.ts',
|
2024-08-13 04:53:07 +03:00
|
|
|
],
|
|
|
|
}),
|
2024-03-18 01:49:55 +03:00
|
|
|
formats: ['es'],
|
|
|
|
},
|
|
|
|
rollupOptions: {
|
|
|
|
external: [
|
2024-08-13 04:53:07 +03:00
|
|
|
/^(node|bun):.*/,
|
2024-03-18 01:49:55 +03:00
|
|
|
'vitest',
|
|
|
|
],
|
|
|
|
output: {
|
|
|
|
chunkFileNames: 'chunk-[hash].js',
|
|
|
|
entryFileNames: '[name]-[hash].test.js',
|
|
|
|
minifyInternalExports: false,
|
|
|
|
},
|
|
|
|
treeshake: false,
|
|
|
|
},
|
|
|
|
commonjsOptions: {
|
|
|
|
ignoreDynamicRequires: true,
|
|
|
|
},
|
2024-03-23 18:13:31 +03:00
|
|
|
outDir: process.env.OUT_DIR || 'dist/tests',
|
2024-03-18 01:49:55 +03:00
|
|
|
emptyOutDir: true,
|
|
|
|
target: 'esnext',
|
|
|
|
minify: false,
|
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
fixupCjs(),
|
|
|
|
{
|
2024-04-25 05:25:56 +03:00
|
|
|
name: 'polyfills',
|
2024-03-18 01:49:55 +03:00
|
|
|
transform(code) {
|
|
|
|
if (!code.includes('vitest')) return code
|
2024-12-06 00:14:29 +03:00
|
|
|
code = code.replace(/^import \{([^}]+)\} from ['"]vitest['"];?$/gm, (_, names) => {
|
2024-08-13 04:53:07 +03:00
|
|
|
const namesParsed = names.split(',').map(name => name.trim())
|
2024-03-18 01:49:55 +03:00
|
|
|
|
|
|
|
const namesFromFixup: string[] = []
|
|
|
|
const newNames = namesParsed
|
|
|
|
.map((name) => {
|
|
|
|
if (['expect', 'vi', 'it'].includes(name)) {
|
|
|
|
namesFromFixup.push(name)
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
})
|
|
|
|
.filter(Boolean)
|
|
|
|
|
|
|
|
let code = `import {${newNames.join(', ')}} from 'bun:test'`
|
|
|
|
|
|
|
|
if (namesFromFixup.length) {
|
2024-04-25 05:25:56 +03:00
|
|
|
code += `\nimport { ${namesFromFixup.join(', ')} } from '${POLYFILLS}'`
|
2024-03-18 01:49:55 +03:00
|
|
|
}
|
|
|
|
return code
|
|
|
|
})
|
|
|
|
return code
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'fix-wasm-load',
|
2024-08-13 04:53:07 +03:00
|
|
|
async transform(code) {
|
2024-03-18 01:49:55 +03:00
|
|
|
if (code.includes('@mtcute/wasm/mtcute.wasm')) {
|
2024-08-21 11:05:07 +03:00
|
|
|
return code.replace('@mtcute/wasm/mtcute.wasm', resolve(__dirname, '../packages/wasm/src/mtcute.wasm'))
|
|
|
|
}
|
|
|
|
if (code.includes('./mtcute.wasm')) {
|
|
|
|
return code.replace(/\.?\.\/mtcute\.wasm/, resolve(__dirname, '../packages/wasm/src/mtcute.wasm'))
|
2024-03-18 01:49:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
2024-08-13 04:53:07 +03:00
|
|
|
},
|
2024-03-18 01:49:55 +03:00
|
|
|
},
|
|
|
|
testSetup(),
|
|
|
|
],
|
2024-02-20 00:46:09 +03:00
|
|
|
define: {
|
2024-03-18 01:49:55 +03:00
|
|
|
'import.meta.env.TEST_ENV': '"bun"',
|
|
|
|
},
|
|
|
|
})
|