-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvite.config.mts
238 lines (230 loc) · 6.94 KB
/
vite.config.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/// <reference types="vitest/config" />
// https://vitejs.dev/config/
import {codecovVitePlugin} from '@codecov/vite-plugin';
import replace from '@rollup/plugin-replace';
import {sentryVitePlugin} from '@sentry/vite-plugin';
import react from '@vitejs/plugin-react';
import path from 'path';
import type {OutputOptions} from 'rollup';
import {defineConfig} from 'vite';
import jsconfigPaths from 'vite-jsconfig-paths';
import eslint from 'vite-plugin-eslint2';
import {coverageConfigDefaults} from 'vitest/config';
import {cjsTokens, ejsPlugin} from './build/plugins.mjs';
import {packageRegexes} from './build/utils.mjs';
// type definition for our custom envvars
declare global {
namespace NodeJS {
interface ProcessEnv {
BUILD_TARGET: 'umd' | 'esm' | 'esm-bundle' | undefined;
}
}
}
const buildTarget = process.env.BUILD_TARGET || 'umd';
const buildTargetDefined = process.env.BUILD_TARGET !== undefined;
/**
* Rollup output options for ESM build, which is what we package in the NPM package
* under the 'esm' subdirectory.
*
* The ESM package is experimental. Known issues:
* @fixme
*
* - the react-intl translations are not distributed yet (also broken in CRA/babel build!)
*/
const esmOutput = {
dir: 'dist/esm',
format: 'esm',
preserveModules: true,
preserveModulesRoot: 'src',
entryFileNames: '[name].js',
assetFileNames: ({name}) => {
if (name?.endsWith('.css')) {
return '[name].[ext]';
}
return 'static/media/[name].[hash:8].[ext]';
},
} satisfies OutputOptions;
const esmBundleOutput = {
dir: 'dist',
format: 'esm',
preserveModules: false,
entryFileNames: 'open-forms-sdk.mjs',
assetFileNames: ({name}) => {
if (name === 'style.css') {
return 'open-forms-sdk.css';
}
return 'static/media/[name].[hash:8].[ext]';
},
inlineDynamicImports: false,
} satisfies OutputOptions;
/**
* Rollup output options for UMD bundle, included in the NPM package but
* the primary distribution mechanism is in a Docker image.
*
* @deprecated - it's better to use the ESM bundle which has separate chunks.
*/
const umdOutput = {
dir: 'dist',
format: 'umd',
exports: 'named',
name: 'OpenForms',
generatedCode: 'es2015',
entryFileNames: 'open-forms-sdk.js',
assetFileNames: ({name}) => {
if (name === 'style.css') {
return 'open-forms-sdk.css';
}
return 'static/media/[name].[hash:8].[ext]';
},
inlineDynamicImports: true,
} satisfies OutputOptions;
const getOutput = (buildTarget: typeof process.env.BUILD_TARGET): OutputOptions => {
switch (buildTarget) {
case 'esm-bundle': {
return esmBundleOutput;
}
case 'esm': {
return esmOutput;
}
case 'umd':
default: {
return umdOutput;
}
}
};
export default defineConfig(({mode}) => ({
base: './',
publicDir: false,
server: {
port: 3000,
},
plugins: [
// BIG DISCLAIMER - Vite only processes files with the .jsx or .tsx extension with
// babel, and changing this configuration is... cumbersome and comes with a performance
// penalty. This manifests if you're using react-intl in .js/.mjs/.ts files etc., as
// they don't get transformed to inject the message ID. The solution is to rename the
// file extension to .jsx/.tsx
react({babel: {babelrc: true}}),
jsconfigPaths(),
eslint({
build: true,
emitErrorAsWarning: mode === 'development',
}),
cjsTokens(),
ejsPlugin(),
// @formio/protected-eval requires js-interpeter (a forked version), which includes
// this['Interpreter'] = Interpreter. When this is bundled, it becomes a strict module
// and 'this' doesn't point to the window object, but is undefined, and causes the SDK
// to crash.
replace({
preventAssignment: false,
include: ['**/node_modules/js-interpreter/interpreter.js'],
delimiters: ['', ''],
values: {
"this\['Interpreter'\]": "window['Interpreter']",
},
}),
/**
* Plugin to ignore (S)CSS when bundling to UMD bundle target, since we use the ESM
* bundle to generate these.
*
* @todo Remove this when we drop the UMD bundle entirely (in 4.0?)
*/
{
name: 'ignore-styles-esm-bundle',
transform(code, id) {
if (!buildTargetDefined) return;
if (buildTarget === 'umd' && (id.endsWith('.css') || id.endsWith('scss'))) {
// skip processing
return {code: '', map: null};
}
},
},
sentryVitePlugin({
silent: mode === 'development',
release: {
create: false,
inject: false,
},
sourcemaps: {
disable: true,
},
bundleSizeOptimizations: {
excludeDebugStatements: true,
excludeTracing: true,
excludeReplayCanvas: true,
excludeReplayShadowDom: true,
excludeReplayIframe: true,
excludeReplayWorker: true,
},
telemetry: false,
}),
// must be last!
codecovVitePlugin({
enableBundleAnalysis: buildTarget !== 'esm' && process.env.CODECOV_TOKEN !== undefined,
bundleName: '@open-formulieren/sdk',
uploadToken: process.env.CODECOV_TOKEN,
}),
],
resolve: {
alias: {
// ensure react-router imports don't end up with multiple copies/installations. See
// https://github.com/remix-run/react-router/issues/12785 for more context.
'react-router/dom': path.resolve(
'./node_modules/react-router/dist/development/dom-export.mjs'
),
'react-router': path.resolve('./node_modules/react-router/dist/development/index.mjs'),
},
},
build: {
target: 'modules', // the default
assetsInlineLimit: 8 * 1024, // 8 KiB
cssCodeSplit: false,
sourcemap: buildTarget !== 'esm',
outDir: 'dist',
// we write the .mjs file to the same directory
emptyOutDir: buildTarget !== 'esm-bundle',
rollupOptions: {
input: 'src/sdk.jsx',
// do not externalize anything in UMD build - bundle everything
external: buildTarget === 'esm' ? packageRegexes : undefined,
output: getOutput(buildTarget),
preserveEntrySignatures: 'strict',
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: `$fa-font-path: '@fortawesome/fontawesome-free/webfonts/';`,
charset: false,
},
},
},
test: {
environment: 'jsdom',
environmentOptions: {
jsdom: {
url: 'http://localhost',
},
},
globals: true, // for compatibility with jest
// See https://vitest.dev/guide/migration.html#fake-timers-defaults
fakeTimers: {
toFake: ['setTimeout', 'clearTimeout', 'Date'],
},
setupFiles: ['./src/vitest.setup.mjs'],
coverage: {
provider: 'istanbul',
include: ['src/**/*.{js,jsx,ts,tsx}'],
exclude: [
'src/**/*.d.ts',
'src/**/*.stories.{js,jsx,ts,tsx}',
'src/api-mocks/*',
'src/**/mocks.{js,jsx}',
'src/story-utils/*',
...coverageConfigDefaults.exclude,
],
reporter: ['text', 'cobertura', 'html'],
},
},
}));