【发布时间】:2016-02-28 04:52:00
【问题描述】:
我们在单页应用程序中使用 WebPack。该应用程序被部署到许多环境中。我们有一个要求,即应用程序需要在给定环境中调用特定端点。
为了给给定环境提供端点地址,需要有一个环境模块。这是当前的解决方案(有很多,这不是问题的重点)。但是,我们需要将 config.js 排除在缩小范围之外,以便在部署过程中将其覆盖。
config.js 如下所示:
module.exports = {
env: {
endpointUrl: 'http://1.2.3.4',
authUrl: 'http://5.6.7.8'
}
};
并使用以下方式引用:
const endpointUrl = config.env.endpointUrl;
const authUrl = config.env.authUrl;
WebPack 配置如下所示:
var webpack = require('webpack');
module.exports = {
entry: {
main: './src/js/main.jsx',
login: './src/js/login-main.jsx'
},
output: {
path: __dirname + '/dist',
filename: '[name].bundle.js'
},
devtool: 'source-map',
module: {
loaders: [{
test: /.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
plugins: ['transform-react-jsx'],
query: {stage: 0}
}, {
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}]
},
plugins: [
new webpack.ProvidePlugin({
fetch: 'imports?this=>global!exports?global.fetch!whatwg-fetch'
}),
new webpack.DefinePlugin({
__DEV__: JSON.stringify(JSON.parse(process.env.DEV || false))
})
]
};
到目前为止,我们已经查看了 externals 和 module loaders,但没有发现任何可行的方法。模块加载器中的 exclude 仍然会导致模块被缩小。
我们研究过的一些 SO 问题:
【问题讨论】:
标签: javascript webpack bundling-and-minification