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

Babel Loader ignores webpack resolve config for its own plugins #895

Open
alexander-schranz opened this issue Mar 8, 2021 · 4 comments
Open

Comments

@alexander-schranz
Copy link
Contributor

alexander-schranz commented Mar 8, 2021

Webpack Version:

4.46.0

Babel Core Version:

"@babel/core": "^7.5.5", -> 7.13.8

Babel Loader Version:

"babel-loader": "^8.0.6", -> 8.2.2

Please tell us about your environment:

OSX 10.14.6

Current behavior:

Error: Cannot find module '@babel/plugin-proposal-decorators'

Expected/desired behavior:

Find Babel plugin.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with a gist/jsbin of your webpack configuration.

We have an uncommen directory structure which looks like the following:

  • admin/assets
    • webpack.config.js
    • babel.config.json
  • vendor/...
    • moduleA
      • package.json
      • .babelrc

The vendors are added as babelRoots:

{
    "babelrcRoots": [
        ".",
        "../../vendor/*"
    ]
}

because how npm works we need to override webpack module resolver to something like this:

        resolve: {
            modules: ['node_modules', assetAdminNodeModulesPath],
        },
        resolveLoader: {
            modules: ['node_modules', assetAdminNodeModulesPath],
        },

but it looks like babel-loader itself does not use this webpack config to load the plugin and so it ends with:

Error: Cannot find module '@babel/plugin-proposal-decorators'

  • What is the expected behavior?

I would expect that the resolve config of weback is used to find the @babel/plugin-proposal-decorators module.

  • What is the motivation / use case for changing the behavior?

Fixing problem with a custom dependency resolve configuration.

@JLHwung
Copy link
Contributor

JLHwung commented Mar 8, 2021

This is expected, the babel plugin is not loaded by webpack, instead it is resolved using node require.resolve so it is not and should not be aware of how Webpack resolve modules for your app.

Can you share a reproduction repo?

@nicolo-ribaudo
Copy link
Member

Babel allows you to directly use require in your Babel config to load plugins however you want, bypassing Babel's resolution algorithm.

module.exports = {
  plugins: [
    require("../../custom-path/@babel/plugin-proposal-decorators")
  ]
}

@alexander-schranz
Copy link
Contributor Author

alexander-schranz commented Mar 8, 2021

Thx for the fast responses!

@JLHwung I did fast create a reproducer based on our setup we use in sulu/skeleton here: https://github.com/alexander-schranz/build-error-reproducer.

If you go there into the assets/admin and run npm install && npm run build you should get:

Error: Cannot find module '@babel/plugin-proposal-decorators'

@nicolo-ribaudo directly require the plugins is sadly not an option as we are not in control of all the .babelrc files. We would then need manually search for them and parse them.

@alexander-schranz
Copy link
Contributor Author

@JLHwung a fallback to process.cwd to load modules would fix this issue. Not sure if this would be correct way to handle this. https://github.com/babel/babel/pull/12986/files

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

No branches or pull requests

3 participants