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
[RFC DRAFT WIP ...] refactor eval("require")
workaround hack
#7515
Conversation
This is not how it works, that babel plugin only look for
|
If you want change that, you need rewrite |
src/common/require-module.js
Outdated
function requireModule(path) { | ||
return requireModuleAtPath(path); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe try this
function requireModule(path) {
return eval("require")(path);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For require.resolve
we can do
requireModule.resolve = eval("require").resolve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it, did not seem to get yarn prepare-release
to pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't
eval("require")
workaround into separate functioneval("require")
workaround into separate function
src/cli/util.js
Outdated
@@ -24,6 +24,8 @@ const thirdParty = require("../common/third-party"); | |||
const arrayify = require("../utils/arrayify"); | |||
const isTTY = require("../utils/is-tty"); | |||
|
|||
const requireModule = require("../common/require-module"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const requireModule = require;
does this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to work, yes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spoke too soon, prod build did not work.
If we do fix this on the prod build, we could remove our custom Babel transform-custom-require.js
plugin and remove the useless require-module.js
module that I added.
I think the main problem is this error:
[error] Dynamic requires are not currently supported by @rollup/plugin-commonjs
From some quick research I found an idea in winstonjs/logform#5 (comment) to use what is now @rollup/plugin-node-resolve
(here on GitHub). Some very interesting reading in these sections:
- https://github.com/rollup/plugins/tree/master/packages/node-resolve#browser
- https://github.com/rollup/plugins/tree/master/packages/node-resolve#using-with-rollupplugin-commonjs
- https://github.com/rollup/plugins/tree/master/packages/node-resolve#resolving-built-ins-like-fs
I am now starting to wonder if we could do the whole prod build with an updated set of Rollup packages, with minimal or even zero Babel transforms, and minimal custom scripting.
Unfortunately I cannot promise when I will have extra time to look into this further. I would be happy if someone else wants to take this over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, forgot that. rollup
replace require
to this error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said before. eval('require')
actually a good one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eval('require')
actually a good one.
Done but not working. I get this kind of an error on prod test:
Cannot find module '../language-html/parser-html' from 'index.js'
I tried hacking scripts/build/babel-plugins/transform-custom-require.js
but with no luck.
The custom Babel plugin assumes that we are using eval('require')
in every case where we need the transform, which is violated by this proposal.
I will now try another idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean eval('require')
in the codebase is actually a good solution.
Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: Christopher J. Brody <chris.brody@gmail.com>
eval("require")
workaround into separate functioneval("require")
workaround hack
This comment has been minimized.
This comment has been minimized.
my code in fisker#302 is the way how to replace |
I think we shouldn't try anything more... All solutions I can think are less good than current solution . |
OK I am putting this on hold for now. |
t.isIdentifier(node.callee, { name: "eval" }) && | ||
node.arguments.length === 1 && | ||
t.isLiteral(node.arguments[0], { value: "require" }) | ||
t.isIdentifier(node.callee, { name: "requireModule" }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't try this way, even it works, we need hard code this function name.
I unintentionally closed this and several other PRs because I deleted the |
I noticed that the workaround solution of using eval("require")(...) is repeated throughout
src/common/internal-plugins.js
. I would really favor keeping this kind of a workaround hack in one place for DRY and to decrease the chance of missing places where we may want to improve this solution in the future.This proposal is triggered by PR #7508, and I hope we can find a way to refactor instances of the workaround hack out of that PR as well.
I am raising this proposal as a DRAFT PR for now, and would be equally happy if this PR is closed in favor of a different solution. I really hope we can find a way to refactor this workaround solution somehow.
/cc @fisker
I’ve added tests to confirm my change works.(If changing the API or CLI) I’ve documented the changes I’ve made (in thedocs/
directory)(If the change is user-facing) I’ve added my changes tochangelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.Testing:
npx eslint
does not show any errors on the updated modules.npm test
passes in my work area.✨Try the playground for this PR✨