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

import.meta.webpackContext doesn't work if requiring inside npm packages #15580

Closed
Tofandel opened this issue Mar 24, 2022 · 4 comments · Fixed by #15585
Closed

import.meta.webpackContext doesn't work if requiring inside npm packages #15580

Tofandel opened this issue Mar 24, 2022 · 4 comments · Fixed by #15585

Comments

@Tofandel
Copy link

Tofandel commented Mar 24, 2022

Bug report

What is the current behavior?
When requiring files using import.meta.webpackContext (added in #15446) that are located within a node_modules folder, eg:

const vuetify = import.meta.webpackContext('vuetify/lib/locale', {
  recursive: false,
  regExp: /(en|hu)\.js$/i,
});

It doesn't work properly and throws a warning during compilation

 WARNING  Compiled with 2 warnings                                                                                                                                                                                             6:40:36 PM

 warning  in ./node_modules/vuetify/lib/locale/ ./node_modules/vuetify/lib/locale/ sync nonrecursive (en%7Chu)\.js$/
 
Module not found: Error: Can't resolve 'webpack-meta-bug/node_modules/vuetify/lib/locale/vuetify/lib/locale/en.js' in 'webpack-meta-bug'

It seems to be adding the context path twice to the request

If the current behavior is a bug, please provide the steps to reproduce.
https://github.com/Tofandel/webpack-meta-bug

Use the following webpack config

const path = require('path')
module.exports = {
  resolve: {
    modules: [
      'node_modules',
      path.resolve(__dirname, './node_modules'),
    ]
  },
}

And use import.meta.webpackContext('some_package_files')

What is the expected behavior?
It should require the files correctly

Other relevant information:
webpack version: 5.70.0
Node.js version: 16.13.1
Operating System: WSL2 - Ubuntu 20.04
Additional tools:

@Tofandel
Copy link
Author

@vankop
Copy link
Member

vankop commented Mar 25, 2022

you should remove several identical resolve.modules setting ( or in case of node_modules just remove this settings )
Screenshot 2022-03-25 at 12 44 27

@vankop vankop closed this as completed Mar 25, 2022
@vankop
Copy link
Member

vankop commented Mar 25, 2022

lets keep issue for now.. need to fix this anyway..

@vankop vankop reopened this Mar 25, 2022
@Tofandel
Copy link
Author

Tofandel commented Mar 25, 2022

There is a fix #15581 ... It passes tests but the pipeline doesn't want to run on the PR

As for removing duplicate resolve.modules, that's the default of vue-cli, so not really an option given that the bug is indeed here, that was the whole point of this issue and fix in the first place, which took me hours to track down so I can imagine future users trying to use this in a vue project only to find the same bug, only to find after 2hours of debug and searching issues that the only workaround is to do

  chainWebpack: config => {
    config.resolve.modules.delete('node_modules');
  }

The problem is the resolution adds an item like
{context: 'node_modules/vuetify/lib/locales', request: 'vuetify/lib/locales/en.js' }

When it should be
{context: 'node_modules', request: 'vuetify/lib/locales/en.js' }

Or

{context: 'node_modules/vuetify/lib/locales', request: 'en.js' } (but this one breaks tests)

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

Successfully merging a pull request may close this issue.

2 participants