-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
refactor(common): Prevent JavaScript being wrapped in eval
#14974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(common): Prevent JavaScript being wrapped in eval
#14974
Conversation
Replace wrapping dynamic import of ESM module in `eval` with load-esm.
Pull Request Test Coverage Report for Build c1fde52d-d1f3-4297-b188-e9e2580fab05Details
💛 - Coveralls |
do we need |
In JavaScript, yes, no problem. But in TypeScript that does not work, as the TypeScript compiler converts the dynamic import to The exception that this does work if you use a recent Node.js engine 22, as in that engine the require is able to load the ESM module, which compensates for the incorrect conversion op the TypeScript compiler. |
eval
eval
load-esm dependency seems break nextjs monorepo project with nx, used typescript alias path |
I need to adjust my reply provider here: #14974 (comment) I just found an issue regarding this arbitrary dynamic import to So it seems that this is bound to the Compiler options One way of preserving the dynamic import is: {
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
}
} So to use
Created a PR, for this alternative solution: #14976 |
You could try creating an extra - load-esm
- package.json
- index.js
- file.ts // load-esm/pacakge.json
{
"type": "commonjs"
} // file.ts
import { loadEsm } from './load-esm'
// use `loadEsm` or use a precompiled solution like Next.js with ncc to compile this might help reduce dependencies and lower the risk of supply chain attacks ( in the long term, we could precompile all dependencies ). |
We also use nextjs in a monorepo, though with turborepo. We have a separate project to share our DTOs between the frontend and backend using nestjs-zod. That unfortunately means some of the nestjs dependencies get brought into the UI which has thrown up this warning after this change:
EDIT: I swapped our import of nestjs-zod to |
Replace wrapping dynamic import of ESM module in
eval
with load-esm.PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Wraps the dynamic import in an
eval
function, which is not the best workaround to avoid the TypeScript compiler breaks the dynamic import.More information: https://stackoverflow.com/a/79265806/28701779
What is the new behavior?
Is uses load-esm instead, which is, just like
eval
, using a simple trick to keep the dynamic import outside the scope of the TypeScript compiler, yet not by wrapping the entire ESM module in aneval
method.Not black and white science, wrapping code in
eval
this case the import and the entire ESM module, is not such a great idea:Does this PR introduce a breaking change?
Other information
Related to discussion #14970 (comment)