parcel-runtime-precache-manifest
Version:
A Parcel v2 runtime that helps generate a manifest for precaching assets
44 lines (43 loc) • 2.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugin_1 = require("@parcel/plugin");
exports.default = new plugin_1.Runtime({
apply({ bundle: { target: { publicUrl }, env: { context } }, bundleGraph }) {
return __awaiter(this, void 0, void 0, function* () {
if (context != 'service-worker')
return;
const manifest = [];
if (!publicUrl.endsWith('/'))
publicUrl += '/';
for (const bundle of bundleGraph.getBundles()) {
let url = bundle.name;
if (bundle.name.endsWith('index.html')) {
// These should never have the `index.html` at the end
url = url.slice(0, -10);
}
if (bundle.env.context !== 'service-worker' &&
!bundle.isInline // ignore bundles which are not outputted as separate files
) {
manifest.push({
url: publicUrl + url,
revision: bundle.hashReference
});
}
}
return {
filePath: 'precache-manifest.js',
code: `self.__precacheManifest=${JSON.stringify(manifest)}`,
isEntry: true
};
});
}
});