-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.dev.js
138 lines (135 loc) · 3.83 KB
/
webpack.dev.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
const path = require('path')
const webpack = require('webpack')
const recursive = require('recursive-readdir-sync')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const srcDir = path.resolve(__dirname, '..', 'src')
const distDir = path.resolve(__dirname, '..', 'dist')
const { NODE_ENV = 'development' } = process.env
function getPagesPath() {
try {
var files = recursive('./src/pages')
} catch (err) {
if (err.errno === 34) {
console.log('Path does not exist')
} else {
// something unrelated went wrong, rethrow
throw err
}
}
return files.filter(page => page.match(/.hbs$/))
}
module.exports = {
context: srcDir,
devtool: 'source-map',
entry: ['./index.js'],
output: {
// The destination file name concatenated with hash (generated whenever you change your code).
// The hash is really useful to let the browser knows when it should get a new bundle
// or use the one in cache
filename: 'main.js',
// The destination folder where to put the output bundle
path: distDir,
// Wherever resource (css, js, img) you call <script src="..."></script>,
// or css, or img use this path as the root
publicPath: '/',
// At some point you'll have to debug your code, that's why I'm giving you
// for free a source map file to make your life easier
sourceMapFilename: 'main.map'
},
devServer: {
contentBase: srcDir,
// match the output path
publicPath: '/',
// match the output `publicPath`
historyApiFallback: true,
port: 5000
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader'],
include: srcDir
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')({
browsers: [
'last 3 version',
'ie >= 10' // supports IE from version 10 onwards
]
})
]
}
},
'sass-loader'
]
},
{
test: /\.hbs$/,
loader: 'handlebars-loader',
query: {
partialDirs: [path.join(srcDir, 'templates', 'partials')],
helperDirs: [path.join(srcDir, 'templates', 'helpers')]
}
},
{
test: /\.(eot?.+|svg?.+|ttf?.+|zip?.+|ico?.+|otf?.+|woff?.+|woff2?.+)$/,
use: 'file-loader?[path][name].[ext]'
},
{
test: /\.(jpg|jpeg|png|gif|ico)$/,
use: [
// if less than 10Kb, bundle the asset inline, if greater, copy it to the dist/assets
// folder using file-loader
'url-loader?limit=1024&[path][name].[ext]'
],
include: path.resolve(srcDir, 'assets')
}
]
},
plugins: getPagesPath().reduce(
(memo, name) => {
const filename = name.match(/\/pages\/(.+).hbs$/)
memo.push(
new HtmlWebpackPlugin({
filename: filename[1] + '.html',
template: path.join(__dirname, '..', name),
path: distDir
})
)
return memo
},
[
new webpack.NamedModulesPlugin(),
// environment globals added must be added to .eslintrc
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(NODE_ENV)
},
NODE_ENV: NODE_ENV,
__DEV__: NODE_ENV === 'development',
__PROD__: NODE_ENV === 'production'
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new Dotenv({
path: path.resolve(__dirname, '..', '.env'),
safe: false
})
]
)
}