-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
206 lines (195 loc) · 5.26 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
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
const path = require('path');
const fs = require('fs');
const sourcePath = './src/';
const outputPath = './dist/';
const copyStateLibs = fs.existsSync('./src/libs') && fs.lstatSync('./src/libs').isDirectory();
const copyStateFont = fs.existsSync('./src/font') && fs.lstatSync('./src/font').isDirectory();
console.log(`CopyWebpackPlugin(libs) : ${copyStateLibs}`);
console.log(`CopyWebpackPlugin(font) : ${copyStateFont}`);
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlBeautifyPlugin = require('@nurminen/html-beautify-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
function generateHtmlPlugins(templateDir) {
const templateFiles = fs.readdirSync(templateDir).filter((file) => file.substr(-5) === '.html');
return templateFiles.map((file) => new HtmlWebpackPlugin({
template: `./${file}`,
filename: `${file}`,
minify: false,
hash: true,
inject: 'body',
chunks: 'all',
}));
}
module.exports = (env, argv) => {
// Webpack 플러그인
const plugins = [
new ESLintPlugin(),
new VueLoaderPlugin(),
new CleanWebpackPlugin({
protectWebpackAssets: false,
}),
new MiniCssExtractPlugin({
filename: 'css/style.css',
}),
];
// html의 개수에 따라 HtmlWebpackPlugin 생성
const htmlList = generateHtmlPlugins(sourcePath);
// HtmlWebpackPlugin 확장 플러그인
const htmlPlugins = [
new HtmlBeautifyPlugin({
config: {
html: {
indent_size: 2,
end_with_newline: true,
preserve_newlines: true,
unformatted: ['p', 'i', 'b', 'span'],
},
},
}),
];
function copyPlugin() {
let val = [];
if (copyStateLibs && !copyStateFont) {
val = [
new CopyWebpackPlugin({
patterns: [
{
from: './libs/**/*',
},
],
}),
];
}
if (!copyStateLibs && copyStateFont) {
val = [
new CopyWebpackPlugin({
patterns: [
{
from: './font/**/*',
},
],
}),
];
}
if (copyStateLibs && copyStateFont) {
val = [
new CopyWebpackPlugin({
patterns: [
{
from: './libs/**/*',
},
{
from: './font/**/*',
},
],
}),
];
}
return val;
}
console.log('\n********************************************************************************');
console.log(`🚀 Build Mode: ${argv.mode}`);
console.log('********************************************************************************\n');
return {
context: path.resolve(__dirname, sourcePath),
entry: {
app: ['./js/app.js'],
},
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, outputPath),
publicPath: '/',
},
target: ['web', 'es5'],
resolve: {
alias: {
vue$: 'vue/dist/vue.esm.js',
},
extensions: ['*', '.js', '.vue', '.json'],
},
devServer: {
static: {
directory: path.resolve(__dirname, sourcePath),
watch: true,
},
open: true,
},
stats: {
preset: 'errors-only',
builtAt: true,
timings: true,
version: true,
},
mode: argv.mode === 'development' ? 'development' : 'production',
devtool: argv.mode === 'development' ? 'source-map' : false,
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
},
},
},
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
},
performance: {
hints: argv.mode === 'production' ? 'warning' : false,
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.(sa|sc|c)ss$/,
use: [
argv.mode === 'development' ? 'style-loader'
: {
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '/',
},
},
'css-loader',
{
loader: 'sass-loader',
options: {
// eslint-disable-next-line global-require
implementation: require('sass'),
},
},
],
},
{
test: /\.(gif|png|jpe?g)$/,
include: /images/,
type: 'asset/resource',
generator: {
filename: argv.mode === 'development' ? 'images/[name][ext]' : 'images/[name][ext]?[hash]',
},
},
{
test: /\.m?js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
configFile: './.babelrc',
},
},
],
},
plugins: plugins.concat(copyPlugin()).concat(htmlList).concat(htmlPlugins),
};
};