Skip to content

Commit acc9500

Browse files
workaround with require shim
1 parent c1424d1 commit acc9500

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

packages/cpt-ui/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"pino": "^10.1.0",
2525
"react": "18.3.1",
2626
"react-dom": "18.3.1",
27+
"react-input-mask": "^2.0.4",
2728
"react-router-dom": "6.30.1",
2829
"sass": "^1.96.0"
2930
},
@@ -36,6 +37,7 @@
3637
"@types/govuk-frontend": "^5.11.0",
3738
"@types/react": "18.3.1",
3839
"@types/react-dom": "18.3.1",
40+
"@types/react-input-mask": "^3.0.6",
3941
"@vitejs/plugin-react-swc": "^4.2.2",
4042
"axios-mock-adapter": "^2.1.0",
4143
"sass": "^1.96.0",

packages/cpt-ui/src/main.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "./shims/require-shim"
12
import React from "react"
23
import ReactDOM from "react-dom/client"
34
import {BrowserRouter} from "react-router-dom"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Shim for packages that incorrectly use CommonJS require() in ES modules
2+
// This is specifically for nhsuk-react-components-extensions which incorrectly
3+
// uses require('react-input-mask') in its ES module build
4+
5+
import ReactInputMask from "react-input-mask"
6+
7+
// Add require to the global scope for the browser environment only
8+
if (typeof window !== "undefined") {
9+
// Create a require function specifically for react-input-mask
10+
function browserRequire(id: string): unknown {
11+
if (id === "react-input-mask") {
12+
return ReactInputMask
13+
}
14+
throw new Error(`require() is not supported in browser ES modules. Module requested: ${id}`)
15+
}
16+
17+
// Assign to globalThis if require doesn't already exist
18+
if (!(globalThis as Record<string, unknown>).require) {
19+
;(globalThis as Record<string, unknown>).require = browserRequire
20+
}
21+
}

packages/cpt-ui/vite.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export default defineConfig(({mode}) => {
4040
}
4141
}
4242
},
43-
define: envWithProcessPrefix
43+
define: {
44+
...envWithProcessPrefix,
45+
"global": "globalThis"
46+
},
47+
optimizeDeps: {
48+
include: ["react-input-mask"],
49+
exclude: []
50+
}
4451
}
4552
})

0 commit comments

Comments
 (0)