forked from the-events-calendar/event-tickets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
210 lines (186 loc) · 7.49 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
/**
* External dependencies
*/
const { resolve } = require('path');
const { reduce, zipObject } = require('lodash');
const merge = require('webpack-merge');
const common = require('@the-events-calendar/product-taskmaster/webpack/common/webpack.config');
const {
getDirectoryNames,
getDirectories,
} = require('@the-events-calendar/product-taskmaster/webpack/utils/directories');
const {
getJSFileNames,
getJSFiles,
} = require('@the-events-calendar/product-taskmaster/webpack/utils/files');
// Do we need to expose this as a variable?
const PLUGIN_SCOPE = 'tickets';
//
// ────────────────────────────────────────────────────────────────────────────────────── I ──────────
// :::::: G E N E R A T E E V E N T S P L U G I N : : : : : : : :
// ──────────────────────────────────────────────────────────────────────────────────────────────
//
const isProduction = process.env.NODE_ENV === 'production';
const postfix = isProduction ? 'min.css' : 'css';
// The targets we would like to compile.
// The `moveFromTo` property is used to move the files in place after the build completed using the
// `MoveTargetsInPlace` plugin; see below.
const targets = [
{
name: 'main',
entry: './src/modules/index.js',
outputScript: './src/resources/js/app/main.min.js',
outputStyle: `src/resources/css/app/[name].${postfix}`,
},
{
name: 'tickets-editor',
entry: './src/Tickets/Blocks/Tickets/app/editor/index.js',
outputScript: './build/Tickets/Blocks/Tickets/editor.min.js',
outputStyle: `build/Tickets/Blocks/Tickets/editor.${postfix}`,
moveFromTo: {
'src/resources/js/app/tickets-editor.js':
'build/Tickets/Blocks/Tickets/editor.js',
'src/resources/css/app/tickets-editor.css':
'build/Tickets/Blocks/Tickets/editor.css',
},
},
{
name: 'ticket-editor',
entry: './src/Tickets/Blocks/Ticket/app/editor/index.js',
outputScript: './build/Tickets/Blocks/Ticket/editor.min.js',
outputStyle: `build/Tickets/Blocks/Ticket/editor.${postfix}`,
moveFromTo: {
'src/resources/js/app/ticket-editor.js':
'build/Tickets/Blocks/Ticket/editor.js',
'src/resources/css/app/ticket-editor.css':
'build/Tickets/Blocks/Ticket/editor.css',
},
},
{
name: 'flexible-tickets-block-editor',
entry: './src/Tickets/Blocks/app/flexible-tickets/block-editor/index.js',
outputScript: './build/FlexibleTickets/block-editor.min.js',
outputStyle: `build/FlexibleTickets/block-editor.${postfix}`,
moveFromTo: {
'src/resources/js/app/flexible-tickets-block-editor.js':
'build/FlexibleTickets/block-editor.js',
'src/resources/css/app/flexible-tickets-block-editor.css':
'build/FlexibleTickets/block-editor.css',
},
},
{
name: 'flexible-tickets-classic-editor',
entry: './src/Tickets/Blocks/app/flexible-tickets/classic-editor/index.js',
outputScript: './build/FlexibleTickets/classic-editor.min.js',
outputStyle: `build/FlexibleTickets/classic-editor.${postfix}`,
moveFromTo: {
'src/resources/js/app/flexible-tickets-classic-editor.js':
'build/FlexibleTickets/classic-editor.js',
'src/resources/css/app/flexible-tickets-classic-editor.css':
'build/FlexibleTickets/classic-editor.css',
},
},
];
// A function cannot be spread directly, we need this temporary variable.
const targetEntries = reduce(
targets,
(carry, target) => ({
...carry,
[target.name]: resolve(__dirname, target.entry),
}),
{}
);
// Configure multiple entry points.
const config = merge(common, {
entry: targetEntries,
});
// WebPack 4 does support multiple entry and output points, but the plugins used by the build do not.
// For this reason we're setting the output target to a string template.
// The files will be moved to the correct location after the build completed, by the `MoveTargetsInPlace` plugin.
// See below.
config.output = {
path: __dirname,
filename: './src/resources/js/app/[name].min.js',
};
// Define, build and add to the stack of plugins a plugin that will move the files in place after they are built.
const fs = require('node:fs');
const normalize = require('path').normalize;
class MoveTargetsInPlace {
constructor(moveTargets) {
// Add, to each move target, the minified version of the file.
Object.keys(moveTargets).forEach((file) => {
const minFile = file.replace(/\.(js|css)/g, '.min.$1');
moveTargets[minFile] = moveTargets[file].replace(
/\.(js|css)/i,
'.min.$1'
);
});
this.moveTargetsObject = moveTargets;
this.sourceFiles = Object.keys(moveTargets).map((file) =>
normalize(file)
);
this.moveFile = this.moveFile.bind(this);
}
moveFile(file) {
const normalizedFile = normalize(file);
if (this.sourceFiles.indexOf(normalizedFile) === -1) {
return;
}
const destination = this.moveTargetsObject[normalizedFile];
console.log(`Moving ${normalizedFile} to ${destination}...`);
// Recursively create the directory for the target.
fs.mkdirSync(destination.replace(/\/[^/]+$/, ''), { recursive: true });
// Move the target.
fs.renameSync(normalizedFile, destination);
}
apply(compiler) {
// compiler.hooks.done.tap ( 'MoveTargetsIntoPlace', this.moveTargets );
compiler.hooks.assetEmitted.tap('MoveTargetsIntoPlace', this.moveFile);
}
}
const moveTargets = targets.reduce((carry, target) => {
return {
...carry,
...target.moveFromTo,
};
}, {});
config.plugins.push(new MoveTargetsInPlace(moveTargets));
// If COMPILE_SOURCE_MAPS env var is set, then set devtool=eval-source-map
if (process.env.COMPILE_SOURCE_MAPS) {
config.devtool = 'eval-source-map';
}
//
// ──────────────────────────────────────────────────────────────────────────────────────────── II ──────────
// :::::: G E N E R A T E S T Y L E S F R O M V I E W S : : : : : : : :
// ──────────────────────────────────────────────────────────────────────────────────────────────────────
//
const stylePath = resolve(__dirname, './src/styles');
const styleDirectories = getDirectories(stylePath);
const styleDirectoryNames = getDirectoryNames(stylePath);
const styleEntries = zipObject(styleDirectoryNames, styleDirectories);
const removeExtension = (str) => str.slice(0, str.lastIndexOf('.'));
const entries = reduce(
styleEntries,
(result, dirPath, dirName) => {
const jsFiles = getJSFiles(dirPath);
const jsFileNames = getJSFileNames(dirPath);
const entryNames = jsFileNames.map(
(filename) => `${dirName}/${removeExtension(filename)}`
);
return {
...result,
...zipObject(entryNames, jsFiles),
};
},
{}
);
const styleConfig = merge(common, {
entry: entries,
output: {
path: __dirname,
},
});
//
// ─── EXPORT CONFIGS ─────────────────────────────────────────────────────────────
//
module.exports = [config, styleConfig];