2023-09-19 17:52:36 +00:00
|
|
|
/// <reference types="vitest" />
|
2022-03-28 14:30:39 +00:00
|
|
|
/* eslint-disable no-console */
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2023-06-15 13:57:48 +00:00
|
|
|
import path from 'path'
|
|
|
|
|
2022-04-13 16:42:50 +00:00
|
|
|
import replace from '@rollup/plugin-replace'
|
2023-02-08 09:09:19 +00:00
|
|
|
import yaml, { ValidYamlType } from '@rollup/plugin-yaml'
|
2023-09-25 14:01:42 +00:00
|
|
|
import { sentryVitePlugin } from '@sentry/vite-plugin'
|
2021-12-28 16:38:39 +00:00
|
|
|
import legacy from '@vitejs/plugin-legacy'
|
2023-03-01 11:48:46 +00:00
|
|
|
import react from '@vitejs/plugin-react-swc'
|
2023-09-25 17:59:53 +00:00
|
|
|
import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite'
|
2022-06-30 17:32:57 +00:00
|
|
|
import { VitePWA } from 'vite-plugin-pwa'
|
2022-11-03 16:32:04 +00:00
|
|
|
|
2023-09-12 07:37:17 +00:00
|
|
|
import { multipleSPA } from './build/multiple-SPA'
|
|
|
|
import { pwaOptions } from './build/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(), '')
|
|
|
|
|
2023-09-25 14:01:42 +00:00
|
|
|
const branch = (mode: string) => getBranch(mode)
|
|
|
|
|
|
|
|
const sentryReleaseName = (mode: string) =>
|
|
|
|
env(mode).VITE_GITHUB_SHA
|
2024-04-16 16:28:03 +00:00
|
|
|
? `${branch(mode).split('/').at(-1)}-` +
|
|
|
|
env(mode).VITE_GITHUB_SHA?.substring(0, 7)
|
2023-09-25 14:01:42 +00:00
|
|
|
: undefined
|
|
|
|
|
2022-06-29 20:35:03 +00:00
|
|
|
export default defineConfig(({ command, mode }) => ({
|
2021-12-28 10:56:23 +00:00
|
|
|
resolve: {
|
2023-02-01 00:28:35 +00:00
|
|
|
alias: [{ find: '@', replacement: 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: {
|
2023-09-29 13:00:41 +00:00
|
|
|
minify: process.env.NODE_ENV === 'production' ? 'esbuild' : false,
|
2022-08-03 16:33:43 +00:00
|
|
|
sourcemap: true,
|
2022-08-03 15:00:50 +00:00
|
|
|
rollupOptions: {
|
|
|
|
output: {
|
2023-09-25 17:59:53 +00:00
|
|
|
hoistTransitiveImports: false,
|
|
|
|
manualChunks: (id) => {
|
|
|
|
if (id.includes('modele-social')) {
|
|
|
|
return 'modele-social'
|
|
|
|
}
|
|
|
|
},
|
2022-08-03 15:00:50 +00:00
|
|
|
chunkFileNames: (chunkInfo) => {
|
|
|
|
if (chunkInfo.isDynamicEntry) {
|
|
|
|
return 'assets/lazy_[name].[hash].js'
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'assets/[name].[hash].js'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-13 16:42:50 +00:00
|
|
|
},
|
2022-11-22 15:11:08 +00:00
|
|
|
define: {
|
2023-09-25 14:01:42 +00:00
|
|
|
BRANCH_NAME: JSON.stringify(branch(mode)),
|
2022-11-22 15:11:08 +00:00
|
|
|
IS_DEVELOPMENT: mode === 'development',
|
|
|
|
IS_STAGING: mode === 'production' && !isProductionBranch(mode),
|
|
|
|
IS_PRODUCTION: mode === 'production' && isProductionBranch(mode),
|
2023-09-27 17:26:34 +00:00
|
|
|
SENTRY_RELEASE_NAME: JSON.stringify(sentryReleaseName(mode)),
|
2022-11-22 15:11:08 +00:00
|
|
|
},
|
2021-12-28 10:56:23 +00:00
|
|
|
plugins: [
|
2022-04-13 16:42:50 +00:00
|
|
|
command === 'build' &&
|
|
|
|
replace({
|
|
|
|
__SENTRY_DEBUG__: false,
|
|
|
|
preventAssignment: false,
|
|
|
|
}),
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2023-09-12 15:47:37 +00:00
|
|
|
react(),
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2023-02-08 09:09:19 +00:00
|
|
|
yaml({
|
|
|
|
transform(data, filePath) {
|
|
|
|
return filePath.endsWith('/rules-en.yaml')
|
|
|
|
? cleanAutomaticTag(data)
|
|
|
|
: data
|
|
|
|
},
|
|
|
|
}),
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2021-12-28 10:56:23 +00:00
|
|
|
multipleSPA({
|
|
|
|
defaultSite: 'mon-entreprise',
|
2023-02-23 16:15:57 +00:00
|
|
|
templatePath: './source/entries/template.html',
|
2021-12-28 10:56:23 +00:00
|
|
|
sites: {
|
|
|
|
'mon-entreprise': {
|
2021-12-29 17:21:02 +00:00
|
|
|
lang: 'fr',
|
2023-02-23 16:15:57 +00:00
|
|
|
entry: '/source/entries/entry-fr.tsx',
|
2023-05-30 12:31:01 +00:00
|
|
|
logo: '/source/assets/images/logo-monentreprise.svg',
|
|
|
|
logoAlt: 'Logo mon-entreprise, site Urssaf',
|
2021-12-28 10:56:23 +00:00
|
|
|
},
|
|
|
|
infrance: {
|
2021-12-29 17:21:02 +00:00
|
|
|
lang: 'en',
|
2023-02-23 16:15:57 +00:00
|
|
|
entry: '/source/entries/entry-en.tsx',
|
2023-05-30 12:31:01 +00:00
|
|
|
logo: '/logo-mycompany-share.png',
|
|
|
|
logoAlt: 'Logo My company in France by Urssaf',
|
2021-12-28 10:56:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2022-08-03 15:00:50 +00:00
|
|
|
VitePWA(pwaOptions),
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2021-12-28 16:38:39 +00:00
|
|
|
legacy({
|
|
|
|
targets: ['defaults', 'not IE 11'],
|
|
|
|
}),
|
2023-09-25 14:01:42 +00:00
|
|
|
|
2023-09-25 17:59:53 +00:00
|
|
|
splitVendorChunkPlugin(),
|
|
|
|
|
2023-09-25 14:01:42 +00:00
|
|
|
sentryVitePlugin({
|
|
|
|
org: 'betagouv',
|
|
|
|
project: 'mon-entreprise',
|
|
|
|
url: 'https://sentry.incubateur.net/',
|
|
|
|
authToken: process.env.SENTRY_AUTH_TOKEN,
|
|
|
|
telemetry: false,
|
|
|
|
release: {
|
|
|
|
// Use same release name as the one used in the app.
|
|
|
|
name: sentryReleaseName(mode),
|
2023-09-27 17:26:34 +00:00
|
|
|
inject: false, // Avoid adding imports with a hash in chunks, as this causes long term caching issues.
|
2023-09-25 14:01:42 +00:00
|
|
|
uploadLegacySourcemaps: {
|
|
|
|
paths: ['./dist'],
|
|
|
|
ignore: ['./node_modules'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2021-12-28 10:56:23 +00:00
|
|
|
],
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2022-02-10 11:07:19 +00:00
|
|
|
server: {
|
2022-11-15 14:26:06 +00:00
|
|
|
port: 3000,
|
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/**',
|
2023-12-14 09:54:17 +00:00
|
|
|
'!**/node_modules/@publicodes\\/react-ui/**',
|
2022-02-10 11:07:19 +00:00
|
|
|
],
|
|
|
|
},
|
2022-06-02 11:30:54 +00:00
|
|
|
proxy: {
|
|
|
|
'/api': 'http://localhost:3004',
|
2022-06-29 20:35:03 +00:00
|
|
|
'/twemoji': {
|
2023-01-16 12:24:23 +00:00
|
|
|
target: 'https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/',
|
2022-06-29 20:35:03 +00:00
|
|
|
changeOrigin: true,
|
|
|
|
rewrite: (path) => path.replace(/^\/twemoji/, ''),
|
2022-09-13 17:32:16 +00:00
|
|
|
timeout: 3 * 1000,
|
2022-06-29 20:35:03 +00:00
|
|
|
},
|
2022-06-02 11:30:54 +00:00
|
|
|
},
|
2022-02-10 11:07:19 +00:00
|
|
|
},
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2022-02-10 11:07:19 +00:00
|
|
|
optimizeDeps: {
|
2023-02-23 16:15:57 +00:00
|
|
|
entries: ['./source/entries/entry-fr.tsx', './source/entries/entry-en.tsx'],
|
2023-12-14 09:54:17 +00:00
|
|
|
include: ['@publicodes\\/react-ui > react/jsx-runtime'],
|
|
|
|
exclude: ['@publicodes\\/react-ui', 'publicodes'],
|
2022-02-10 11:07:19 +00:00
|
|
|
},
|
2023-09-12 07:37:17 +00:00
|
|
|
|
2022-11-22 11:12:17 +00:00
|
|
|
ssr: {
|
|
|
|
/**
|
|
|
|
* Prevent listed dependencies from being externalized for SSR build cause some
|
2023-09-12 07:37:17 +00:00
|
|
|
* packages are not esm ready or package.json setup seems wrong:
|
2022-11-22 11:12:17 +00:00
|
|
|
*/
|
2023-09-12 07:37:17 +00:00
|
|
|
noExternal: [/tslib/],
|
2022-11-22 11:12:17 +00:00
|
|
|
},
|
2024-02-16 14:38:02 +00:00
|
|
|
test: {
|
|
|
|
environmentMatchGlobs: [
|
|
|
|
// all tests in source with tsx will run in happy-dom (component tests)
|
|
|
|
['source/**/*.test.tsx', 'happy-dom'],
|
|
|
|
],
|
|
|
|
setupFiles: ['./vitest-setup.ts'],
|
|
|
|
},
|
2022-03-03 13:37:53 +00:00
|
|
|
}))
|
2021-12-28 10:56:23 +00:00
|
|
|
|
|
|
|
/**
|
2023-09-12 07:37:17 +00:00
|
|
|
* We use this function to hide some features in production while keeping them
|
|
|
|
* in feature-branches. In case we do A/B testing with several branches served
|
|
|
|
* in production, we should add the public faced branch names in the test below.
|
|
|
|
* This is different from the import.meta.env.MODE in that a feature branch may
|
|
|
|
* be build in production mode (with the NODE_ENV) but we may still want to show
|
|
|
|
* or hide some features.
|
2021-12-28 10:56:23 +00:00
|
|
|
*/
|
2023-09-12 07:37:17 +00:00
|
|
|
const isProductionBranch = (mode: string) => {
|
|
|
|
return ['master', 'next'].includes(getBranch(mode))
|
2021-12-28 10:56:23 +00:00
|
|
|
}
|
2022-11-22 15:11:08 +00:00
|
|
|
|
2023-09-12 07:37:17 +00:00
|
|
|
const getBranch = (mode: string) => {
|
2022-11-22 15:11:08 +00:00
|
|
|
let branch: string | undefined = env(mode)
|
|
|
|
.VITE_GITHUB_REF?.split('/')
|
|
|
|
?.slice(-1)?.[0]
|
|
|
|
|
|
|
|
if (branch === 'merge') {
|
|
|
|
branch = env(mode).VITE_GITHUB_HEAD_REF
|
|
|
|
}
|
|
|
|
|
|
|
|
return branch ?? ''
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:09:19 +00:00
|
|
|
const cleanAutomaticTag = (data: ValidYamlType): ValidYamlType => {
|
|
|
|
if (typeof data === 'string' && data.startsWith('[automatic] ')) {
|
|
|
|
return data.replace('[automatic] ', '')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
return data.map((val) => cleanAutomaticTag(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data && typeof data === 'object') {
|
|
|
|
return Object.fromEntries<ValidYamlType>(
|
|
|
|
Object.entries(data).map(([key, val]) => [key, cleanAutomaticTag(val)])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|