是否有一种从webpack配置中获取webpack别名的好方法:
config.resolve.alias = {
NormalizeNodePath: (__dirname, 'node_modules/normalize.css/normalize.css'),
BluePrintIconsNodePath: (__dirname, 'node_modules/@blueprintjs/icons/lib/css/blueprint-icons.css'),
BluePrintCoreNodePath: (__dirname, 'node_modules/@blueprintjs/core/lib/css/blueprint.css')
}
并在javascript中将其公开为字符串?
答案 0 :(得分:2)
如果我对问题的理解正确,有几种方法可以完成此任务。
// paths.js
const NormalizeNodePath = join(__dirname, 'node_modules/normalize.css/normalize.css')
module.exports = {
NormalizeNodePath,
}
// webpack.config.js
const { NormalizeNodePath } = require('./paths')
// use it in alias config
// src/whatever.js
const { NormalizeNodePath } = require('../paths')
// use in your code
DefinePlugin
并将其公开为代码中的常量,该值在捆绑期间将替换为文字字符串。const NormalizeNodePath = join(__dirname, 'node_modules/normalize.css/normalize.css')
module.exports = {
// other config...
resolve: {
alias: {
NormalizeNodePath
},
},
plugins: [
new webpack.DefinePlugin({
NormalizeNodePath,
})
]
}
// Use the variable name `NormalizeNodePath` in your code
答案 1 :(得分:1)
您可以使用DefinePlugin
将全局变量注入代码中。
示例
const alias = {
NormalizeNodePath: (__dirname, "node_modules/normalize.css/normalize.css"),
BluePrintIconsNodePath: (__dirname, "node_modules/@blueprintjs/icons/lib/css/blueprint-icons.css"),
BluePrintCoreNodePath: (__dirname, "node_modules/@blueprintjs/core/lib/css/blueprint.css")
};
const config = {
resolve: {
alias
},
plugins: [new webpack.DefinePlugin(alias)]
};