-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
62 lines (57 loc) · 2.14 KB
/
index.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
const mix = require('laravel-mix');
const { spawn } = require('child_process');
const { existsSync } = require('fs');
const { normalize, resolve } = require('path');
const glob = require('glob');
const hasbin = require('hasbin');
const bin = existsSync('./vendor/bin/jigsaw') ? normalize('./vendor/bin/jigsaw') : hasbin('jigsaw', (result) => {
if (result) return 'jigsaw';
console.error('Could not find Jigsaw; please install it with Composer.');
process.exit(1);
});
const env = process.env.NODE_ENV === 'development' ? 'local' : process.env.NODE_ENV;
const jigsaw = () => spawn(bin, ['build', env], { stdio: 'inherit', shell: true }).on('exit', (code) => {
if (code > 0) {
console.warn(`\nJigsaw build failed, see above.`);
}
});
// Picks up everything except new files created directly in 'source/'
const watch = ({ files, dirs, notDirs }) => (compilation, callback) => {
files.flatMap(pattern => glob.sync(pattern))
.map(file => compilation.fileDependencies.add(resolve(file)));
dirs.flatMap(pattern => glob.sync(pattern))
.filter(dir => !notDirs.includes(dir))
.map(dir => compilation.contextDependencies.add(resolve(dir)));
callback();
}
mix.extend('jigsaw', new class {
config = {
watch: {
files: [
'config.php',
'bootstrap.php',
'blade.php',
'listeners/**/*.php',
'source/*.md',
'source/*.php',
'source/*.html',
],
dirs: ['source/*/'],
notDirs: ['source/_assets/', 'source/assets/'],
},
}
register(config = {}) {
Array.isArray(config.watch)
? this.config.watch.files.push(...config.watch)
: this.config.watch = { ...this.config.watch, ...config.watch };
}
webpackPlugins() {
const watchConfig = this.config.watch;
return [new class {
apply(compiler) {
compiler.hooks.afterCompile.tapAsync('JigsawWatchPlugin', watch(watchConfig));
compiler.hooks.afterDone.tap('JigsawBuildPlugin', jigsaw);
}
}];
}
});