-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.development.config.js
208 lines (205 loc) · 6.6 KB
/
webpack.development.config.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PurgecssPlugin = require("purgecss-webpack-plugin");
const fs = require('fs');
const path = require('path');
const glob = require('glob')
const WebpackBar = require('webpackbar');
const CopyPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
process.env.NODE_ENV = 'development';
const getWebpackConfig = (name, library) => {
return {
entry: {
[name]: './examples/index.tsx',
},
// target: 'node',
output: {
filename: '[name].min.js',
library: library,
libraryTarget: 'umd',
path: resolveApp('dist/'),
publicPath: '/',
},
resolve: {
mainFields: ['module', 'main'],
extensions: ['.ts', '.tsx', '.js', 'jsx'],
modules: ['node_modules'],
fallback: {
tty: false,
os: false,
// 为了 axios, 安装的 polyfill, webpack 5 有 break change, 不再支持这些 polyfill 了,需要分别手动安装配置 来支持 axios
util: require.resolve("util"),
path: require.resolve("path-browserify"),
stream: require.resolve("stream-browserify"),
url: require.resolve("url/"),
https: require.resolve("https-browserify"),
fs: false,
http: require.resolve("stream-http"),
buffer: require.resolve("buffer")
// end
},
alias: {
'@/apis': path.resolve(__dirname, './src/apis'),
'@/store': path.resolve(__dirname, './src/store'),
'@/components': path.resolve(__dirname, './src/components'),
}
},
devServer: {
compress: true,
port: 3001,
static: {
directory: path.join(__dirname, 'dist'),
},
hot: true,
open: true,
historyApiFallback: true,
},
externals: {
},
module: {
rules: [
{
test: /\.worker\.ts$/,
loader: "worker-loader",
},
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [path.resolve('src'), path.resolve('examples'), path.resolve('documents')],
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve('babel-preset-react-app/webpack-overrides'),
presets: [
[
require.resolve('babel-preset-react-app'),
{
runtime: 'classic',
},
],
],
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-svgo,+titleProp,+ref![path]',
},
},
},
],
].filter(Boolean),
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: true,
},
},
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [[require.resolve('babel-preset-react-app/dependencies'), { helpers: true }]],
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
// Babel sourcemaps are needed for debugging into node_modules
// code. Without the options below, debuggers like VSCode
// show incorrect code and set breakpoints on the wrong lines.
sourceMaps: true,
inputSourceMap: true,
},
},
{
test: /\.css$/,
// exclude: /node_modules/,
use: ['style-loader', { loader: 'css-loader' }, 'postcss-loader'],
},
{
test: /\.less$/,
exclude: /node_modules/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{ loader: 'css-loader', options: {
modules: {
// localIdentName: '[hash:base64:6]'
localIdentName: '[folder]_[local]',
},
importLoaders: 2
} },
{
loader: 'less-loader',
options: {
sourceMap: true,
lessOptions: {
javascriptEnabled: true,
modifyVars: {},
}
},
},
],
},
{
test: /\.(gif|jpg|png|bmp|eot|woff|woff2|ttf|svg)/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
outputPath: 'assets'
}
}
]
}
],
},
plugins: [
new NodePolyfillPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new PurgecssPlugin({
paths: glob.sync(`${path.resolve(__dirname, 'src')}/**/*`, { nodir: true }),
}),
new HtmlWebpackPlugin({
template: 'examples/index.html',
filename: 'index.html',
inject: true,
templateParameters: {
title: '家庭工具坊',
keywords: '家庭 工具坊 会生活 深证',
description: '过好每一天,利用好手头的信息,进行家庭决策',
}
}),
new webpack.optimize.AggressiveMergingPlugin(),
...(process.env.MODE === 'ANALYZER' ? [new BundleAnalyzerPlugin({ analyzerMode: 'static' })] : []),
new WebpackBar(),
// 解决 process not definded 的问题
new webpack.DefinePlugin({
process: {env: {}}
}),
new CopyPlugin({
patterns: [
{ from: path.resolve(__dirname, './static'), to: "static" },
],
}),
],
performance: {
hints: false,
},
devtool: 'source-map',
};
};
module.exports = getWebpackConfig('index', 'index');;