/
webpack.config.ts
175 lines (164 loc) · 3.82 KB
/
webpack.config.ts
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
const path = require('path');
const CopyModulesWebpackPlugin = require('copy-modules-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { REPLACEMENTS } = require('@leanup/stack/lib/replacements');
REPLACEMENTS.forEach((replacement: { from: any; search: any; to: any; replace: any }) => {
replacement.search = replacement.from;
replacement.replace = replacement.to;
delete replacement.from;
delete replacement.to;
});
// https://webpack.js.org/configuration/dev-server/#devserverproxy
const { PROXIES } = require('@leanup/stack/lib/proxies');
const ESBUILD_LOADER_JS = {
test: /\.js$/,
loader: 'esbuild-loader',
};
const ESBUILD_LOADER_TS = {
test: /\.ts$/,
loader: 'esbuild-loader',
options: {
loader: 'ts',
},
};
const FONT_FILE_LOADER = {
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'assets/fonts',
},
},
],
};
const IMAGE_FILE_LOADER = {
test: /\.(png|svg|jpg|jpeg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
outputPath: 'assets/images',
},
},
],
};
const LESS_LOADER = {
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
};
const SASS_LOADER = {
test: /\.(sa|s?c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
sassOptions: {
includePaths: ['node_modules'],
},
},
},
],
};
const STRING_REPLACE_LOADER = {
test: /\.(j|t)sx?$/,
loader: 'string-replace-loader',
options: {
multiple: REPLACEMENTS,
},
};
export const LEANUP_WEBPACK_RULES = {
STRING_REPLACE_LOADER,
ESBUILD_LOADER_JS,
ESBUILD_LOADER_TS,
FONT_FILE_LOADER,
IMAGE_FILE_LOADER,
LESS_LOADER,
SASS_LOADER,
};
export function webpackConfig(env: any, argv: any, loaders: any[] = []): Object {
argv.host = typeof argv.host === 'string' ? argv.host : 'localhost';
const config = {
devServer: {
compress: true,
contentBase: path.join(process.cwd(), `public`),
host: argv.host,
disableHostCheck: true,
publicPath: '/',
proxy: PROXIES,
},
entry: {
main: path.join(process.cwd(), `src`, `main.ts`),
},
module: {
rules: [
STRING_REPLACE_LOADER,
ESBUILD_LOADER_JS,
ESBUILD_LOADER_TS,
FONT_FILE_LOADER,
IMAGE_FILE_LOADER,
LESS_LOADER,
SASS_LOADER,
].concat(loaders),
},
// optimization: {
// minimize: true,
// minimizer: [
// new ESBuildMinifyPlugin({
// target: 'es2015',
// }),
// ],
// },
output: {
path: path.join(process.cwd(), 'dist'),
},
plugins: [
new CopyModulesWebpackPlugin({
destination: '.reports/nexus-iq',
includePackageJsons: true,
}),
new CopyPlugin({
patterns: [
{
from: 'public',
},
],
}),
new MiniCssExtractPlugin(),
],
resolve: {
alias: {},
modules: ['node_modules'],
extensions: ['.mjs', '.js', '.jsx', '.svelte', '.ts', '.tsx', '.vue', '.gql', '.graphql'],
},
};
const cannotFindCliModule = /Cannot find module.+@leanup\/cli/;
function loadAddon(name: string) {
try {
require(`@leanup/cli-${name}/webpack.config`)(argv, config);
} catch (error) {
if (false === cannotFindCliModule.test(error)) {
throw error;
}
}
}
['addons', 'cucumber', 'graphql', 'pwa', 'webhint'].forEach(loadAddon);
return config;
}