forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
92 lines (79 loc) · 2.78 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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.
const { spawnSync } = require('child_process');
const path = require('path');
const Watchpack = require('watchpack');
const generateLocalizations = require('./resources/js/cli/generate-localizations');
const modNamesGenerator = require('./resources/js/cli/mod-names-generator');
const spawnOptions = { stdio: 'inherit' };
const modsFile = path.resolve(__dirname, 'database/mods.json');
const routesFile = path.resolve(__dirname, 'routes/web.php');
const langDir = path.resolve(__dirname, 'resources/lang');
const webpackConfig = require('./webpack.unmix.js');
let resolved = false;
const watches = [
{
callback: modNamesGenerator,
path: modsFile,
type: 'file',
},
{
callback: () => {
spawnSync('php', ['artisan', 'ziggy:generate', 'resources/builds/ziggy.js'], spawnOptions);
},
path: routesFile,
type: 'file',
},
{
callback: () => {
generateLocalizations();
// touching the file on first build might cause karma's watchers to fire after tests start.
if (resolved) {
spawnSync('touch', [path.resolve(__dirname, 'resources/js/main.coffee')], spawnOptions);
}
},
path: langDir,
type: 'dir',
},
];
function configPromise(env, argv) {
return new Promise((resolve) => {
const options = {
// fire an aggregated event after 200ms on changes.
aggregateTimeout: 200,
};
// same as webpack-cli's handling
if (argv['watch-poll'] === 'true' || argv['watch-poll'] === '') options.poll = true;
if (!argv.watch) {
watches.forEach((watched) => {
watched.callback();
});
return resolve(webpackConfig);
}
const wp = new Watchpack(options);
wp.watch(
watches.filter((x) => x.type === 'file').map((x) => x.path),
watches.filter((x) => x.type === 'dir').map((x) => x.path),
); // files and directories are different arguments.
// directory watchers cause change events on start, file watchers don't;
// run the callback for each file watcher once.
watches.filter((x) => x.type === 'file').forEach((watched) => {
watched.callback();
watched.ranOnce = true;
});
wp.on('aggregated', (changes, removals) => {
watches.forEach((watched) => {
if (changes.includes(watched.path) || removals.includes(watched.path)) {
watched.callback();
watched.ranOnce = true;
}
});
// let webpack run after the first build.
if (!resolved && watches.reduce((value, watched) => value && watched.ranOnce, true)) {
resolved = true;
resolve(webpackConfig);
}
});
});
}
module.exports = configPromise;