1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// webpack.common.js
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const resolve = (dir) => path.resolve(__dirname, dir);

module.exports = {
entry: [
resolve('./src/index.tsx')
],
output: {
path: resolve('./dist'),
publicPath: '/',
filename: '[name].[hash].js',
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.(js|jsx)$/,
exclude: [
resolve('./node_modules'),
resolve('./dist'),
],
use: ['babel-loader'],
},
{
test: /\.css$|\.scss$/,
use: [
'style-loader',
'css-loader?url=false',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
resources: [resolve('./src/assets/sass/variables.scss')],
caches: true
},
},
],
include: [
resolve('./src'),
resolve('./node_modules/normalize.css'),
resolve('./node_modules/react-toastify/dist/ReactToastify.css'),
],
},
{
test: /\.jpe?g$|\.png$|\.svg$|\.woff$|\.ttf$/,
loader: 'file-loader?name=[name].[ext]'
},
],
},
resolve: {
extensions: ['.js', '.jsx', '.es6', '.ts', '.tsx'],
alias: {
'@': resolve('src'),
},
},
plugins: [
new HtmlWebpackPlugin({
template: resolve('./public/index.html'),
favicon: resolve('./src/assets/images/favicon.svg'),
title: 'Caching',
}),
new webpack.HotModuleReplacementPlugin(),
],
devServer: {
historyApiFallback: true,
hot: true,
proxy: {
'/api/**': {
target: 'http://dev-api.baixing.cn',
changeOrigin: true,
secure: false,
},
}
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// webpack.dev.js
const webpack = require('webpack');
const merge = require('webpack-merge');
const common = require('../webpack.common.js');
const path = require('path');
const dotenv = require('dotenv');

const resolve = (dir) => path.resolve(__dirname, dir);

const envFile = dotenv.config({ path: resolve('../.env.dev') }).parsed
const envKeys = Object.keys(envFile).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(envFile[next]);
return prev;
}, {})

module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
output: {
publicPath: '/promotion/',
},
plugins: [
new webpack.DefinePlugin(envKeys)
]
})
1
2
3
4
5
// package.json
"scripts": {
"start": "webpack-dev-server --open --config ./config/webpack.local.js",
"build-dev": "webpack --config ./config/webpack.dev.js",
}