Inconsistent Module Resolution (context require vs. ES6 import) #19215
-
Bug reportWhat is the current behavior? // file structure
// ./src/mods
// ./1.js
// ./2.js
// index.js
// configuration
resolve.modules: ['node_modules', path.resolve(__dirname, 'src')],
// index.js
const req = require.context('./mods', true, /\.js$/); // both 1.js and 2.js are modules
import mod1Relative from './mods/1.js'; // not a module
import mod1Alias from 'mods/2.js'; // module how I declar the 'module': resolver.getHook('before-rawModule').tapAsync('runtime-external-resolve', (req, ctx, cb) => {
console.log('----isModule: ', req.request);
return cb();
}); If the current behavior is a bug, please provide the steps to reproduce. What is the expected behavior? Other relevant information: |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
@harshdev2909 Thank you for raising this! After reviewing Webpack's source code, I noticed this behavior appears to have been intentionally introduced in here, which was added 5 years ago. To help clarify, could you elaborate on the "modifying the configuration" you're referring to? This would help narrow down the context for further discussion. |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
That is expected, it is limitation of const req = import.meta.webpackContext('./mods', {
recursive: true,
regExp: /\.js$/
}); Unfortunately when using |
Beta Was this translation helpful? Give feedback.
-
What you try to resolve? With more context I think I can help you better. |
Beta Was this translation helpful? Give feedback.
That is expected, it is limitation of
require.context
, note - in ECMA modules I recommend to useUnfortunately when using
require.context
andimport.meta.webpackContext
are always trying to resolve as a module, the PR was merged to supportContextReplacementPlugin
,