-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.common.js
145 lines (139 loc) · 3.96 KB
/
webpack.common.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
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import ESLintPlugin from "eslint-webpack-plugin";
import fse from "fs-extra";
import { merge, capitalize } from "lodash-es";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import NodePolyfillPlugin from "node-polyfill-webpack-plugin";
import path from "path";
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
import webpack from "webpack";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export default (options = {}) => {
const cwd = process.cwd();
const name = options.name || "build";
const pack = JSON.parse(
fse.readFileSync(
new URL(
options.packagePath || path.join(cwd, "package.json"),
import.meta.url
)
)
);
pack.name = pack.name.split("-")[0] || pack.name;
const banner = options.banner || `${pack.name} ${name}\n@version ${pack.version}\n{@link ${pack.homepage}}`;
let plugins = [];
plugins.push(new webpack.BannerPlugin({ banner }));
plugins.push(new MiniCssExtractPlugin({ filename: "style.css" }));
plugins.push(new NodePolyfillPlugin());
plugins.push(new ESLintPlugin({ exclude: ["node_modules", "dist"] }));
plugins = plugins.concat(options.plugins || []);
const mock = merge(
{
https: true,
http: true,
net: true,
tls: true,
os: true,
"fs-extra": true,
fs: true,
dns: true,
},
options.mock
);
const include = options.include || [];
const mockIndexPath = options.mockIndexPath || path.resolve(__dirname, "src/browser/mock");
const isProd = options.isProd === undefined? process.env.NODE_ENV == "production": options.isProd;
const alias = options.alias || {};
const entry = {};
const mainEntry = options.entry || path.resolve(cwd, `src/browser/${name}`);
entry[`${pack.name}.${name}`] = mainEntry;
for (let key in mock) {
const val = mock[key];
if (val === false) {
continue;
}
alias[key] = val === true? mockIndexPath : val;
}
return merge(
{
mode: isProd? "production" : "development",
performance: { hints: false },
watch: !isProd,
devtool: isProd? false : "inline-source-map",
entry,
output: {
path: options.distPath || path.join(cwd, `/dist/${name}`),
filename: "[name].js",
library: options.library || capitalize(name) + capitalize(pack.name),
libraryTarget: "umd",
libraryExport: "default",
clean: true,
},
optimization: {
minimizer: [
new TerserPlugin({
extractComments: false,
}),
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
mergeRules: false,
},
],
},
}),
],
},
plugins,
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/,
include,
options: {
configFile: path.join(mainEntry, ".babelrc"),
},
},
{
test: /\.html$/,
loader: "html-loader",
options: {
esModule: false,
minimize: {
removeScriptTypeAttributes: false,
},
},
},
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"resolve-url-loader",
{
loader: "sass-loader",
options: {
sourceMap: true,
},
},
],
},
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
type: 'asset/resource',
dependency: { not: ['url'] }
},
],
},
resolve: {
alias
}
},
options.config
);
};