-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.config.js
134 lines (130 loc) · 3.79 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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const webpack = require('webpack');
module.exports = (env, argv) => {
const { global, entry } = env;
const ASSETS_PATH = path.join(__dirname, env.path);
const ENTRY_PATH = path.join(ASSETS_PATH, 'src/index.tsx');
const DIST_PATH = path.join(ASSETS_PATH, 'dist/');
let ASSETS_UMD = env.path.replace('/packages/', '').split('-').join('_').toUpperCase();
console.log(ASSETS_UMD, global, entry, env);
const plugins = env.analysis
? [
new MiniCssExtractPlugin(),
// new BundleAnalyzerPlugin({
// analyzerPort: Math.round(Math.random() * 100 + 8000),
// }),
]
: [
new MiniCssExtractPlugin(),
new webpack.ProvidePlugin({
// process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
];
return {
entry: {
index: entry ? path.join(__dirname, entry) : ENTRY_PATH,
},
mode: argv.mode,
module: {
rules: [
{
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre',
exclude: /node_modules/, //不包含 node_modules 中的JS文件
},
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
['@babel/plugin-proposal-private-property-in-object', { loose: true }],
],
},
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
compilerOptions: {
declaration: false,
},
},
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
},
],
},
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader', // translates CSS into CommonJS
},
{
loader: 'less-loader', // compiles Less to CSS
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
sideEffects: true,
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ['file-loader'],
},
],
},
resolve: {
extensions: ['*', '.ts', '.tsx', '.js', '.jsx'],
fallback: {
fs: false, //https://webpack.js.org/migrate/5/#clean-up-configuration
// crypto: require.resolve('crypto-browserify'),
// constants: require.resolve('constants-browserify'),
// // // crypto: false,
// // // constants: false,
// stream: require.resolve('stream-browserify'),
// buffer: require.resolve('buffer'),
// buffer: require.resolve('buffer-browserify'),
},
},
// devtool: 'cheap-module-source-map',
output: {
library: global || ASSETS_UMD,
libraryTarget: 'umd',
path: DIST_PATH,
publicPath: './',
filename: 'index.min.js',
},
plugins: plugins,
externals: {
react: 'React',
'react-dom': 'ReactDOM',
'node:os': 'commonjs2 node:os',
},
watch: Boolean(env.watch),
};
};