Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

brodybits
Copy link
Contributor

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 the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Testing:

  • I verified that npx eslint does not show any errors on the updated modules.
  • I verified that npm test passes in my work area.

Try the playground for this PR

@fisker
Copy link
Sponsor Member

fisker commented Feb 2, 2020

This is not how it works, that babel plugin only look for

eval('require')(module)
//              ^^^^^^ this is required

// and
eval('require').propOrMethod
//              ^^^^^^^^^^^^ this is required

@fisker
Copy link
Sponsor Member

fisker commented Feb 2, 2020

If you want change that, you need rewrite scripts/build/babel-plugins/transform-custom-require.js, or maybe @rollup/plugin-replace can replace, not sure.

Comment on lines 17 to 19
function requireModule(path) {
return requireModuleAtPath(path);
}
Copy link
Sponsor Member

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);
}

Copy link
Sponsor Member

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 

Copy link
Contributor Author

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.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't

@brodybits brodybits changed the title [RFC ...] refactor eval("require") workaround into separate function [RFC DRAFT WIP ...] refactor eval("require") workaround into separate function Feb 2, 2020
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");
Copy link
Sponsor Member

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?

Copy link
Contributor Author

@brodybits brodybits Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work, yes!

Copy link
Contributor Author

@brodybits brodybits Feb 4, 2020

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:

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.

Copy link
Sponsor Member

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.

Copy link
Sponsor Member

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.

Copy link
Contributor Author

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.

Copy link
Sponsor Member

@fisker fisker Feb 4, 2020

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.

Christopher J. Brody and others added 3 commits February 4, 2020 08:12
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>
@brodybits brodybits changed the title [RFC DRAFT WIP ...] refactor eval("require") workaround into separate function [RFC DRAFT WIP ...] refactor eval("require") workaround hack Feb 4, 2020
@fisker

This comment has been minimized.

@fisker
Copy link
Sponsor Member

fisker commented Feb 4, 2020

my code in fisker#302 is the way how to replace eval("require") to require, but if we do requireModule('./foo'), we can't locate the argument string now, so no way to replace that string

@fisker
Copy link
Sponsor Member

fisker commented Feb 4, 2020

I think we shouldn't try anything more... All solutions I can think are less good than current solution .

@brodybits
Copy link
Contributor Author

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" })
Copy link
Sponsor Member

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.

@thorn0
Copy link
Member

thorn0 commented Mar 22, 2020

I unintentionally closed this and several other PRs because I deleted the next branch, but looks like a good solution wasn't found here and it's not really clear whether there is a problem to solve in the first place, so let's keep this PR closed.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants