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

94 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2023-09-12 07:37:17 +00:00
import { writeFileSync } from 'fs'
import { join, resolve } 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-09-12 07:37:17 +00:00
import { objectTransform } from '../source/utils'
2023-02-06 16:31:32 +00:00
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: {
2023-09-12 07:37:17 +00:00
alias: [{ find: '@', replacement: resolve('./source') }],
},
build: {
2023-09-12 07:37:17 +00:00
outDir: './dist',
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 () => {
2023-09-12 07:37:17 +00:00
const path = join(
import.meta.url,
'../../dist/builded-simulation-data.js'
)
console.log('path', path)
2023-02-06 10:32:25 +00:00
type PageConfigType = {
default: Record<string, Omit<PageConfig, 'component'>>
}
const algoliaUpdate = ((await import(path)) as PageConfigType).default
2023-02-06 10:32:25 +00:00
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)
},
},
],
})