-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpackHelper.js
More file actions
206 lines (191 loc) · 5.32 KB
/
webpackHelper.js
File metadata and controls
206 lines (191 loc) · 5.32 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* The point of this file is to automate some of the repetitive webpack definitions, especially as it relates to prod vs dev
*/
import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import CopyWebpackPlugin from "copy-webpack-plugin";
import MiniCSSExtractPlugin from "mini-css-extract-plugin";
import webpack from "webpack";
const pageList = [
{
template: "src/index.html", // What is the source for this
chunks: ["index"], // What bundle should be loaded here?
filename: "index.html" // Where does the output go?
}
];
/**
* Return the entry points for webpack
* @param {"DEV" | "PROD"} environment
* @returns {Object}
*/
export function getEntryPoints(environment) {
return {
index: ["whatwg-fetch", ppath("src/index")]
};
}
/**
* Return the output data for webpack
* @param environment
* @returns {{path: *|string, filename: string}}
*/
export function getOutputData(environment) {
const outputPath = environment === "PROD" ? ppath("dist") : ppath("src");
return {
path: outputPath,
publicPath: "/",
filename: "bundle.[name].[chunkhash].js"
};
}
/**
* Is the app served at / or at /?
* @param environment
* @returns {string}
*/
export function getSubdirectory(environment) {
if (environment === "DEV") {
return "";
} else {
return "";
}
}
/**
*
* Return the array of CopyPlugin configurations used by webpack
* @param {"DEV" | "PROD"} environment
* @returns {Array}
*/
export function getCopyPlugins(environment) {
return [
new CopyWebpackPlugin([
// Put files here that need to be directly copied
// { from: 'src/sampleNonCrisisConfig.json', to: 'sampleNonCrisisConfig.json' },
])
];
}
/**
* Get CSS related plugins
* @param environment
* @returns {*[]}
*/
export function getCSSPlugins(environment) {
return [
new MiniCSSExtractPlugin({
filename: "[name].[contenthash].css",
chunkFilename: "[name].[contenthash].css"
})
];
}
/**
* Return the array of HTMLWebpackPlugin configurations used by webpack
* @param {"DEV" | "PROD"} environment
* @returns {Array}
*/
export function getHTMLPlugins(environment) {
const prodParams = {
inject: true,
thisEnvironmentType: "PROD", // This is a custom property available in our html via ejs
thisSubdirectory: getSubdirectory("PROD"),
buildTimestamp: new Date(),
minify: {
// Lots of options for minifying here
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
};
const devParams = {
inject: true,
thisSubdirectory: getSubdirectory("DEV"),
thisEnvironmentType: "DEV", // This is a custom property available in our html via ejs
buildTimestamp: new Date()
};
const baseParams = environment === "PROD" ? prodParams : devParams;
const pluginArray = [];
pageList.forEach(page => {
const pageConfig = Object.assign({}, page, baseParams); // Combine page data and baseParams
if (Object.keys(pageConfig)) {
const plugin = new HtmlWebpackPlugin(pageConfig);
pluginArray.push(plugin);
}
});
return pluginArray;
}
/**
* Return all the plugins for webpack
* @param environment
*/
export function getPlugins(environment) {
const plugins = [
...getCopyPlugins(environment),
...getHTMLPlugins(environment),
...getCSSPlugins(environment)
];
return plugins;
}
/**
* Return all the rules for webpack
* @param environment
*/
export function getRules(environment) {
return [
{
test: /\.js$/,
use: ["babel-loader"]
},
{
test: /\.s?css$/,
use: [
{
loader: MiniCSSExtractPlugin.loader
},
{
loader: "css-loader",
options: {
sourceMap: true,
importLoaders: 1
}
},
{
loader: "postcss-loader",
options: {
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
]
},
{
test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
use: [
{
loader: "file-loader",
options: {
name: "[name].[ext]",
outputPath: "fonts/" // where the fonts will go
// publicPath: '../' // override the default path
}
}
]
}
];
}
/**
* Retrun the absolute path
* @param location
* @returns {*|string}
*/
function ppath(location) {
return path.resolve(__dirname, location);
}