forked from dahnielson/parcel-plugin-workbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (27 loc) · 916 Bytes
/
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
const logger = require("parcel-bundler/src/Logger");
const { workboxConfig: getWorkboxConfig, chars } = require("./constants");
const getServiceWorker = require("./serviceWorker");
module.exports = bundler => {
const { outDir, minify } = bundler.options;
let pkg = {};
let workboxConfig = {};
bundler.on("bundled", () => {
// get package.json
if (
bundler.mainAsset &&
bundler.mainAsset.package &&
bundler.mainAsset.package.pkgfile
) {
// for parcel-bundler version@<1.8
pkg = require(bundler.mainAsset.package.pkgfile);
} else if (bundler.mainBundle) {
pkg = bundler.mainBundle.entryAsset.package;
} else {
logger.warn(chars.error, "mainAsset/mainBundle not available!");
return;
}
workboxConfig = getWorkboxConfig(outDir, pkg);
logger.status(chars.workbox, "Workbox");
getServiceWorker(workboxConfig, minify);
});
};