有没有一个好方法可以从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中将其公开为字符串?
发布于 2018-07-16 02:10:49
如果我对这个问题的理解正确的话,有几种方法可以做到这一点。
中
// 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
发布于 2018-07-16 02:09:36
您可以使用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)]
};
https://stackoverflow.com/questions/51350652
复制相似问题