-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.js
179 lines (166 loc) · 5.66 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* eslint-disable */
const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
// Nodes fs module to read directory contents
const fs = require("fs");
// Function to walk through files and directories at a given path
function walkDir(rootDir) {
const paths = [];
// A recursive function
// - If a path is a file it will add to an array to be returned
// - If a path is a directory it will call itself on the directory
function walk(directory, parent) {
const dirPath = path.resolve(__dirname, directory);
const templateFiles = fs.readdirSync(dirPath);
// Check each path found, add files to array and call self on directories found
templateFiles.forEach(file => {
const filepath = path.resolve(__dirname, directory, file);
const isDirectory = fs.lstatSync(filepath).isDirectory();
if (isDirectory) {
// File is a directory
const subDirectory = path.join(directory, file);
if (subDirectory == "views/layout") {
return;
}
if (parent) {
// Join parent/file before passing so we have correct path
const parentPath = path.join(parent, file);
walk(subDirectory, parentPath);
} else {
walk(subDirectory, file);
}
} else if (parent) {
// Parent exists, join it with file to create the pat¨h
const fileWithParent = path.join(parent, file);
paths.push(fileWithParent);
} else {
paths.push(file);
}
});
}
// Start our function, it will call itself until there no paths left
walk(rootDir);
return paths;
}
// Generates html plugins
function generateHtmlPlugins(templateDir) {
// Read files in template directory
const templateFiles = walkDir(templateDir);
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}`
),
inject: false
});
});
}
const htmlPlugins = generateHtmlPlugins("./views");
module.exports = {
entry: {
"sunburst/sunburst": "./sunburst/sunburst.js",
"getdata/getdata": "./getdata/getdata.js"
},
output: {
path: path.join(__dirname, "dist"),
publicPath: "/",
filename: "[name].js"
},
target: "web",
devtool: "inline-source-map",
devServer: {
contentBase: path.resolve(__dirname, "./dist"),
watchContentBase: true,
compress: true,
port: 8080,
overlay: true
},
optimization: {
minimizer: [new TerserPlugin({}), new OptimizeCSSAssetsPlugin({})]
},
module: {
rules: [
{
// Loads the javacript into html template provided.
// Entry point is set below in HtmlWebPackPlugin in Plugins
test: /\.html$/,
use: [
{
loader: "html-loader",
options: { minimize: true }
}
]
},
{
// Loads images into CSS and Javascript files
test: /\.jpg$/,
use: [{ loader: "url-loader" }]
},
{
// Loads CSS into a file when you import it via Javascript
// Rules are set in MiniCssExtractPlugin
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"]
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: "css-loader",
options: {
sourceMap: true
}
},
{
loader: "sass-loader" // compiles SASS to CSS
}
]
},
{
test: /\.pug$/,
use: ["pug-loader"]
},
{
test: /\.(woff2?|eot|ttf|otf|svg)(\?.*)?$/,
loader: "url-loader"
},
{
test: /\.(csv|tsv)$/,
use: ["csv-loader"]
},
{
test: /\.xml$/,
use: ["xml-loader"]
}
]
},
plugins: [
new CleanWebpackPlugin(),
new CopyPlugin([
{ from: "sunburst/settings", to: "sunburst" },
{ from: "sunburst/data", to: "sunburst/data" },
{ from: "getdata/settings", to: "getdata" },
{ from: "getdata/data", to: "getdata/data" }
]),
new MiniCssExtractPlugin({
filename: "[name].css"
})
]
// Join htmlPlugin array to the end of webpack plugins array.
.concat(htmlPlugins)
};