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

eslint-import-resolver-webpack is not compatible with webpack 5 - TypeError: callback is not a function #1966

Open
lavor opened this issue Dec 21, 2020 · 3 comments

Comments

@lavor
Copy link

lavor commented Dec 21, 2020

eslint-import-resolver-webpack is not compatible with webpack 5 externals API of webpack config.

When I use function for resolving externals, I am getting an error:

ESLint: 
Resolve error: TypeError: callback is not a function
    at D:\++projects\++datlowe\ui\datlowe-core\src\js\/externals.js:102:13
    at findExternal (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-import-resolver-webpack\index.js:330:15)
    at D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-import-resolver-webpack\index.js:321:49
    at Array.some (<anonymous>)
    at findExternal (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-import-resolver-webpack\index.js:321:22)
    at Object.exports.resolve (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-import-resolver-webpack\index.js:135:7)
    at v2 (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-module-utils\resolve.js:117:23)
    at withResolver (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-module-utils\resolve.js:122:16)
    at fullResolve (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-module-utils\resolve.js:139:22)
    at relative (D:\++projects\++datlowe\ui\datlowe-core\node_modules\eslint-module-utils\resolve.js:84:10)
(import/namespace)

Issue happens because there was a change to that funtion API:

  • Webpack 4: function(context, request, callback) {}
  • Webpack 5: function({ context, request }, callback) {}
    My function for resolving external is now looking like this:
// there was change of API in webpack 5
// webpack 4 version looks line this
// function(context, request, callback) {
function({context, request}, callback) {
    const modulePath = path.join(context || '', request || '');
    if (/node_modules[/\\]/.test(modulePath)) {
        const splittedPath = modulePath.split(/node_modules[/\\]/);
        if (splittedPath && splittedPath.length > 0) {
            // if request is not a relative path starting with dot (.), use request instead of the whole module path
            const externalModulePath = /^\./.test(request) ? splittedPath[splittedPath.length - 1] : request;
            // the externalized module is referenced as a commonjs module
            return callback(null, `commonjs ${externalModulePath}`);
        }
    }

    // Continue without externalizing the import
    callback();
}

The issue is in findExternal function here. In weback 5 the first argument is an object consisting of context and request and the callback is the second not the third argument now.

@ljharb
Copy link
Member

ljharb commented Dec 22, 2020

A PR that handles both cases would be appreciated.

@jet2jet
Copy link
Contributor

jet2jet commented Apr 3, 2021

I also encountered this problem.
Since it seems not so difficult to fix, I created the PR #2023.

@lenichols
Copy link

Still waiting on this fix.

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

No branches or pull requests

4 participants