Skip to content

Commit

Permalink
👽 [#724] Ensure that a _ global exists
Browse files Browse the repository at this point in the history
lodash/template requires the window._ global to be defined, otherwise
the templates don't render properly.

Failing to provide this breaks the formio file upload stories.
  • Loading branch information
sergei-maertens committed Dec 3, 2024
1 parent 66b2d8b commit e97bcb0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'design-token-editor/lib/css/dte.css';
import 'design-token-editor/lib/css/root.css';
import 'flatpickr';
import 'flatpickr/dist/l10n/nl.js';
import lodash from 'lodash';
import {fixIconUrls as fixLeafletIconUrls} from 'map';
import {initialize, mswLoader} from 'msw-storybook-addon';
import {Formio, Templates} from 'react-formio';
Expand All @@ -28,6 +29,8 @@ import {
import {reactIntl} from './reactIntl.mjs';
import ThemeProvider from './theme';

window._ = lodash;

initialize({
onUnhandledRequest: 'bypass',
serviceWorker: {
Expand Down
4 changes: 1 addition & 3 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// https://vitejs.dev/config/
import react from '@vitejs/plugin-react';
import lodashTemplate from 'lodash/template';
import {readFile} from 'node:fs/promises';
import {defineConfig} from 'vite';
import jsconfigPaths from 'vite-jsconfig-paths';
import {coverageConfigDefaults} from 'vitest/config';
Expand All @@ -11,15 +10,14 @@ import {coverageConfigDefaults} from 'vitest/config';
// https://github.com/difelice/ejs-loader/blob/master/index.js
const ejsPlugin = () => ({
name: 'compile-ejs',
async transform(_, id) {
async transform(src: string, id: string) {
const options = {
variable: 'ctx',
evaluate: /\{%([\s\S]+?)%\}/g,
interpolate: /\{\{([\s\S]+?)\}\}/g,
escape: /\{\{\{([\s\S]+?)\}\}\}/g,
};
if (id.endsWith('.ejs')) {
const src = await readFile(id, 'utf-8');
// @ts-ignore
const code = lodashTemplate(src, options);
return {code: `export default ${code}`, map: null};
Expand Down

0 comments on commit e97bcb0

Please sign in to comment.