forked from laozhu/hugo-nuo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
136 lines (131 loc) · 3.41 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
const path = require('path');
const ManifestPlugin = require('webpack-manifest-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// ENV constant
const ENV = { mode: process.env.NODE_ENV };
ENV.isDev = process.env.NODE_ENV === 'development';
ENV.isProd = process.env.NODE_ENV === 'production';
ENV.devtool = ENV.isProd ? 'source-map' : 'cheap-module-eval-source-map';
console.log(`Running webpack in the ${process.env.NODE_ENV} mode`);
// PATH constant
const PATH = { base: __dirname };
PATH.src = path.resolve(PATH.base, 'src');
PATH.styles = path.resolve(PATH.src, 'styles');
PATH.scripts = path.resolve(PATH.src, 'scripts');
PATH.images = path.resolve(PATH.src, 'images');
PATH.fonts = path.resolve(PATH.src, 'fonts');
PATH.exclude = /(node_modules|bower_components)/;
PATH.entry = { bundle: ['./src/styles/main.css', './src/scripts/main.js'] };
PATH.dist = path.resolve(PATH.base, 'static/static');
PATH.manifest = path.resolve(PATH.base, 'data', 'manifest.json');
PATH.publicPath = '/static/';
PATH.filename = {
scripts: 'scripts/[name].js?v=[chunkhash:10]',
styles: 'styles/[name].css?v=[chunkhash:10]',
images: 'images/[name].[ext]?v=[hash:10]',
fonts: 'fonts/[name].[ext]?v=[hash:10]',
};
const loaders = env => [
{
test: /\.css$/,
exclude: PATH.exclude,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: true,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: [
require('stylelint')({
config: {
"extends": "stylelint-config-standard",
},
}),
require('postcss-import')(),
require('postcss-preset-env')({
stage: 2,
features: {
'nesting-rules': true
}
}),
require('postcss-reporter')({
clearReportedMessages: true,
}),
require('autoprefixer')(),
require('cssnano')(),
],
},
},
],
},
{
test: /\.js$/,
exclude: PATH.exclude,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: env.isProd ? false : true,
presets: ['@babel/preset-env'],
},
},
{
loader: 'eslint-loader',
options: {
useEslintrc: false,
envs: [ "browser", "es6" ],
extends: "eslint:recommended",
},
},
],
},
{
test: /\.(png|svg|webp|jpe?g|gif)$/,
include: PATH.images,
exclude: PATH.exclude,
loader: 'file-loader',
options: {
name: PATH.filename.images,
},
},
{
test: /\.(eot|ttf|otf|woff2?|svg)$/,
include: PATH.fonts,
exclude: PATH.exclude,
loader: 'file-loader',
options: {
name: PATH.filename.fonts,
},
},
]
const plugins = env => [
new MiniCssExtractPlugin({
filename: PATH.filename.styles,
}),
new ManifestPlugin({
fileName: PATH.manifest,
filter: chunk => chunk.name && /\S*.(js|css)$/.test(chunk.name),
}),
]
module.exports = {
mode: ENV.mode,
context: PATH.base,
entry: PATH.entry,
output: {
path: PATH.dist,
publicPath: PATH.publicPath,
filename: PATH.filename.scripts,
},
devtool: ENV.devtool,
module: {
rules: loaders(ENV),
},
plugins: plugins(ENV),
};