-
Notifications
You must be signed in to change notification settings - Fork 17
/
webpack.config.base.js
123 lines (116 loc) · 3.3 KB
/
webpack.config.base.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
'use strict'
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const routeDataMapper = require('webpack-route-data-mapper')
const readConfig = require('read-config')
const path = require('path')
// base config
const SRC = './src'
const DEST = './public'
const HOST = process.env.HOST || '0.0.0.0'
const PORT = process.env.PORT || 3000
const constants = readConfig(`${SRC}/constants.yml`)
const { BASE_DIR } = constants
// page/**/*.pug -> dist/**/*.html
const htmlTemplates = routeDataMapper({
baseDir: `${SRC}/pug/page`,
src: '**/[!_]*.pug',
locals: Object.assign(
{},
constants,
{
meta: readConfig(`${SRC}/pug/meta.yml`)
}
)
})
module.exports = {
// エントリーファイル
entry: {
'js/script.js': `${SRC}/js/script.js`,
'css/style.css': `${SRC}/scss/style.scss`,
},
// 出力するディレクトリ・ファイル名などの設定
output: {
path: path.resolve(__dirname, DEST + BASE_DIR),
filename: '[name]',
publicPath: BASE_DIR,
},
module: {
// 各ファイル形式ごとのビルド設定
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
options: {
compact: true,
cacheDirectory: true,
}
},
{
test: /\.pug$/,
use: [
{
loader: 'pug-loader',
options: {
root: path.resolve(`${SRC}/pug/`),
pretty: true,
}
}
],
},
{
test: /\.(jpe?g|png|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
importLoaders: 2,
}
},
'postcss-loader',
{
loader: 'sass-loader',
options: {
includePaths: [ `${SRC}/scss` ],
},
}
]
})
},
{
test: /.ya?ml$/,
loader: 'js-yaml-loader',
}
]
},
// webpack-dev-serverの設定
devServer: {
host: HOST,
port: PORT,
contentBase: DEST,
openPage: path.relative('/', BASE_DIR),
},
// キャシュ有効化
cache: true,
// 拡張子省略時のpath解決
resolve: {
extensions: ['.js', '.json', '*'],
alias: {
'@': path.join(__dirname, SRC, 'js'),
}
},
plugins: [
// 複数のHTMLファイルを出力する
...htmlTemplates,
// style.cssを出力
new ExtractTextPlugin('[name]')
],
}