Skip to content

Commit e4b3a57

Browse files
committed
🐛 Define vite build dir based on build version
For release builds we want to the assets to be placed in `/dist/[release number]`. This logic was present in the previous build setup, with CRA. In the re-structure of the SDK project, #724, the build process of CRA was replaced with vite. But this logic rule was lost in the process
1 parent 5e94e35 commit e4b3a57

File tree

1 file changed

+187
-173
lines changed

1 file changed

+187
-173
lines changed

vite.config.mts

+187-173
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {sentryVitePlugin} from '@sentry/vite-plugin';
66
import react from '@vitejs/plugin-react';
77
import path from 'path';
88
import type {OutputOptions} from 'rollup';
9-
import {defineConfig} from 'vite';
9+
import {defineConfig, loadEnv} from 'vite';
1010
import jsconfigPaths from 'vite-jsconfig-paths';
1111
import eslint from 'vite-plugin-eslint2';
1212
import {coverageConfigDefaults} from 'vitest/config';
@@ -35,204 +35,218 @@ const buildTargetDefined = process.env.BUILD_TARGET !== undefined;
3535
*
3636
* - the react-intl translations are not distributed yet (also broken in CRA/babel build!)
3737
*/
38-
const esmOutput = {
39-
dir: 'dist/esm',
40-
format: 'esm',
41-
preserveModules: true,
42-
preserveModulesRoot: 'src',
43-
entryFileNames: '[name].js',
44-
assetFileNames: ({name}) => {
45-
if (name?.endsWith('.css')) {
46-
return '[name].[ext]';
47-
}
48-
return 'static/media/[name].[hash:8].[ext]';
49-
},
50-
} satisfies OutputOptions;
38+
const esmOutput = (buildDist: string) =>
39+
({
40+
dir: `${buildDist}/esm`,
41+
format: 'esm',
42+
preserveModules: true,
43+
preserveModulesRoot: 'src',
44+
entryFileNames: '[name].js',
45+
assetFileNames: ({name}) => {
46+
if (name?.endsWith('.css')) {
47+
return '[name].[ext]';
48+
}
49+
return 'static/media/[name].[hash:8].[ext]';
50+
},
51+
}) satisfies OutputOptions;
5152

52-
const esmBundleOutput = {
53-
dir: 'dist',
54-
format: 'esm',
55-
preserveModules: false,
56-
entryFileNames: 'open-forms-sdk.mjs',
57-
assetFileNames: ({name}) => {
58-
if (name === 'style.css') {
59-
return 'open-forms-sdk.css';
60-
}
61-
return 'static/media/[name].[hash:8].[ext]';
62-
},
63-
inlineDynamicImports: false,
64-
} satisfies OutputOptions;
53+
const esmBundleOutput = (buildDist: string) =>
54+
({
55+
dir: buildDist,
56+
format: 'esm',
57+
preserveModules: false,
58+
entryFileNames: 'open-forms-sdk.mjs',
59+
assetFileNames: ({name}) => {
60+
if (name === 'style.css') {
61+
return 'open-forms-sdk.css';
62+
}
63+
return 'static/media/[name].[hash:8].[ext]';
64+
},
65+
inlineDynamicImports: false,
66+
}) satisfies OutputOptions;
6567

6668
/**
6769
* Rollup output options for UMD bundle, included in the NPM package but
6870
* the primary distribution mechanism is in a Docker image.
6971
*
7072
* @deprecated - it's better to use the ESM bundle which has separate chunks.
7173
*/
72-
const umdOutput = {
73-
dir: 'dist',
74-
format: 'umd',
75-
exports: 'named',
76-
name: 'OpenForms',
77-
generatedCode: 'es2015',
78-
entryFileNames: 'open-forms-sdk.js',
79-
assetFileNames: ({name}) => {
80-
if (name === 'style.css') {
81-
return 'open-forms-sdk.css';
82-
}
83-
return 'static/media/[name].[hash:8].[ext]';
84-
},
85-
inlineDynamicImports: true,
86-
} satisfies OutputOptions;
74+
const umdOutput = (buildDist: string) =>
75+
({
76+
dir: buildDist,
77+
format: 'umd',
78+
exports: 'named',
79+
name: 'OpenForms',
80+
generatedCode: 'es2015',
81+
entryFileNames: 'open-forms-sdk.js',
82+
assetFileNames: ({name}) => {
83+
if (name === 'style.css') {
84+
return 'open-forms-sdk.css';
85+
}
86+
return 'static/media/[name].[hash:8].[ext]';
87+
},
88+
inlineDynamicImports: true,
89+
}) satisfies OutputOptions;
8790

