Browse Source

使用 DefinePlugin 定义前端全局变量

master
巴林闲侠 2 years ago
parent
commit
0c8f183ddd
  1. 2
      web/client/src/layout/containers/layout/index.js
  2. 8
      web/config.js
  3. 4
      web/package.json
  4. 1
      web/routes/attachment/index.js
  5. 12
      web/webpack.config.prod.js

2
web/client/src/layout/containers/layout/index.js

@ -66,7 +66,7 @@ const LayoutContainer = props => {
dom.scrollTop = 0;
}
})
console.log(API_ROOT);
return (
<Layout id="layout">
<Layout.Header style={{ padding: 0 }}>

8
web/config.js

@ -19,7 +19,7 @@ args.option('qnak', 'qiniuAccessKey');
args.option('qnsk', 'qiniuSecretKey');
args.option('qnbkt', 'qiniuBucket');
args.option('qndmn', 'qiniuDomain');
const flags = args.parse(process.argv);
const flags = dev ? args.parse(process.argv) : {};
const FS_UNIAPP_API = process.env.FS_UNIAPP_API || flags.apiUrl;
@ -45,7 +45,7 @@ if (
args.showHelp();
process.exit(-1);
}
console.log(1,);
const product = {
port: flags.port || 8080,
staticDirs: [path.join(__dirname, './client')],
@ -115,10 +115,9 @@ const product = {
maxRetries: 1
}
};
console.log(2, product);
let config;
if (dev) {
console.log(2.1);
config = {
port: product.port,
staticDirs: product.staticDirs,
@ -138,5 +137,4 @@ if (dev) {
config = product;
}
console.log(3, config);
module.exports = config;//区分开发和发布

4
web/package.json

@ -8,8 +8,8 @@
"start": "cross-env NODE_ENV=development npm run start-params",
"start-params": "node server -p 5400 -u http://127.0.0.1:4400",
"deploy": "export NODE_ENV=production && npm run build && node server",
"build-dev": "export NODE_ENV=development&&webpack --config webpack.config.js",
"build": "export NODE_ENV=production&&webpack --config webpack.config.prod.js"
"build-dev": "set NODE_ENV=development&&webpack --config webpack.config.js",
"build": "set NODE_ENV=production&&webpack --config webpack.config.prod.js"
},
"keywords": [
"app"

1
web/routes/attachment/index.js

@ -230,7 +230,6 @@ module.exports = {
}
router.use(download_);
router.get('/api/root', getApiRoot);
router.post('/_upload/new', upload);
router.delete('/_upload/cleanup', remove);
router.post('/_upload/attachments/ali/:p', uploadAliOSS);

12
web/webpack.config.prod.js

@ -31,7 +31,17 @@ module.exports = {
new HtmlWebpackPlugin({
filename: '../index.html',
template: './client/index.ejs'
})
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
...(() => {
let nextParams = {}
for (let k in config.frontParams) {
nextParams[k] = JSON.stringify(config.frontParams[k])
}
return nextParams
})()
}),
],
optimization: {
splitChunks: {

Loading…
Cancel
Save