mon-entreprise/site/vite-build-simulation-data....

63 lines
1.5 KiB
TypeScript
Raw Normal View History

import { unlinkSync, writeFileSync } from 'fs'
import path from 'path'
import { defineConfig } from 'vite'
2023-02-02 11:12:45 +00:00
import { PageConfig } from '@/pages/Simulateurs/configs/types'
export default defineConfig({
resolve: {
alias: [{ find: '@', replacement: path.resolve('./source') }],
},
build: {
outDir: './',
target: 'esnext',
emptyOutDir: false,
lib: {
entry: './scripts/search/export-simulation-data.ts',
formats: ['es'],
fileName: 'builded-simulation-data',
},
},
plugins: [
{
name: 'remove-component-from-config',
enforce: 'pre',
transform(code, id) {
2023-02-02 11:12:45 +00:00
// Remove `component` and `seoExplanations` from config
return /Simulateurs\/.+\/_config\.ts$/.test(id)
2023-02-02 11:12:45 +00:00
? code.replace(/(component|seoExplanations):[^,]+,/g, '')
: code
},
},
{
name: 'postbuild-commands',
closeBundle: async () => {
const path = './builded-simulation-data.js'
2023-02-02 11:12:45 +00:00
type PageConfigType = {
default: Record<string, Omit<PageConfig, 'component'>>
}
const algoliaUpdate = ((await import(path)) as PageConfigType).default
unlinkSync(path)
writeFileSync(
2023-02-02 11:12:45 +00:00
'./source/public/simulation-data.json',
JSON.stringify(algoliaUpdate)
)
2023-02-02 11:12:45 +00:00
writeFileSync(
'./source/public/simulation-data-title.json',
JSON.stringify(
Object.fromEntries(
Object.entries(algoliaUpdate).map(([id, { title }]) => [
id,
{ title },
])
)
)
)
// eslint-disable-next-line no-console
console.log('done!')
},
},
],
})