-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpostBuildScript.js
More file actions
42 lines (39 loc) · 1.42 KB
/
postBuildScript.js
File metadata and controls
42 lines (39 loc) · 1.42 KB
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
const path = require('path');
const fs = require('fs');
const publicPath = path.resolve(__dirname, './public');
const buildPath = path.resolve(__dirname, './build');
const swPath = path.resolve(publicPath, './serviceWorker.js');
const swInBuildPath = path.resolve(buildPath, './serviceWorker.js');
// Removing service worker created by create-react-app
// and its precache-maniffest since we have our own
// service worker...
const filesInBuild = fs.readdirSync(buildPath);
filesInBuild.forEach(file => {
if (
file.indexOf('precache-manifest.') === 0 ||
file.indexOf('service-worker.js') === 0
) {
fs.unlinkSync(path.resolve(buildPath, file));
}
});
console.log('Removed unnecessary files.');
// Changing this.version in the serviceWorker.js
let contents = fs.readFileSync(swPath, 'utf-8');
let version = contents.match(
/this.version\s*=\s*([\d|\.]*)/
)[1] / 1;
version = Math.round(version * 100 + 1) / 100;
contents = contents.replace(
/this.version\s*=\s*[\d|\.]*/,
`this.version = ${version}`
);
fs.writeFileSync(swPath, contents, 'utf-8');
// Changing this.production in build/serviceWorker.js
contents = contents.replace(
/this.production\s*=\s*false/,
'this.production = true'
);
fs.writeFileSync(swInBuildPath, contents, 'utf-8');
console.log('Changed this.version in serviceWorker.js');
console.log(`Current this.version is ${version}.`);
console.log('Changed this.production to true in serviceWorker.js');