-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
114 lines (109 loc) · 3.16 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
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const glob = require("glob");
// Our function that generates our html plugins
function generateHtmlPlugins (templateDir) {
// Read files in /html directory
const templateFiles = fs
.readdirSync(path.resolve(__dirname, templateDir))
.filter(function(file){ //ignore folder
return file.indexOf(".html") > -1
})
return templateFiles.map(item => {
// Split names and extension
const parts = item.split('.')
const name = parts[0]
const extension = parts[1]
// Create new HTMLWebpackPlugin with options
return new HtmlWebPackPlugin({
filename: `${name}.html`,
template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`)
})
})
}
const htmlPlugins = generateHtmlPlugins('./src/html')
let jsEntryArray = glob.sync('./src/modules/**/global.js') // Returns Array of files
module.exports = env => {
console.log('environment:' + env.NODE_ENV)
return {
entry: ['./src/index.js'].concat(jsEntryArray),
output: {
path: path.resolve(__dirname, 'dist'), // Output folder
filename: 'js/main.js' // JS output path
},
devServer: {
contentBase: './dist',
hot: true
},
resolve: {
alias: {
NodeModules: path.resolve(__dirname, 'node_modules/')
}
},
module: {
rules: [
{
test: /\.html$/,
use: [{
loader: "html-loader",
options: {
minimize: false,
interpolate: true // allow html snippets with commonJs require tags
}
}]
},
{
test: /\.scss$/,
use: [
(env.NODE_ENV === 'development') ? "style-loader" : MiniCssExtractPlugin.loader,
"css-loader",
"postcss-loader",
"sass-loader",
"import-glob-loader"
]
},
{ // Process javascript
test: /\.js|jsx$/,
exclude: /node_modules/,
use: [
"babel-loader",
"eslint-loader"
]
},
{ // Handle images
test: /\.(png|svg|jpg|gif)$/,
use: {
loader: 'file-loader',
options: {
name: "[name].[ext]",
outputPath: 'mysource_files/'
}
}
},
{ //handle fonts
test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: './mysource_files/[name].[ext]'
}
}]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css",
}),
// # conditionally load CleanWebpackPlugin
... (env.NODE_ENV !== 'development') ? [new CleanWebpackPlugin(['dist'],{})] : []
]
.concat(htmlPlugins)
}
};