You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I understand, but the fact that it's called require.context is anecdotical, isn't it? I mean Webpack doesn't use the require function under the hood, it just parses the expression. In this case it's a shame to restrict its usage to CJS environments only, don't you think?
Ah.. if you need context you still can use import('/a/a' + name)
require.context exists only because webpack starts when ESM was not ready, so require.context is just hack for import(...), because currently ESM is preferable and we should reduce usage of commonjs, we stop to add new features for require (i.e. CommonJS) and also block them to ESM
Unless I'm missing something here, it could have been called webpack.context or blabla and work the same way. The fact that it has require in it is arbitrary.
@vankop@tavoyne To be honest, I have quite different feelings, on the one hand, I understand well how it is used and why it is needed, on the other hand, we already had the experience of adding new stuff to module system and it always ended badly.
It changes import logic, it is very bad idea, we should not do it, in this case better to use import.meta.context(), because import.meta.* is free to use, maybe even import.meta.webpackContent to prevent overlapping with other tools
import.meta.webpackContext would be the name to choose here.
But we should use the chance to improve the API a bit and let it take an object instead of many arguments. This would also allow to add the other options a context takes, like include or exclude or mode
Activity
vankop commentedon Feb 11, 2022
Thanks for your issue. This done intentionally since in esm there is no
require, you can mark this js file astype="javascript/auto"in yourwebpack.config( use https://webpack.js.org/configuration/module/#ruletype )Ah.. if you need
contextyou still can useimport('/a/a' + name)https://webpack.js.org/api/module-methods/#dynamic-expressions-in-importtavoyne commentedon Feb 11, 2022
I understand, but the fact that it's called
require.contextis anecdotical, isn't it? I mean Webpack doesn't use therequirefunction under the hood, it just parses the expression. In this case it's a shame to restrict its usage to CJS environments only, don't you think?I agree this is handy, but I need the filenames.
vankop commentedon Feb 11, 2022
Webpack tries to follow specs. Since esm kind of strict environment there is no cjs stuff in it.
tavoyne commentedon Feb 11, 2022
But
require.contextis not a cjs stuff, it's webpack's.alexander-akait commentedon Feb 11, 2022
require.contextexists only because webpack starts when ESM was not ready, sorequire.contextis just hack forimport(...), because currently ESM is preferable and we should reduce usage of commonjs, we stop to add new features forrequire(i.e. CommonJS) and also block them to ESMtavoyne commentedon Feb 11, 2022
Unless I'm missing something here, it could have been called
webpack.contextorblablaand work the same way. The fact that it hasrequirein it is arbitrary.vankop commentedon Feb 11, 2022
@sokra should we add
import.meta.context?tavoyne commentedon Feb 11, 2022
I agree with you @alexander-akait. But
require.context()has an extra functionality compared toimport(): it's the fact that you can access filenames.tavoyne commentedon Feb 11, 2022
Here's an example of what you can do with
require.contextbut not withimport().It's so powerful.
Could it be even more agnostic, e.g.
webpack.context?alexander-akait commentedon Feb 11, 2022
@vankop @tavoyne To be honest, I have quite different feelings, on the one hand, I understand well how it is used and why it is needed, on the other hand, we already had the experience of adding new stuff to module system and it always ended badly.
tavoyne commentedon Feb 11, 2022
I understand.
And what about adding the functionality to
import()directly?This would work well with existing options, e.g.
webpackMode,webpackPrefetch,webpackInclude, etc.alexander-akait commentedon Feb 11, 2022
It changes
importlogic, it is very bad idea, we should not do it, in this case better to useimport.meta.context(), becauseimport.meta.*is free to use, maybe evenimport.meta.webpackContentto prevent overlapping with other toolstavoyne commentedon Feb 11, 2022
Makes complete sense. Could work on a PR if it's something that you'd consider integrating.
alexander-akait commentedon Feb 11, 2022
@sokra What do you think?
sokra commentedon Feb 15, 2022
import.meta.webpackContextwould be the name to choose here.But we should use the chance to improve the API a bit and let it take an object instead of many arguments. This would also allow to add the other options a context takes, like
includeorexcludeormodemaoxiaoke commentedon Mar 17, 2022
similar phenomenon was found when using require.resolveWeak in a pure ESM package @sokra @alexander-akait