-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
174 lines (165 loc) · 5.21 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
const path = require('path');
const glob = require('glob');
const sourcePath = './public/src/';
const outputPath = './public/dist/';
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const { CleanWebpackPlugin } = require('clean-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');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const siteInfo = require('./site-info');
function generateHtmlPlugins(templateDir) {
var templateFiles = glob.sync(`${templateDir}**/*.html`, { ignore: [`${templateDir}/_template/**/*.html`] }).map((file) => file.replace(templateDir, ''));
console.log(templateFiles);
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',
}),
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
}),
];
// 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'],
},
},
replace: [
{ test: '@@_title', with: siteInfo.title },
{ test: '@@_description', with: siteInfo.description },
{ test: '@@_keywords', with: siteInfo.description },
{ test: '@@_author', with: siteInfo.author },
{ test: '@@_og_locale', with: siteInfo.og.locale },
{ test: '@@_og_url', with: siteInfo.og.url },
{ test: '@@_og_type', with: siteInfo.og.type },
{ test: '@@_og_img_url', with: siteInfo.og.img.url },
{ test: '@@_og_img_type', with: siteInfo.og.img.type },
{ test: '@@_og_img_width', with: siteInfo.og.img.width },
{ test: '@@_og_img_height', with: siteInfo.og.img.height },
{ test: '@@_og_img_alt', with: siteInfo.og.img.alt },
{
test: 'content="/img',
with: argv.mode === 'development' ? 'content="/img' : `content="${siteInfo.og.img.url}img`,
},
],
}),
];
console.log('\n********************************************************************************');
console.log(`🚀 Build Mode: ${argv.mode}`);
console.log('********************************************************************************\n');
return {
context: path.resolve(__dirname, sourcePath),
entry: {
app: argv.mode === 'development' ? ['./css/development.scss', './css/style.scss', './js/app.js'] : ['./css/style.scss', './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: {
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)$/,
type: 'asset/resource',
generator: {
filename: argv.mode === 'development' ? '[path][name][ext]' : '[path][name][ext]?[hash]',
},
},
{
test: /\.m?js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
configFile: './.babelrc',
},
},
],
},
plugins: plugins.concat(htmlList).concat(htmlPlugins),
};
};