88-
const getOutput = (buildTarget: typeof process.env.BUILD_TARGET): OutputOptions => {
91+
const getOutput = (
92+
buildTarget: typeof process.env.BUILD_TARGET,
93+
buildDist: string
94+
): OutputOptions => {
8995
switch (buildTarget) {
9096
case 'esm-bundle': {
91-
return esmBundleOutput;
97+
return esmBundleOutput(buildDist);
9298
}
9399
case 'esm': {
94-
return esmOutput;
100+
return esmOutput(buildDist);
95101
}
96102
case 'umd':
97103
default: {
98-
return umdOutput;
104+
return umdOutput(buildDist);
99105
}
100106
}
101107
};
102108

103-
export default defineConfig(({mode}) => ({
104-
base: './',
105-
publicDir: false,
106-
server: {
107-
port: 3000,
108-
},
109-
plugins: [
110-
// BIG DISCLAIMER - Vite only processes files with the .jsx or .tsx extension with
111-
// babel, and changing this configuration is... cumbersome and comes with a performance
112-
// penalty. This manifests if you're using react-intl in .js/.mjs/.ts files etc., as
113-
// they don't get transformed to inject the message ID. The solution is to rename the
114-
// file extension to .jsx/.tsx
115-
react({babel: {babelrc: true}}),
116-
jsconfigPaths(),
117-
eslint({
118-
build: true,
119-
emitErrorAsWarning: mode === 'development',
120-
}),
121-
cjsTokens(),
122-
ejsPlugin(),
123-
// @formio/protected-eval requires js-interpeter (a forked version), which includes
124-
// this['Interpreter'] = Interpreter. When this is bundled, it becomes a strict module
125-
// and 'this' doesn't point to the window object, but is undefined, and causes the SDK
126-
// to crash.
127-
replace({
128-
preventAssignment: false,
129-
include: ['**/node_modules/js-interpreter/interpreter.js'],
130-
delimiters: ['', ''],
131-
values: {
132-
"this\['Interpreter'\]": "window['Interpreter']",
133-
},
134-
}),
135-
/**
136-
* Plugin to ignore (S)CSS when bundling to UMD bundle target, since we use the ESM
137-
* bundle to generate these.
138-
*
139-
* @todo Remove this when we drop the UMD bundle entirely (in 4.0?)
140-
*/
141-
{
142-
name: 'ignore-styles-esm-bundle',
143-
transform(code, id) {
144-
if (!buildTargetDefined) return;
145-
if (buildTarget === 'umd' && (id.endsWith('.css') || id.endsWith('scss'))) {
146-
// skip processing
147-
return {code: '', map: null};
148-
}
149-
},
109+
export default defineConfig(({mode}) => {
110+
const env = loadEnv(mode, process.cwd(), '');
111+
let buildDist = 'dist';
112+
if (env.SDK_VERSION && env.SDK_VERSION !== 'latest') {
113+
buildDist = `${buildDist}/${env.SDK_VERSION}`;
114+
}
115+
116+
return {
117+
base: './',
118+
publicDir: false,
119+
server: {
120+
port: 3000,
150121
},
151-
sentryVitePlugin({
152-
silent: mode === 'development',
153-
release: {
154-
create: false,
155-
inject: false,
122+
plugins: [
123+
// BIG DISCLAIMER - Vite only processes files with the .jsx or .tsx extension with
124+
// babel, and changing this configuration is... cumbersome and comes with a performance
125+
// penalty. This manifests if you're using react-intl in .js/.mjs/.ts files etc., as
126+
// they don't get transformed to inject the message ID. The solution is to rename the
127+
// file extension to .jsx/.tsx
128+
react({babel: {babelrc: true}}),
129+
jsconfigPaths(),
130+
eslint({
131+
build: true,
132+
emitErrorAsWarning: mode === 'development',
133+
}),
134+
cjsTokens(),
135+
ejsPlugin(),
136+
// @formio/protected-eval requires js-interpeter (a forked version), which includes
137+
// this['Interpreter'] = Interpreter. When this is bundled, it becomes a strict module
138+
// and 'this' doesn't point to the window object, but is undefined, and causes the SDK
139+
// to crash.
140+
replace({
141+
preventAssignment: false,
142+
include: ['**/node_modules/js-interpreter/interpreter.js'],
143+
delimiters: ['', ''],
144+
values: {
145+
"this\['Interpreter'\]": "window['Interpreter']",
146+
},
147+
}),
148+
/**
149+
* Plugin to ignore (S)CSS when bundling to UMD bundle target, since we use the ESM
150+
* bundle to generate these.
151+
*
152+
* @todo Remove this when we drop the UMD bundle entirely (in 4.0?)
153+
*/
154+
{
155+
name: 'ignore-styles-esm-bundle',
156+
transform(code, id) {
157+
if (!buildTargetDefined) return;
158+
if (buildTarget === 'umd' && (id.endsWith('.css') || id.endsWith('scss'))) {
159+
// skip processing
160+
return {code: '', map: null};
161+
}
162+
},
156163
},
157-
sourcemaps: {
158-
disable: true,
164+
sentryVitePlugin({
165+
silent: mode === 'development',
166+
release: {
167+
create: false,
168+
inject: false,
169+
},
170+
sourcemaps: {
171+
disable: true,
172+
},
173+
bundleSizeOptimizations: {
174+
excludeDebugStatements: true,
175+
excludeTracing: true,
176+
excludeReplayCanvas: true,
177+
excludeReplayShadowDom: true,
178+
excludeReplayIframe: true,
179+
excludeReplayWorker: true,
180+
},
181+
telemetry: false,
182+
}),
183+
// must be last!
184+
codecovVitePlugin({
185+
enableBundleAnalysis: buildTarget !== 'esm' && process.env.CODECOV_TOKEN !== undefined,
186+
bundleName: '@open-formulieren/sdk',
187+
uploadToken: process.env.CODECOV_TOKEN,
188+
}),
189+
],
190+
resolve: {
191+
alias: {
192+
// ensure react-router imports don't end up with multiple copies/installations. See
193+
// https://github.com/remix-run/react-router/issues/12785 for more context.
194+
'react-router/dom': path.resolve(
195+
'./node_modules/react-router/dist/development/dom-export.mjs'
196+
),
197+
'react-router': path.resolve('./node_modules/react-router/dist/development/index.mjs'),
159198
},
160-
bundleSizeOptimizations: {
161-
excludeDebugStatements: true,
162-
excludeTracing: true,
163-
excludeReplayCanvas: true,
164-
excludeReplayShadowDom: true,
165-
excludeReplayIframe: true,
166-
excludeReplayWorker: true,
167-
},
168-
telemetry: false,
169-
}),
170-
// must be last!
171-
codecovVitePlugin({
172-
enableBundleAnalysis: buildTarget !== 'esm' && process.env.CODECOV_TOKEN !== undefined,
173-
bundleName: '@open-formulieren/sdk',
174-
uploadToken: process.env.CODECOV_TOKEN,
175-
}),
176-
],
177-
resolve: {
178-
alias: {
179-
// ensure react-router imports don't end up with multiple copies/installations. See
180-
// https://github.com/remix-run/react-router/issues/12785 for more context.
181-
'react-router/dom': path.resolve(
182-
'./node_modules/react-router/dist/development/dom-export.mjs'
183-
),
184-
'react-router': path.resolve('./node_modules/react-router/dist/development/index.mjs'),
185-
},
186-
},
187-
build: {
188-
target: 'modules', // the default
189-
assetsInlineLimit: 8 * 1024, // 8 KiB
190-
cssCodeSplit: false,
191-
sourcemap: buildTarget !== 'esm',
192-
outDir: 'dist',
193-
// we write the .mjs file to the same directory
194-
emptyOutDir: buildTarget !== 'esm-bundle',
195-
rollupOptions: {
196-
input: 'src/sdk.jsx',
197-
// do not externalize anything in UMD build - bundle everything
198-
external: buildTarget === 'esm' ? packageRegexes : undefined,
199-
output: getOutput(buildTarget),
200-
preserveEntrySignatures: 'strict',
201199
},
202-
},
203-
css: {
204-
preprocessorOptions: {
205-
scss: {
206-
additionalData: `$fa-font-path: '@fortawesome/fontawesome-free/webfonts/';`,
207-
charset: false,
200+
build: {
201+
target: 'modules', // the default
202+
assetsInlineLimit: 8 * 1024, // 8 KiB
203+
cssCodeSplit: false,
204+
sourcemap: buildTarget !== 'esm',
205+
outDir: buildDist,
206+
// we write the .mjs file to the same directory
207+
emptyOutDir: buildTarget !== 'esm-bundle',
208+
rollupOptions: {
209+
input: 'src/sdk.jsx',
210+
// do not externalize anything in UMD build - bundle everything
211+
external: buildTarget === 'esm' ? packageRegexes : undefined,
212+
output: getOutput(buildTarget, buildDist),
213+
preserveEntrySignatures: 'strict',
208214
},
209215
},
210-
},
211-
test: {
212-
environment: 'jsdom',
213-
environmentOptions: {
214-
jsdom: {
215-
url: 'http://localhost',
216+
css: {
217+
preprocessorOptions: {
218+
scss: {
219+
additionalData: `$fa-font-path: '@fortawesome/fontawesome-free/webfonts/';`,
220+
charset: false,
221+
},
216222
},
217223
},
218-
globals: true, // for compatibility with jest
219-
// See https://vitest.dev/guide/migration.html#fake-timers-defaults
220-
fakeTimers: {
221-
toFake: ['setTimeout', 'clearTimeout', 'Date'],
222-
},
223-
setupFiles: ['./src/vitest.setup.mjs'],
224-
coverage: {
225-
provider: 'istanbul',
226-
include: ['src/**/*.{js,jsx,ts,tsx}'],
227-
exclude: [
228-
'src/**/*.d.ts',
229-
'src/**/*.stories.{js,jsx,ts,tsx}',
230-
'src/api-mocks/*',
231-
'src/**/mocks.{js,jsx}',
232-
'src/story-utils/*',
233-
...coverageConfigDefaults.exclude,
234-
],
235-
reporter: ['text', 'cobertura', 'html'],
224+
test: {
225+
environment: 'jsdom',
226+
environmentOptions: {
227+
jsdom: {
228+
url: 'http://localhost',
229+
},
230+
},
231+
globals: true, // for compatibility with jest
232+
// See https://vitest.dev/guide/migration.html#fake-timers-defaults
233+
fakeTimers: {
234+
toFake: ['setTimeout', 'clearTimeout', 'Date'],
235+
},
236+
setupFiles: ['./src/vitest.setup.mjs'],
237+
coverage: {
238+
provider: 'istanbul',
239+
include: ['src/**/*.{js,jsx,ts,tsx}'],
240+
exclude: [
241+
'src/**/*.d.ts',
242+
'src/**/*.stories.{js,jsx,ts,tsx}',
243+
'src/api-mocks/*',
244+
'src/**/mocks.{js,jsx}',
245+
'src/story-utils/*',
246+
...coverageConfigDefaults.exclude,
247+
],
248+
reporter: ['text', 'cobertura', 'html'],
249+
},
236250
},
237-
},
238-
}));
251+
};
252+
});

0 commit comments

Comments
 (0)