-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.site.sw.mix.js
49 lines (37 loc) · 1.75 KB
/
webpack.site.sw.mix.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
const mix = require('laravel-mix')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const stripIndent = require('common-tags').stripIndent
// npm run prod --section=site.sw
let fileSW = 'service-worker.js',
filePathSW = `public/${fileSW}`
mix.webpackConfig({
plugins: [{
apply: (compiler) => {
compiler.hooks.beforeCompile.tap('BeforeRunPlugin', (params, callback) => {
let manifestPaths = JSON.parse(fs.readFileSync(path.resolve('./public/mix-manifest.json'), 'utf8'))
fs.copyFile(`./resources/js/service-worker-vanilla.js`, filePathSW, (err) => {
if (err) throw err;
console.info('\n\n', chalk.black.bgGreen.italic(`service-worker-vanilla.js was copied to ${fileSW}`), '\n\n');
fs.readFile(path.resolve(filePathSW), 'utf8', (err, contentFileSW) => {
if (err) return console.log(chalk.white.bgRed.bold(err));
let newContentFileSW = contentFileSW.replace(/^DummyUrlsToCache$/gm, stripIndent(String.raw)`
const urlsToCache = [
'${manifestPaths['/site/js/app.js']}',
'${manifestPaths['/site/css/app.css']}',
];
`);
fs.writeFile(path.resolve(filePathSW), newContentFileSW, 'utf8', (err) => {
if (err) throw err;
});
});
});
})
}
}]
})
mix.version([`${filePathSW}`]).mergeManifest()
mix.then(webpackStats => {
console.log(chalk.black.bgGreen.bold(`Webpack build succssesfully done!`, '\n'));
})