-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
179 lines (176 loc) · 5.53 KB
/
webpack.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
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PurgecssPlugin = require("purgecss-webpack-plugin");
const glob = require('glob')
const fs = require('fs');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
// const CompressionWebpackPlugin = require('compress-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 = 'production';
const getWebpackConfig = (name, library) => {
return {
entry: {
[name]: './src/index.ts',
},
output: {
filename: '[name].min.js',
library: library,
libraryTarget: 'umd',
path: resolveApp('dist/'),
},
resolve: {
mainFields: ['module', 'main'],
extensions: ['.ts', '.tsx', '.js'],
modules: ['node_modules'],
fallback: { tty: false, os: false, util: false },
alias: {
'@/apis': path.resolve(__dirname, './src/apis'),
'@/store': path.resolve(__dirname, './src/store'),
'@/components': path.resolve(__dirname, './src/components'),
}
},
// externals: {
// react: {
// root: 'React',
// commonjs2: 'react',
// commonjs: 'react',
// amd: 'react',
// },
// 'react-dom': {
// root: 'ReactDom',
// commonjs2: 'react-dom',
// commonjs: 'react-dom',
// amd: 'react-dom',
// },
// },
module: {
rules: [
{
test: /\.worker\.ts$/,
loader: "worker-loader",
},
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [path.resolve('src')],
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: [{ loader: MiniCssExtractPlugin.loader }, { loader: 'css-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,
},
},
},
],
},
{
test: /\.(gif|jpg|png|bmp|eot|woff|woff2|ttf|svg)/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
outputPath: 'assets'
}
}
]
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new PurgecssPlugin({
paths: glob.sync(`${path.resolve(__dirname, 'src')}/**/*`, { nodir: true }),
}),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
...(process.env.MODE === 'ANALYZER' ? [new BundleAnalyzerPlugin({ analyzerMode: 'static' })] : []),
// new CompressionWebpackPlugin({
// exclude: /.(html|map)$/i
// }),
new CopyPlugin({
patterns: [
{ from: path.resolve(__dirname, './static'), to: "static" },
],
}),
],
performance: {
hints: false,
},
devtool: 'source-map',
};
};
module.exports = getWebpackConfig('index', 'index');;