Skip to content

Commit b9006f7

Browse files
committed
add different env config
1 parent de157e8 commit b9006f7

8 files changed

+30
-2
lines changed

build/webpack.dev.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ const path = require('path')
22
const webpack = require('webpack')
33
const HtmlWebpackPlugin = require('html-webpack-plugin')
44
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
5+
const fs = require('fs')
56

67
const PORT = 4222;
78
const HOST = 'http://localhost';
89
const URL = `${HOST}:${PORT}`;
910

11+
const config = fs.readFileSync(path.join(__dirname, '../config/config-local.js'), 'utf-8')
12+
1013
module.exports = {
1114
mode: 'development',
1215
entry: [
@@ -72,6 +75,7 @@ module.exports = {
7275
new HtmlWebpackPlugin({
7376
template: "../src/app.html",
7477
filename: "index.html",
78+
config: `<script>${config}</script>`
7579
}),
7680
new webpack.HotModuleReplacementPlugin(),
7781

build/webpack.prod.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module.exports = {
8585
new HtmlWebpackPlugin({
8686
template: "../src/app.html",
8787
filename: "index.html",
88+
config: '<script src="config.js"></script>'
8889
}),
8990
new MiniCssExtractPlugin({
9091
filename: "[name].[hash:7].css",

config/config-local.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.CONFIG = {
2+
api: 'http://localhost:8089/api'
3+
}

config/config-prod.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.CONFIG = {
2+
api: 'http://api-o.yixi.pro/api'
3+
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"scripts": {
2323
"dev": "yarn start",
2424
"start": "webpack-dev-server --open --config build/webpack.dev.config.js --progress",
25-
"build": "webpack --config build/webpack.prod.config.js",
25+
"package": "webpack --config build/webpack.prod.config.js",
26+
"build": "yarn package && cp config/config-prod.js dist/config.js",
2627
"deploy:gh-page": "node ./build/gh-pages-deploy.js",
2728
"test": "jest",
2829
"test:coverage": "jest --coverage",

src/app.html

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
88
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css" />
99
<title>Outliner</title>
10+
<%= htmlWebpackPlugin.options.config %>
1011
</head>
1112
<body>
1213
<div id="MainApp"></div>

src/hooks/useRequest.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import useSWR, { ConfigInterface, responseInterface } from 'swr'
22
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
33
import Auth from '@root/tools/auth/auth'
4+
import getConfig from '@root/tools/getConfig'
45

56
export type GetRequest = AxiosRequestConfig | null
67

@@ -19,7 +20,7 @@ export interface IConfig<Data = unknown, Error = unknown>
1920

2021
export const httpRequest = axios.create({
2122
timeout: 10 * 1000,
22-
baseURL: 'http://localhost:8089/api',
23+
baseURL: getConfig().api,
2324
})
2425

2526
httpRequest.interceptors.request.use((config) => {

src/tools/getConfig.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
interface IGlobalConfig {
2+
api: string
3+
}
4+
5+
declare global {
6+
// tslint:disable-next-line:interface-name
7+
interface Window {
8+
CONFIG: IGlobalConfig
9+
}
10+
}
11+
12+
export default function getConfig() {
13+
return window.CONFIG || {}
14+
}

0 commit comments

Comments
 (0)