-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcopy-to-dist.js
94 lines (87 loc) · 2.66 KB
/
copy-to-dist.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
// Because broccoli is limited as fuck we need to handle this assets copying separatelly
// This script must be run at the end of the CI pipeline, or it will fail
const fs = require("fs");
const path = require("path");
const manifest = require("./_dist/manifest.json");
const { execSync } = require("child_process");
const { log } = require("util");
const distFolder = path.join(__dirname, "dist");
const editionsFolder = path.join(__dirname, "editions");
const targets = fs
.readdirSync(editionsFolder)
// exclude hidden files and the editions that do not have a subfolder
.filter((file) => !file.startsWith(".") && !/base|offline/.test(file))
.filter((file) => {
const filePath = path.join(editionsFolder, file);
log(filePath);
return fs.statSync(filePath).isDirectory;
})
.map((name) => path.join(distFolder, name));
/**
* For each edition, copy a manifest file to it's dest directory.
* This should (I'm not sure) allow them to operate as separate applications
*/
const copyManifests = () => {
targets.forEach((target) => {
const url = `/${path.basename(target)}`;
// copy each file of the edition
log("meta files for", url);
fs.writeFileSync(
path.join(target, "manifest.json"),
JSON.stringify({ ...manifest, start_url: url, scope: url }, null, 2),
"utf8"
);
// execSync(`cp _dist/sw.js ${target}`);
});
};
/*
Service worker stuff
*/
const workboxBuild = require("workbox-build");
// NOTE: This should be run *AFTER* all your assets are built
const buildSW = () => {
// This will return a Promise
return workboxBuild.generateSW({
globDirectory: "dist",
globPatterns: ["**/*.{html,json,js,css}"],
globIgnores: ["offline.html", "recipes/**"],
maximumFileSizeToCacheInBytes: 3000000,
swDest: "dist/sw.js",
// Define runtime caching rules.
runtimeCaching: [
{
// Match any image request
urlPattern: /\.(?:png|jpg|jpeg|svg|ico|gif)$/,
// Apply a cache-first strategy.
handler: "CacheFirst",
options: {
// Use a custom cache name.
cacheName: "images",
// Only cache 10 images.
expiration: {
maxEntries: 10,
},
},
},
{
// some images do not have extension
urlPattern: /\/images\/\w+/,
handler: "CacheFirst",
options: {
cacheName: "images",
expiration: {
maxEntries: 10,
},
},
},
],
});
};
/**
* EXECUTE THE TASKS
*/
copyManifests();
// Copy the rest of the files for the main site
// including manifest and service-worker
execSync(`cp -pr _dist/* dist/`);
buildSW().then(console.log);