You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
used with (e.g. hapi application, another framework, standalone, ...): Angular
any other relevant information:
What are you trying to achieve or the steps to reproduce?
When using joi in a web application, e.g. with Angular, dist/joi-browser.min.js is spliced in the web application.
dist/joi-browser.min.js uses UMD. In the UMD preamble, there is a reference to window (used as fallback when no module loader is recognized). This fails when joi is used in a web worker, because in that context there is no global window.
What was the result you got?
Error: window is undefined
What result did you expect?
No error
Request
Is there a way to build dist/joi-browser.min.js without the reference to window?
Workaround
In a modern web application this path of the UMD preamble isn't used.
We created a quick fix, that replaces the reference to window with undefined in node_modules/@hapi/joi/dist/joi-browser.min.js, and use it in postinstall.
File ./scripts/quickfix-joi.js:
#!/usr/bin/env node
/**
* `joi-browser.min.js` is build with UMD, which in this case refers to `window`.
* That doesn't work in web workers (because there is no `window` global).
*
* However, the UMD path where the `window` reference is not used in our project.
*
* This quick fix replaces the reference to `window` in `joi-browser.min.js` with `undefined`.
* It is to be executed in `postinstall`.
*/
const fs = require('fs').promises;
const joiBrowserPath = './node_modules/@hapi/joi/dist/joi-browser.min.js';
const options = { encoding: 'utf8' };
async function doIt() {
const joiSrc = await fs.readFile(joiBrowserPath, options);
// Replace the window object, which doesn't exist in a worker, with undefined
// This parameter isn't used in our project anyway.
const result = joiSrc.replace('window', 'undefined');
await fs.writeFile(joiBrowserPath, result, options);
console.log(`Doctored \`${joiBrowserPath}\` successfully.`);
}
console.log(
`Doctoring \`${joiBrowserPath}\`.
It uses \`window\` in the UMD preliminary, but we don't use that. We replace it with \`undefined\`
…`
);
doIt();
Unfortunately, no community resources were available to help resolve this issue after two weeks, and it is being closed. We close unresolved community issues to keep the issue tracker organized and effective.
Support plan
Context
What are you trying to achieve or the steps to reproduce?
When using
joi
in a web application, e.g. with Angular,dist/joi-browser.min.js
is spliced in the web application.dist/joi-browser.min.js
uses UMD. In the UMD preamble, there is a reference towindow
(used as fallback when no module loader is recognized). This fails whenjoi
is used in a web worker, because in that context there is no globalwindow
.What was the result you got?
Error:
window is undefined
What result did you expect?
No error
Request
Is there a way to build
dist/joi-browser.min.js
without the reference towindow
?Workaround
In a modern web application this path of the UMD preamble isn't used.
We created a quick fix, that replaces the reference to
window
withundefined
innode_modules/@hapi/joi/dist/joi-browser.min.js
, and use it inpostinstall
.File
./scripts/quickfix-joi.js
:In
package.json
:The text was updated successfully, but these errors were encountered: