-
Notifications
You must be signed in to change notification settings - Fork 18
/
webpack.config.js
executable file
·222 lines (191 loc) · 5.2 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/** Package information */
import pck from './package.json' assert {
type: 'json'
};
/** Build helpers */
import build from './build.helpers.js';
/** Webpack plugins */
import HtmlWebpackPlugin from 'html-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
/** Developer imports */
import webpack from 'webpack';
import moment from 'moment';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
/** Directory name and file name constants */
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Read build.options.js if it exists
*/
let buildOptions = build.readJson('./build.options.json');
/**
* Set asset output directory (containing .js and .css files etc.)
*
* (!) Can be set in `build.options.json`
*/
let assetDir = buildOptions && buildOptions.assetDir ? buildOptions.assetDir : 'indexer';
/** Trim any leading and trailing slash as well as double slashes */
assetDir = assetDir.replace(/([^:]\/)\/+/g, '$1').replace(/^\/|\/$/g, '');
/**
* Parameters passed with `HtmlWebpackPlugin`
*/
let templateParameters = {
buildInject: {},
additonalCss: [],
version: pck.version,
indexerPath : `/${assetDir}/`
};
if(buildOptions)
{
console.info('Build options:', JSON.stringify(buildOptions, null, 2), '\n');
} else {
console.info('No build options was found - skipping any potential build options during the build.\n');
}
/**
* Handle any extra build features
*/
if(buildOptions.extraFeatures)
{
Object.keys(buildOptions.extraFeatures).forEach((key) =>
{
/** Is this feature enabled? */
if(buildOptions.extraFeatures[key] === true)
{
/** Construct path, read the feature's JSON data */
let path = `${buildOptions.extrasDir}/${key}`, data = build.readJson(`${path}/data.json`);
if(data && data.integral && Object.keys(data.integral).length > 0)
{
let integrals = data.integral;
/** Iterate over the feature's parts/snippets, read it and so on */
Object.keys(integrals).forEach((part) =>
{
let relativePath = build.trimPartPath(integrals[part].path);
let extractor = integrals[part].extractor;
let partType = integrals[part].type || 'buildInject';
let fullPath = `${path}/${relativePath}`;
let options = typeof integrals[part].options === 'object' &&
integrals[part].options !== null ? integrals[part].options : {};
if(build.extractors.hasOwnProperty(extractor))
{
let integral = build.extractors[extractor](fullPath, options);
/** If reading was successful - set injection values */
if(integral)
{
if(partType === 'additonalCss')
{
templateParameters[partType].push(integral);
} else {
if(!templateParameters[partType].hasOwnProperty(key))
{
templateParameters[partType][key] = {};
}
templateParameters[partType][key][part] = integral;
}
}
} else {
build.exit('Extractor ', `'${extractor}'`, 'was not found.');
}
});
}
}
});
}
const banner = () =>
{
return `
@preserved
## IVFi-PHP - ${pck.description} ##
[Version: ${pck.version}]
[Git: https://github.com/sixem/ivfi-php]
[Build: [fullhash] @ ${moment().format('dddd, MMMM Do YYYY')}]
[Chunkhash: [chunkhash]]
Copyright (c) 2022 emy | five.sh | github.com/sixem
Licensed under GPL-3.0
\n`;
};
const config = (env, argv) => {
const isProduction = argv.mode === 'production';
return {
context: __dirname,
mode: isProduction ? 'production' : 'development',
entry: {
index: './src/core/main.ts'
},
resolve: {
extensions: ['.js', '.ts', '.json']
},
output: {
filename: 'main.js',
path: __dirname + `/build/${assetDir}`
},
optimization: {
minimize: isProduction ? true : false,
minimizer: [
new TerserPlugin({
extractComments: false
}),
new CssMinimizerPlugin()
]
},
plugins: [
new HtmlWebpackPlugin({
inject: false,
minify: false,
template: __dirname + '/src/php/template.php',
filename: __dirname + '/build/indexer.php',
templateParameters: () => {
return templateParameters;
}
}),
new MiniCssExtractPlugin({
filename: `./css/style.css`
}),
new webpack.BannerPlugin({
banner: banner(),
entryOnly: true
}),
],
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader'
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isProduction ? false : true
}
},
{
loader: 'sass-loader'
},
{
loader: 'postcss-loader'
}
],
},
{
test: /\.(woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: [
{
loader: 'file-loader',
options: {
name: 'assets/fonts/[name].[ext]',
publicPath: `/${assetDir}`
}
}
]
}
]
}
}
};
export default config;