-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwebpack.common.js
More file actions
121 lines (116 loc) · 3.35 KB
/
webpack.common.js
File metadata and controls
121 lines (116 loc) · 3.35 KB
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
const path = require('path')
const HandlebarsPlugin = require('handlebars-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const postcssPresetEnv = require('postcss-preset-env')
const ImageminPlugin = require('imagemin-webpack-plugin').default
const cssnano = require('cssnano')
const { registerHandlersHelpers } = require('./webpack.helpers.js')
const mode = process.env.NODE_ENV || 'production'
const sourceDir = path.join(__dirname, 'src')
const templateDir = path.join(__dirname, 'generated')
const buildDir = path.join(__dirname, 'build')
const isProd = mode === 'production'
const prodPlugins = [new ImageminPlugin({ test: /\.(jpeg|png|gif|svg)$/i })]
module.exports = {
mode,
devtool: 'source-map',
entry: path.join(sourceDir, 'entry.js'),
output: {
filename: isProd ? 'bundle.[chunkhash].js' : './bundle.js',
path: buildDir,
publicPath: '/',
},
resolve: {
alias: {
'~constants': `${sourceDir}/js/constants`,
'~managers': `${sourceDir}/js/managers`,
'~utils': `${sourceDir}/js/utils`,
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { importLoaders: 1 } },
isProd
? {
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [postcssPresetEnv(), cssnano()],
},
}
: null,
'sass-loader',
].filter(Boolean),
},
{
test: /\.(eot|ttf|woff|woff2)$/,
use: [
{
loader: 'file-loader',
options: { name: 'fonts/[name].[ext]' },
},
],
},
{
test: /\.(glsl|vs|fs|vert|frag)$/,
use: ['webpack-glsl-loader'],
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
{
loader: 'ignore-loader',
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(sourceDir, 'views', 'layout', 'template.hbs'),
filename: path.join(templateDir, 'template.hbs'),
inject: false,
}),
new HandlebarsPlugin({
htmlWebpackPlugin: {
enabled: true,
prefix: 'html',
},
entry: path.join(sourceDir, 'views', '*.hbs'),
output: name => {
const page = name !== 'index' ? name : ''
return path.join(buildDir, page, 'index.html')
},
data: path.join(sourceDir, 'data', '*.json'),
partials: [path.join(templateDir, 'template.hbs'), path.join(sourceDir, 'views', '*', '*.hbs')],
onBeforeSetup: Handlebars => {
return registerHandlersHelpers(Handlebars)
},
}),
new CopyWebpackPlugin([
{
from: path.join(sourceDir, 'img'),
to: 'img',
},
]),
new MiniCssExtractPlugin({
filename: isProd ? '[name].[chunkhash].css' : '[name].css',
chunkFilename: '[id].css',
fallback: 'style-loader',
use: [{ loader: 'css-loader', options: { minimize: isProd } }],
}),
].concat(isProd ? prodPlugins : [])
}