2022-03-28 14:30:39 +00:00
/* eslint-disable no-console */
2022-04-13 16:42:50 +00:00
import replace from '@rollup/plugin-replace'
2021-12-28 10:56:23 +00:00
import yaml from '@rollup/plugin-yaml'
2021-12-28 16:38:39 +00:00
import legacy from '@vitejs/plugin-legacy'
2021-12-28 10:56:23 +00:00
import react from '@vitejs/plugin-react'
2022-01-26 08:53:15 +00:00
import fs from 'fs/promises'
2021-12-28 10:56:23 +00:00
import path from 'path'
2022-02-08 15:37:43 +00:00
import serveStatic from 'serve-static'
2022-06-29 20:35:03 +00:00
import { defineConfig , loadEnv , Plugin } from 'vite'
2022-06-30 17:32:57 +00:00
import { VitePWA } from 'vite-plugin-pwa'
2022-06-29 20:35:03 +00:00
import shimReactPdf from 'vite-plugin-shim-react-pdf'
2022-07-25 14:41:27 +00:00
import { runScriptOnFileChange } from './scripts/runScriptOnFileChange'
2022-08-03 15:00:50 +00:00
import { pwaOptions } from './vite-pwa-options'
2022-01-26 08:53:15 +00:00
2022-06-29 20:35:03 +00:00
const env = ( mode : string ) = > loadEnv ( mode , process . cwd ( ) , '' )
export default defineConfig ( ( { command , mode } ) = > ( {
2021-12-28 10:56:23 +00:00
resolve : {
2022-03-03 13:37:53 +00:00
alias : { '@' : path . resolve ( './source' ) } ,
2021-12-28 10:56:23 +00:00
extensions : [ '.js' , '.ts' , '.jsx' , '.tsx' , '.json' ] ,
} ,
2021-12-29 17:21:02 +00:00
publicDir : 'source/public' ,
2022-04-13 16:42:50 +00:00
build : {
2022-08-03 15:00:50 +00:00
sourcemap : ! true ,
rollupOptions : {
output : {
chunkFileNames : ( chunkInfo ) = > {
if ( chunkInfo . isDynamicEntry ) {
return 'assets/lazy_[name].[hash].js'
}
return 'assets/[name].[hash].js'
} ,
} ,
} ,
2022-04-13 16:42:50 +00:00
} ,
2021-12-28 10:56:23 +00:00
plugins : [
2022-07-25 14:41:27 +00:00
{
name : 'run-script-on-file-change' ,
apply : 'serve' ,
buildStart() {
if ( mode === 'development' ) {
void runScriptOnFileChange ( )
}
} ,
} ,
2022-04-13 16:42:50 +00:00
command === 'build' &&
replace ( {
__SENTRY_DEBUG__ : false ,
preventAssignment : false ,
} ) ,
2021-12-28 10:56:23 +00:00
react ( {
2022-03-03 13:37:53 +00:00
babel : { plugins : [ 'babel-plugin-styled-components' ] } ,
} ) ,
2022-05-31 09:53:07 +00:00
yaml ( ) ,
2022-01-12 11:08:24 +00:00
shimReactPdf ( ) ,
2021-12-28 10:56:23 +00:00
multipleSPA ( {
defaultSite : 'mon-entreprise' ,
templatePath : './source/template.html' ,
sites : {
'mon-entreprise' : {
2021-12-29 17:21:02 +00:00
lang : 'fr' ,
2022-04-03 15:40:00 +00:00
entry : '/source/entry-fr.tsx' ,
2021-12-28 10:56:23 +00:00
title :
"mon-entreprise.urssaf.fr : L'assistant officiel du créateur d'entreprise" ,
description :
'Du statut juridique à la première embauche, en passant par la simulation des cotisations, vous trouverez ici toutes les ressources pour démarrer votre activité.' ,
2022-08-03 15:00:50 +00:00
shareImage : '/logo-share.png' ,
2021-12-28 10:56:23 +00:00
} ,
infrance : {
2021-12-29 17:21:02 +00:00
lang : 'en' ,
2022-04-03 15:40:00 +00:00
entry : '/source/entry-en.tsx' ,
2021-12-28 10:56:23 +00:00
title :
'My company in France: A step-by-step guide to start a business in France' ,
description :
'Find the type of company that suits you and follow the steps to register your company. Discover the French social security system by simulating your hiring costs. Discover the procedures to hire in France and learn the basics of French labour law.' ,
2022-08-03 15:00:50 +00:00
shareImage : '/logo-mycompany-share.png' ,
2021-12-28 10:56:23 +00:00
} ,
} ,
} ) ,
2022-08-03 15:00:50 +00:00
VitePWA ( pwaOptions ) ,
2021-12-28 16:38:39 +00:00
legacy ( {
targets : [ 'defaults' , 'not IE 11' ] ,
} ) ,
2021-12-28 10:56:23 +00:00
] ,
2022-06-30 17:32:57 +00:00
esbuild : {
logOverride : { 'this-is-undefined-in-esm' : 'silent' } , // will be fixed in next version of @vitejs/plugin-react (actualy 1.3.2), issue https://github.com/vitejs/vite/pull/8674
} ,
2022-02-10 11:07:19 +00:00
server : {
2022-06-29 20:35:03 +00:00
hmr : {
clientPort :
typeof env ( mode ) . HMR_CLIENT_PORT !== 'undefined'
? parseInt ( env ( mode ) . HMR_CLIENT_PORT )
: undefined ,
} ,
2022-02-10 11:07:19 +00:00
// Keep watching changes in the publicodes package to support live reload
// when we iterate on publicodes logic.
// https://vitejs.dev/config/#server-watch
watch : {
ignored : [
'!**/node_modules/publicodes/**' ,
'!**/node_modules/publicodes-react/**' ,
] ,
} ,
2022-06-02 11:30:54 +00:00
proxy : {
'/api' : 'http://localhost:3004' ,
2022-06-29 20:35:03 +00:00
'/twemoji' : {
target : 'https://twemoji.maxcdn.com' ,
changeOrigin : true ,
rewrite : ( path ) = > path . replace ( /^\/twemoji/ , '' ) ,
} ,
2022-06-02 11:30:54 +00:00
} ,
2022-02-10 11:07:19 +00:00
} ,
optimizeDeps : {
2022-04-03 15:40:00 +00:00
entries : [ './source/entry-fr.tsx' , './source/entry-en.tsx' ] ,
2022-02-10 11:07:19 +00:00
exclude : [ 'publicodes-react' , 'publicodes' ] ,
2022-03-10 09:26:15 +00:00
// Optimize cjs deps from publicodes
include : [ 'publicodes > moo' , 'publicodes > nearley' , 'yaml' ] ,
2022-02-10 11:07:19 +00:00
} ,
2022-03-03 13:37:53 +00:00
} ) )
2021-12-28 10:56:23 +00:00
type MultipleSPAOptions = {
defaultSite : string
templatePath : string
sites : Record < string , Record < string , string > >
}
/ * *
* A custom plugin to create multiple virtual html files from a template . Will
* generate distinct entry points and single - page application outputs .
* /
function multipleSPA ( options : MultipleSPAOptions ) : Plugin {
const fillTemplate = async ( siteName : string ) = > {
const siteData = options . sites [ siteName ]
const template = await fs . readFile ( options . templatePath , 'utf-8' )
const filledTemplate = template
. toString ( )
2022-02-09 09:26:56 +00:00
. replace ( /\{\{(.+)\}\}/g , ( _match , p1 ) = > siteData [ ( p1 as string ) . trim ( ) ] )
2022-03-28 14:30:39 +00:00
2021-12-28 10:56:23 +00:00
return filledTemplate
}
2022-03-28 14:30:39 +00:00
2021-12-28 10:56:23 +00:00
return {
name : 'multiple-spa' ,
enforce : 'pre' ,
configureServer ( vite ) {
2022-02-10 11:07:19 +00:00
// TODO: this middleware is specific to the "mon-entreprise" app and
// shouldn't be in the "multipleSPA" plugin
2022-02-08 15:37:43 +00:00
vite . middlewares . use (
'/simulateur-iframe-integration.js' ,
serveStatic ( new URL ( './dist' , import . meta . url ) . pathname , {
index : 'simulateur-iframe-integration.js' ,
} )
)
2021-12-28 10:56:23 +00:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
vite . middlewares . use ( async ( req , res , next ) = > {
2022-06-29 20:35:03 +00:00
const url = req . originalUrl ? . replace ( /^\/%2F/ , '/' )
2022-04-24 17:28:32 +00:00
const firstLevelDir = url ? . slice ( 1 ) . split ( '/' ) [ 0 ]
2022-04-26 13:16:02 +00:00
if ( url && /\?.*html-proxy/ . test ( url ) ) {
return next ( )
}
2022-06-30 17:32:57 +00:00
if ( url && [ '/' , '/index.html' ] . includes ( url ) ) {
2022-06-29 20:35:03 +00:00
res . writeHead ( 302 , { Location : '/' + options . defaultSite } ) . end ( )
2022-07-07 10:22:29 +00:00
}
// this condition is for the start:netlify script to match /mon-entreprise or /infrance
else if (
2022-06-29 20:35:03 +00:00
firstLevelDir &&
url &&
Object . keys ( options . sites )
. map ( ( site ) = > ` / ${ site } .html ` )
. includes ( url )
) {
const siteName = firstLevelDir . replace ( '.html' , '' )
const content = await vite . transformIndexHtml (
'/' + siteName ,
await fillTemplate ( siteName )
)
res . end ( content )
2021-12-28 10:56:23 +00:00
} else if (
2022-04-24 17:28:32 +00:00
firstLevelDir &&
Object . keys ( options . sites ) . some ( ( name ) = > firstLevelDir === name )
2021-12-28 10:56:23 +00:00
) {
2022-04-24 17:28:32 +00:00
const siteName = firstLevelDir
2021-12-28 10:56:23 +00:00
const content = await vite . transformIndexHtml (
2022-04-24 17:28:32 +00:00
url ,
await fillTemplate ( siteName )
2021-12-28 10:56:23 +00:00
)
res . end ( content )
} else {
next ( )
}
} )
} ,
config ( config , { command } ) {
if ( command === 'build' && ! config . build ? . ssr ) {
config . build = {
. . . config . build ,
rollupOptions : {
2022-08-03 15:00:50 +00:00
. . . config . build ? . rollupOptions ,
2021-12-28 10:56:23 +00:00
input : Object.fromEntries (
Object . keys ( options . sites ) . map ( ( name ) = > [
name ,
` virtual: ${ name } .html ` ,
] )
) ,
} ,
}
}
} ,
resolveId ( id ) {
2022-01-12 13:18:41 +00:00
const pathname = id . split ( '/' ) . slice ( - 1 ) [ 0 ]
2021-12-28 10:56:23 +00:00
if ( pathname ? . startsWith ( 'virtual:' ) ) {
return pathname . replace ( 'virtual:' , '' )
}
2022-03-28 14:30:39 +00:00
2021-12-28 10:56:23 +00:00
return null
} ,
async load ( id ) {
if (
Object . keys ( options . sites ) . some ( ( name ) = > id . endsWith ( name + '.html' ) )
) {
2021-12-29 17:21:02 +00:00
return await fillTemplate ( id . replace ( /\.html$/ , '' ) )
2021-12-28 10:56:23 +00:00
}
} ,
}
}