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

91 lines
2.2 KiB
TypeScript
Raw Normal View History

import { unlinkSync, writeFileSync } from 'fs'
import path from 'path'
2023-06-15 13:57:48 +00:00
import { defineConfig } from 'vite'
import { PageConfig } from '@/pages/simulateurs/_configs/types'
2023-02-02 11:12:45 +00:00
2023-02-06 16:31:32 +00:00
import { objectTransform } from './source/utils'
const filterOgImage = (obj: Record<string, Omit<PageConfig, 'component'>>) =>
objectTransform(obj, (entries) => {
return entries.map(([key, val]) => {
if (
'meta' in val &&
val.meta != null &&
typeof val.meta === 'object' &&
'ogImage' in val.meta
) {
delete val.meta.ogImage
}
return [key, val]
})
})
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
2023-02-09 10:25:08 +00:00
const isConfigFile = /pages\/.+\/config\.tsx?$/.test(id)
2023-02-06 16:31:32 +00:00
2023-02-02 13:11:41 +00:00
if (isConfigFile) {
// eslint-disable-next-line no-console
console.log('transform:', id)
}
return isConfigFile
2023-02-06 16:31:32 +00:00
? code.replace(/^\s+(component|seoExplanations):?[^,]*,/gm, '')
: code
},
},
{
name: 'postbuild-commands',
2023-02-06 10:32:25 +00:00
closeBundle: () => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setTimeout(async () => {
const path = './builded-simulation-data.js'
type PageConfigType = {
default: Record<string, Omit<PageConfig, 'component'>>
}
const algoliaUpdate = ((await import(path)) as PageConfigType).default
2023-02-06 10:32:25 +00:00
unlinkSync(path)
writeFileSync(
'./source/public/simulation-data.json',
2023-02-06 16:31:32 +00:00
JSON.stringify(filterOgImage(algoliaUpdate))
2023-02-06 10:32:25 +00:00
)
writeFileSync(
'./source/public/simulation-data-title.json',
JSON.stringify(
Object.fromEntries(
Object.entries(algoliaUpdate).map(
([, { iframePath, title }]) => [iframePath, { title }]
)
2023-02-06 10:32:25 +00:00
)
2023-02-02 11:12:45 +00:00
)
)
2023-02-06 10:32:25 +00:00
// eslint-disable-next-line no-console
console.log('done!')
}, 1000)
},
},
],
})