Skip to content

add createRequire support#15579

Merged
sokra merged 5 commits intomainfrom
support-create-require
May 11, 2022
Merged

add createRequire support#15579
sokra merged 5 commits intomainfrom
support-create-require

Conversation

@vankop
Copy link
Copy Markdown
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
Copy Markdown
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)

@vankop vankop force-pushed the support-create-require branch from da341a2 to 552f53a Compare March 24, 2022 15:23
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@vankop vankop force-pushed the support-create-require branch from 16f53cb to 34c4edc Compare March 25, 2022 07:50
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@vankop vankop force-pushed the support-create-require branch from 64778f0 to 8df5134 Compare March 31, 2022 12:10
# 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
Copy Markdown
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