forked from joomla-extensions/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
378 lines (313 loc) · 10.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* webpack.config.js
*
* @license License GNU General Public License version 2 or later; see LICENSE.txt
* @author Andrea Gentil - Anibal Sanchez <[email protected]>
* @copyright (c)2012-2023 Extly, CB. All rights reserved.
*
*/
// Array of Webpack plugins
let buildPlugins = [];
// Extension directories to be visited
const packageTypeDir = 'package';
const extensionTypesDirs = [
'component',
'modules',
'plugins',
'file',
'template',
'library',
'platform',
];
// Only template xml are processed, other xml config files are ignored
const packageIgnoreFiles = ['config.xml'];
// WARNING - Clean these development folders before building
const globalCleanDevAssets = [
/\/node_modules\//,
];
// Required Webpack plugins
const CopyWebpackPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const fs = require('fs');
const fsExtra = require('fs-extra');
const moment = require('moment');
const path = require('path');
const readDirRecursive = require('fs-readdir-recursive');
const ZipFilesPlugin = require('webpack-zip-files-plugin');
const glob = require("glob");
const touch = require("touch")
let definitions;
const releaseDate = moment()
.format('YYYY-MM-DD');
const year = moment()
.format('YYYY');
const releaseDir = 'build/release';
const releaseDirAbs = path.resolve(__dirname, releaseDir);
const templatesDir = 'build/templates';
const translationsDir = 'build/translations';
const packageDirAbs = path.resolve(__dirname, packageTypeDir);
const renderDirectories = [templatesDir, translationsDir];
const allExtensionTypesDirs = extensionTypesDirs.concat([packageTypeDir]);
const tagTransformation = (content) => content
.toString()
.replace(/\[MANIFEST_COPYRIGHT\]/g, definitions.MANIFEST_COPYRIGHT)
.replace(/; \[TRANSLATION_COPYRIGHT\]/g, definitions.TRANSLATION_COPYRIGHT)
.replace('// [PHP_COPYRIGHT]', definitions.PHP_COPYRIGHT)
.replace('/* [CSS_COPYRIGHT] */', definitions.CSS_COPYRIGHT)
.replace('// [JS_COPYRIGHT]', definitions.JS_COPYRIGHT)
.replace(/\[COPYRIGHT\]/g, definitions.COPYRIGHT)
.replace(/\[AUTHOR_EMAIL\]/g, definitions.AUTHOR_EMAIL)
.replace(/\[AUTHOR_URL\]/g, definitions.AUTHOR_URL)
.replace(/\[AUTHOR\]/g, definitions.AUTHOR)
.replace(/\[EXTENSION_CDN\]/g, definitions.EXTENSION_CDN)
.replace(/\[EXTENSION_CLASS_NAME\]/g, definitions.EXTENSION_CLASS_NAME)
.replace(/\[EXTENSION_ALIAS\]/g, definitions.EXTENSION_ALIAS)
.replace(/\[EXTENSION_DESC\]/g, definitions.EXTENSION_DESC)
.replace(/\[EXTENSION_NAME\]/g, definitions.EXTENSION_NAME)
.replace(/\[LICENSE_CODE\]/g, definitions.LICENSE_CODE)
.replace(/\[LICENSE\]/g, definitions.LICENSE)
.replace(/\[RELEASE_VERSION\]/g, definitions.RELEASE_VERSION)
.replace(/\[TRANSLATION_KEY\]/g, definitions.TRANSLATION_KEY)
.replace(/\[DATE\]/g, releaseDate)
.replace(/\[YEAR\]/g, year);
function loadEnvironmentDefinitions() {
const defs = {};
const env = new Dotenv();
Object.keys(env.definitions)
.forEach((definition) => {
const key = definition.replace('process.env.', '');
let value = env.definitions[definition];
value = value.replace(/^"(.+(?="$))"$/, '$1');
value = value.replace(/%CR%/g, '\n');
value = value.replace(/%TAB%/g, '\t');
defs[key] = value;
});
return defs;
}
function cleanDevAssets() {
const cleanDevAssetsDirs = allExtensionTypesDirs.map(
// Read all files
(extensionTypesDir) => glob.sync(
path.resolve(__dirname, extensionTypesDir) + '/**/*', {
dot: true
}
)
)
// One flat array
.flat()
// Filter to files that match the globalCleanDevAssets to clean
.filter((item) => {
return globalCleanDevAssets.find((globalCleanDevFolder) => {
return globalCleanDevFolder.test(item);
});
});
cleanDevAssetsDirs.map((file) => fsExtra.removeSync(file));
}
function removeReleaseDirectory() {
return new FileManagerPlugin({
onStart: {
delete: [
releaseDirAbs,
],
mkdir: [
releaseDirAbs,
],
}
});
}
function discoverFilesToRender(tplDirectory, extensionType) {
const tplPath = `${tplDirectory}/${extensionType}/`;
const absTplPath = `${__dirname}/${tplPath}`;
return glob.sync(
path.resolve(__dirname, `${tplPath}**/*.@(ini|xml|php|css|js)`),
)
.map(
(file) => file.replace(absTplPath, '')
);
}
function discoverManifestTemplates(tplDirectory, extensionType) {
const tplPath = `${tplDirectory}/${extensionType}/`;
const absTplPath = `${__dirname}/${tplPath}`;
return glob.sync(
path.resolve(__dirname, `${tplPath}**/*.xml`),
)
.map(
(file) => file.replace(absTplPath, '')
);
}
function resolveExtensionTemplate(tplDirectory, extensionType) {
return path.resolve(
__dirname,
`${tplDirectory}/${extensionType}`,
);
}
function renderTemplates() {
const renderTpls = [];
// For templates and translation directories
renderDirectories.forEach((tplDirectory) => {
// For all extension types, including the package
allExtensionTypesDirs.forEach((extensionType) => {
const extTplDir = resolveExtensionTemplate(tplDirectory, extensionType);
const templates = discoverFilesToRender(tplDirectory, extensionType);
// const templatesDeprecated = discoverTemplateDeprecated(tplDirectory, extensionType);
// For each template
templates.forEach((file) => {
const dest = path.resolve(__dirname, `${extensionType}/${file}`);
const item = {
context: extTplDir,
from: file,
to: dest,
transform: tagTransformation,
};
// Render each template
renderTpls.push(item);
});
});
});
return new CopyWebpackPlugin(renderTpls);
}
function isPackageType() {
let packageMode = false;
try {
packageMode = fs.lstatSync(packageDirAbs)
.isDirectory();
} catch (e) {
console.log('Package definition not detected.');
}
return packageMode;
}
function declarePackageGeneration() {
const packageDirAbsBar = `${packageDirAbs}/`;
// Include all files from the package directory
const pkgFiles = glob.sync(`${packageDirAbsBar}**/*`)
.map(
(file) => file.replace(packageDirAbsBar, '')
);
// Discover extensions to be included
const pkgEntries = pkgFiles.map((file) => {
const packageFile = path.resolve(packageDirAbs, file);
return {
src: packageFile,
};
});
const folders = new Map();
// Add all extension types directories into the package
extensionTypesDirs.forEach((extensionTypeDir) => {
const extTemplates = discoverManifestTemplates(templatesDir, extensionTypeDir);
extTemplates.forEach((extTemplate) => {
// Exclude discovered files that are not xml
if (!extTemplate.endsWith('.xml')) {
return null;
}
const extTemplateFile = path.parse(extTemplate)
.base;
// It is already included, continue with the rest of the files
if (packageIgnoreFiles.includes(extTemplateFile)) {
return;
}
// Prepare the folder to be included
const srcFile = path.resolve(
__dirname,
`${extensionTypeDir}/${extTemplate}`,
);
const srcDir = path.dirname(srcFile);
const distKey = path.basename(srcDir);
const item = {
src: srcDir,
dist: distKey,
};
// Ignore if it has already been included
if (!folders.has(srcDir)) {
folders.set(srcDir, item);
}
});
});
// All discovered folders to be included
pkgEntries.push(...folders.values());
// Complete the definition of the zip file
const outputFile = path.resolve(
__dirname,
`${releaseDir}/pkg_${definitions.EXTENSION_ALIAS}_v${definitions.RELEASE_VERSION}`,
);
const zipFile = {
entries: pkgEntries,
output: outputFile,
format: 'zip',
};
return new ZipFilesPlugin(zipFile);
}
function declareZipsGeneration() {
const zipDirectories = [templatesDir];
const zipPlugins = [];
// For each templates directory to be zipped
zipDirectories.forEach((tplDirectory) => {
// For all extension types
extensionTypesDirs.forEach((extensionType) => {
const extZipDir = resolveExtensionTemplate(tplDirectory, extensionType);
const templates = discoverFilesToRender(tplDirectory, extensionType);
// For each template
templates.forEach((tplFile) => {
const srcFile = path.resolve(__dirname, `${extensionType}/${tplFile}`);
const srcDir = path.dirname(srcFile);
const extname = path.extname(srcFile);
if (extname !== '.xml') return;
const manifestTplFile = `${extZipDir}/${tplFile}`;
const extensionTplDir = path.dirname(manifestTplFile);
const parts = extensionTplDir.split('/');
const extElement = parts.pop();
let renamedExtElement = extElement;
// The component must be renamed to the extension alias
if (renamedExtElement === 'component') {
renamedExtElement = definitions.EXTENSION_ALIAS;
// The file extension goes to the cli
} else if (renamedExtElement === 'file') {
renamedExtElement = 'cli';
}
const outputFile = path.resolve(
__dirname,
`${releaseDir}/${renamedExtElement}_v${definitions.RELEASE_VERSION}`,
);
const zipFile = {
entries: [{
src: srcDir,
dist: extElement,
}],
output: outputFile,
format: 'zip',
};
// Define the zip
const itemZip = new ZipFilesPlugin(zipFile);
zipPlugins.push(itemZip);
});
});
});
return zipPlugins;
}
// Let's build something
// Ensure that there is a .gitkeep, the webpack runs "packing" .gitkeep
touch('.gitkeep');
// Global constant definitions (.env)
definitions = loadEnvironmentDefinitions();
// Clean the development assets before packing
cleanDevAssets();
// Start clean
buildPlugins.push(removeReleaseDirectory());
// Render the manifests and translations
buildPlugins.push(renderTemplates());
if (isPackageType()) {
// Define the package generation
buildPlugins.push(declarePackageGeneration());
} else {
// Just define the zips with everything
buildPlugins = buildPlugins.concat(declareZipsGeneration());
}
// We are ready, Webpack generate!
module.exports = {
entry: './.gitkeep',
output: {
filename: '.gitkeep',
path: path.resolve(__dirname, releaseDir),
},
plugins: buildPlugins,
};