Skip to content

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

Open

Description

From swc-project/swc-loader#64 (comment) by Gio (@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;

Activity

  1. thatsmydoing commented on Oct 27, 2023

    @thatsmydoing
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions