[英]Why does Webpack throw error when using this resolve.alias config?
Using an alias key which contains substring of another alias results in an error. 使用包含另一个别名的子字符串的别名键会导致错误。
//in this given file structure
src
|--app
| |--module.js
|--component
| |--module.js
|--config
|-config.js
//and config
alias: {
'base': path.resolve('src/config'),
'base/app': path.resolve('src/app'),
'base/component': path.resolve('src/component')
}
import app from 'base/config'
works as expected, returns module src/config.js 按预期工作,返回模块src / config.js
import app from 'base/app/module'
Module not found: Error: Can't resolve 'base/app/module' 找不到模块:错误:无法解析“基础/应用/模块”
import app from 'base/component/module'
Module not found: Error: Can't resolve 'base/component/module' 找不到模块:错误:无法解析“基础/组件/模块”
I believe this has to do with how webpack's enhanced-resolve AliasPlugin
works. 我认为这与webpack的增强解析AliasPlugin
工作方式有关。 By having the other base
be an alias and having other aliases start with base/
, you're triggering a false positive in this logic that you didn't intend since the path isn't actually below where base
points to (for base/app
, /src/app
is not below /src/config
) 通过将另一个base
用作别名,并让其他别名以base/
开头,您会在此逻辑中触发您不希望的误报,因为该路径实际上未低于base
指向的位置(对于base/app
, /src/app
不在/src/config
之下)
So, you could either use another delimiter ( _
) for this one instance, or better yet set up aliases that behave how Webpack intends just to avoid breaking changes in the future. 因此,您可以为此实例使用另一个定界符( _
),或者更好地设置别名,使其行为符合Webpack的意图,以避免将来破坏更改。
So you could replace the /
with another character, like so: 因此,您可以将/
替换为另一个字符,如下所示:
alias: {
'base': path.resolve('src/config'),
'base_app': path.resolve('src/app'),
'base_component': path.resolve('src/component')
}
// ...
import app from 'base_app/module'
Or if that's not ideal, you could instead redefine your alias to avoid the issue altogether: 或者,如果这不理想,则可以重新定义别名,以完全避免出现此问题:
alias: {
'base': path.resolve('src')
}
// ...
import config from 'base/config'
import app from 'base/app/module'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.