Skip to content

Commit

Permalink
Several modifications
Browse files Browse the repository at this point in the history
- Get the service worker file from entry asset's dir
- Upgrade to use 5.1.2 workbox, using importScripts
  • Loading branch information
nagyv committed Apr 15, 2020
1 parent 5849347 commit ac3b244
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "parcel-plugin-workbox-precache",
"version": "0.0.6",
"version": "0.1.6",
"main": "src/index.js",
"author": "Jos van Bakel <jos.van.bakel@wearespindle.com>",
"contributors": [
"Jos van Bakel <jos.van.bakel@wearespindle.com>"
"Jos van Bakel <jos.van.bakel@wearespindle.com>",
"Viktor Nagy <viktor.nagy@gmail.com>"
],
"license": "MIT",
"repository": "https://github.com/wearespindle/parcel-plugin-workbox-precache",
"peerDependencies": {
"parcel-bundler": "^1.12.0",
"workbox-build": "^4.1.0"
"parcel-bundler": "^1.12.0"
}
}
14 changes: 7 additions & 7 deletions src/helpers/buildServiceWorker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const workbox = require('workbox-build');

const computeBundleHashes = require('./computeBundleHashes.js');
const injectScript = require('./injectScript.js');
Expand All @@ -11,8 +10,10 @@ const writeFile = promisify(fs.writeFile);

module.exports = async function upgradeServiceWorker(bundle, swFilename, precacheFilter) {
const outDir = path.resolve(bundle.entryAsset.options.outDir);
const swFile = path.join(outDir, swFilename);
if (!fs.existsSync(swFile)) {
const baseDir = path.resolve(path.dirname(bundle.entryAsset.name));
const inFile = path.join(baseDir, swFilename);
const outFile = path.join(outDir, swFilename);
if (!fs.existsSync(inFile)) {
console.log(`Skipping workbox precache because ${swFilename} is not found.`);
return;
}
Expand All @@ -28,11 +29,10 @@ module.exports = async function upgradeServiceWorker(bundle, swFilename, precach
console.log(`Copied workbox libraries to ${workboxLibDir}`);

const script = `
importScripts('${workboxLibDir}/workbox-sw.js');
workbox.setConfig({modulePathPrefix: '/${workboxLibDir}/'});
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const precacheManifest = ${JSON.stringify(precacheManifest, null, 2)};
`;

const contents = await readFile(swFile, { encoding: 'utf8' });
await writeFile(swFile, injectScript(contents, script));
const contents = await readFile(inFile, { encoding: 'utf8' });
await writeFile(outFile, injectScript(contents, script));
};

0 comments on commit ac3b244

Please sign in to comment.