Javascript 如何设置QA模式的网页包/网页包服务?
我有一个 webpack.common.js webpack.dev.js webpack.prod.js webpack.qa.js 我设置的3种环境中的每一种都有一种模式Javascript 如何设置QA模式的网页包/网页包服务?,javascript,webpack,Javascript,Webpack,我有一个 webpack.common.js webpack.dev.js webpack.prod.js webpack.qa.js 我设置的3种环境中的每一种都有一种模式 const path = require("path"); const merge = require("webpack-merge"); const convert = require("koa-connect"); const proxy = require("http-proxy-middleware"); cons
const path = require("path");
const merge = require("webpack-merge");
const convert = require("koa-connect");
const proxy = require("http-proxy-middleware");
const historyApiFallback = require("koa2-connect-history-api-fallback");
const common = require("./webpack.common.js");
module.exports = merge(common, {
// Provides process.env.NODE_ENV with value development.
// Enables NamedChunksPlugin and NamedModulesPlugin.
mode: "QA",
devtool: "inline-source-map",
// configure `webpack-serve` options here
serve: {
// The path, or array of paths, from which static content will be served.
// Default: process.cwd()
// see https://github.com/webpack-contrib/webpack-serve#options
content: path.resolve(__dirname, "dist"),
add: (app, middleware, options) => {
// SPA are usually served through index.html so when the user refresh from another
// location say /about, the server will fail to GET anything from /about. We use
// HTML5 History API to change the requested location to the index we specified
app.use(historyApiFallback());
app.use(
convert(
// Although we are using HTML History API to redirect any sub-directory requests to index.html,
// the server is still requesting resources like JavaScript in relative paths,
// for example http://localhost:8080/users/main.js, therefore we need proxy to
// redirect all non-html sub-directory requests back to base path too
proxy(
// if pathname matches RegEx and is GET
(pathname, req) => pathname.match("/.*/") && req.method === "GET",
{
// options.target, required
target: "http://localhost:8080",
pathRewrite: {
"^/.*/": "/" // rewrite back to base path
}
}
)
)
);
}
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: ["style-loader", "css-loader", "sass-loader"]
}
]
}
});
然后在我的代码中,我通过
process.env.NODE\u env
引用模式,但是当我运行QA配置时,我得到
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.mode should be one of these:
"development" | "production" | "none"
-> Enable production optimizations or development hints.
我如何设置这些模式以外的模式
我正在使用webpack和
编辑
在my package.json中
"scripts": {
"dev": "webpack-serve --config webpack.dev.js --open",
"prod": "webpack -p --config webpack.prod.js",
"qa": "cross-env NODE_ENV=QA webpack --config webpack.prod.js"
},
然后在我的一个文件里
console.log("process.env.NODE_ENV",process.env.NODE_ENV)
你不能设定!只有这3种模式可用。这些模式不需要用于不同的环境,它们只是webpack发现的将某些默认值应用于基于环境的构建的一种方式。如果你认为你的问答应该(实际上应该)像prod一样,那就把它当作prod TL;TR:模式与env无关,它们只是将某些默认值应用于构建的一种方式
要访问前端代码上的
process.env.NODE_env
,必须使用:
嗯,我正在使用mode将我的基本URL更改为我的API服务器,在Dev、QA和prod中有什么不同。我应该使用什么来代替呢?process.env.NODE_env在哪里设置?我检查了我的代码,我已经在使用process.env.NODE_env了,但是没有通过“mode”设置它,如果是这样,那么我就不能设置QA标志。是的,它不是通过mode设置的。在运行脚本时,必须将此添加到。您最好使用cross-env来实现这一点:
“build:qa”:“cross-env NODE\u env=Q&A网页”
。差不多吧。
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
});