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

__webpack_require__(...).context is not a function #8

Open
thatsmydoing opened this issue Oct 27, 2023 · 1 comment
Open

__webpack_require__(...).context is not a function #8

thatsmydoing opened this issue Oct 27, 2023 · 1 comment

Comments

@thatsmydoing
Copy link

From swc-project/swc-loader#64 (comment) by @callmeteus

I'm trying to port from babel-loader and ts-loader to swc-loader, but everytime require.context is called, it returns undefined.

When printing the require variable, it returns the following module:

function webpackEmptyContext(req) {
	var e = new Error("Cannot find module '" + req + "'");
	e.code = 'MODULE_NOT_FOUND';
	throw e;
}
webpackEmptyContext.keys = () => ([]);
webpackEmptyContext.resolve = webpackEmptyContext;
webpackEmptyContext.id = 5102;
module.exports = webpackEmptyContext;
@thatsmydoing
Copy link
Author

The issue we've found here is when the regexp passed to require.context uses a lookbehind expression like /(?<!.spec).(js|tsx)$/, swc transforms it to RegExp("(?<!.spec).(js|tsx)$"). Playground

webpack only processes require.context if the provided regex is a literal and otherwise skips it. So in our case, removing the lookbehind made swc keep it as a literal and it worked for us. Another option is to set the target to ES2022 (if the project allows) where swc will keep it as a literal too.

Though it would be nice if swc-loader could be smart about this and not transform require.context arguments but I'm not sure how feasible that is.

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

No branches or pull requests

1 participant