Skip to content

Commit

Permalink
🔨 [#724] Support outputting UMD build of Open Forms SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Dec 20, 2024
1 parent 6ac84e6 commit 643aa30
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"start": "npm run build:design-tokens && node scripts/start.js",
"build": "node scripts/build.js && npm run build:esm",
"build:esm": "rimraf dist/esm && NODE_ENV=production babel --no-babelrc --config-file ./.babelrc.esm-build --ignore 'src/**/*.spec.js','src/**/fixtures/**','src/setupTests.js','src/reportWebVitals.js' src --out-dir dist/esm",
"build:vite": "BUILD_TARGET=umd vite build && BUILD_TARGET=esm vite build",
"test": "TZ=Europe/Amsterdam vitest",
"test:storybook": "test-storybook --coverage",
"clean": "rimraf dist/*",
Expand Down
79 changes: 63 additions & 16 deletions vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,70 @@
/// <reference types="vitest/config" />
// https://vitejs.dev/config/
import react from '@vitejs/plugin-react';
import type {OutputOptions} from 'rollup';
import {defineConfig} from 'vite';
import jsconfigPaths from 'vite-jsconfig-paths';
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';
}
}
}

const buildTarget = process.env.BUILD_TARGET || 'umd';

/**
* 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-vite/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;

/**
* Rollup output options for UMD bundle, included in the NPM package but
* the primary distribution mechanism is in a Docker image.
*
* @todo - optimize with bundle splitting/chunk management.
*/
const umdOutput = {
dir: 'dist-vite',
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;

export default defineConfig({
base: './',
publicDir: false,
Expand All @@ -23,27 +80,17 @@ export default defineConfig({
ejsPlugin(),
],
build: {
minify: false,
target: 'modules', // the default
outDir: 'dist-vite',
assetsInlineLimit: 8 * 1024, // 8 KiB
cssCodeSplit: false,
sourcemap: buildTarget !== 'esm',
outDir: 'dist-vite/umd',
rollupOptions: {
input: 'src/sdk.jsx',
external: packageRegexes,
output: {
dir: 'dist-vite/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]';
},
sourcemap: false,
},
// do not externalize anything in UMD build - bundle everything
external: buildTarget === 'esm' ? packageRegexes : undefined,
output: buildTarget === 'esm' ? esmOutput : umdOutput,
preserveEntrySignatures: 'strict',
},
},
Expand Down

0 comments on commit 643aa30

Please sign in to comment.