【发布时间】:2020-01-23 00:20:44
【问题描述】:
webpack.config.js
const path = require('path')
HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'index_bundle.js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-proposal-class-properties"]
}
}
},
{
test:/\.css$/,
use:['style-loader','css-loader']
}
]
},
plugins: [
new webpack.DefinePlugin({
APIHOST: JSON.stringify('test'),
BLOCKCHAINHOST: JSON.stringify('test')
}),
new HtmlWebpackPlugin({
template: './src/template.html'
}),
]
}
我定义了 2 个变量 APIHOST 和 BLOCKCHAINHOST,并尝试在 reactjs App.js 中进行控制台记录
componentDidMount() {
console.log(APIHOST)
}
我得到的错误是 APIHOST 未定义。我不确定在这里做什么,我尝试为 webpack.defineplugin 添加单引号,所以它看起来像 'APIHOST': JSON.stringify('test') 但它仍然给我同样的错误。
【问题讨论】:
-
尝试要求 webpack:
const webpack = require('webpack') -
如果
app编译并给出undefined我认为webpack是必需的。我想看到的是控制台的webpack输出。或者可能是Codesandbox,我可以在其中使用尽可能少的代码。 -
@RutherfordWonkington 你是对的,我怎么会错过这个
-
我终于意识到这是一个编译错误或警告。如果你的 webpack 配置中没有 eslint-loader,你应该试试这个:
new webpack.optimize.UglifyJsPlugin({ minimize : true, compress : { warnings : false } }),如果你的项目中有 eslint,尝试在你的.eslintrc.json中添加globals: {"APIHOST":true}
标签: javascript reactjs webpack ecmascript-6 ecmascript-5