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

add createRequire support #15579

Merged
merged 5 commits into from May 11, 2022
Merged

add createRequire support #15579

merged 5 commits into from May 11, 2022

Conversation

vankop
Copy link
Member

@vankop vankop commented Mar 24, 2022

What kind of change does this PR introduce?
closes #14228

Did you add tests for your changes?
yes

Does this PR introduce a breaking change?
no

What needs to be documented once your changes are merged?
nothing

@webpack-bot
Copy link
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

lib/javascript/JavascriptParser.js Outdated Show resolved Hide resolved
it("should create require", () => {
const require = _createRequire(import.meta.url);
expect(require("./a")).toBe(1);
expect(_createRequire(import.meta.url)("./c")).toBe(3);
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO
add support of context. maybe contextify request?

}
};

parser.hooks.import.tap(
Copy link
Member Author

Choose a reason for hiding this comment

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

should we add parser option to enable such behaviour? (and enable it by default in target=node)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we don't want to enable that by default in web target since it's a "node.js polyfill" which we have chosen to not provide by default.

@vankop vankop force-pushed the support-create-require branch 4 times, most recently from 93f44bf to 16f53cb Compare March 25, 2022 06:50
Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

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

Looks good, but I can't see where context is used when passing a string to createRequire.

Also it might make sense to rename context to issuer since context usually points to a directory in webpack context, while issuer would be the requesting module.

Comment on lines 486 to 499
if (
arg.type === "MemberExpression" &&
arg.object.type === "MetaProperty"
) {
if (
arg.object.meta.type === "Identifier" &&
arg.object.meta.name === "import" &&
arg.object.property.type === "Identifier" &&
arg.object.property.name === "meta" &&
arg.property.type === "Identifier" &&
arg.property.name === "url"
) {
// same module context
return false;
Copy link
Member

Choose a reason for hiding this comment

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

I think we evaluate import.meta to an identifier in evaluateExpression, so this special case isn't needed in my opinion.

You can use evaluated.isIdentifier() && evaluated.identifier === "import.meta.url" for that case in the code below

}
};

parser.hooks.import.tap(
Copy link
Member

Choose a reason for hiding this comment

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

Yes, we don't want to enable that by default in web target since it's a "node.js polyfill" which we have chosen to not provide by default.

expect(require.cache).toBe(_require.cache);
expect(require.cache).toBe(_createRequire(import.meta.url).cache);
});

Copy link
Member

Choose a reason for hiding this comment

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

Needs a test case when a string is passed to createRequire. e. g. createRequire(import.meta.url + "/../somewhere-else/file.js")

Copy link
Member Author

Choose a reason for hiding this comment

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

import.meta.url + "/../somewhere-else/file.js" will not produce valid context. I have added support to new URL(...)

@webpack-bot
Copy link
Contributor

@vankop Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@sokra Please review the new changes.

@vankop vankop force-pushed the support-create-require branch 2 times, most recently from 5d967db to 64778f0 Compare March 31, 2022 12:02
# Conflicts:
#	declarations/WebpackOptions.d.ts
#	lib/config/defaults.js
#	schemas/WebpackOptions.check.js
#	schemas/WebpackOptions.json
#	test/Defaults.unittest.js
#	test/__snapshots__/Cli.basictest.js.snap
#	types.d.ts
@sokra sokra merged commit 2738eeb into main May 11, 2022
@sokra sokra deleted the support-create-require branch May 11, 2022 18:16
@sokra
Copy link
Member

sokra commented May 11, 2022

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: createRequire detect
3 participants