parcel-plugin-precaching-sw
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -35,2 +35,15 @@ /* eslint-disable no-console */ | ||
build(); | ||
const build2 = async () => { | ||
const bundler = new Parcel(['app.css', 'app.js'], clientConfig); | ||
bundler.bundle(); | ||
// const b = await bundler.bundle(); | ||
// printSummary([b]); | ||
}; | ||
const arg = process.argv[2]; | ||
if (arg === '1' || typeof arg === 'undefined') { | ||
build(); | ||
} else if (arg === '2') { | ||
build2(); | ||
} |
@@ -41,4 +41,12 @@ { | ||
] | ||
}, | ||
"app.css": { | ||
"fileName": "css-sw.js", | ||
"allowed": [ | ||
"css", | ||
"jpeg", | ||
"map" | ||
] | ||
} | ||
} | ||
} |
124
index.js
/* eslint-disable no-console */ | ||
const { readFileSync, writeFileSync, unlinkSync, existsSync } = require('fs'); | ||
const { | ||
readFileSync, writeFileSync, unlinkSync, existsSync, | ||
} = require('fs'); | ||
const path = require('path'); | ||
const defaultConfig = { | ||
'bypass': false, | ||
'allowed': [ | ||
bypass: false, | ||
allowed: [ | ||
'css', | ||
@@ -15,12 +18,13 @@ 'js', | ||
'png', | ||
'webmanifest' | ||
'webmanifest', | ||
], | ||
'additional': [], | ||
'offlineUrl': '/offline.html', | ||
'fileName': 'sw.js', | ||
'outDir': path.resolve(__dirname, '../'), | ||
additional: [], | ||
offlineUrl: '/offline.html', | ||
fileName: 'sw.js', | ||
outDir: path.resolve(__dirname, '../'), | ||
}; | ||
const getConfig = (pkg, bundleId) => { | ||
const conf = pkg['precachingSW'] || {}; | ||
const conf = pkg.precachingSW || {}; | ||
@@ -57,2 +61,3 @@ let { | ||
/** | ||
@@ -68,64 +73,69 @@ * Flatten the bundle structure to array of strings | ||
...Array.from(bundle.entryAsset.parentBundle.childBundles) | ||
.map(a => { | ||
getAssets(a, result); | ||
}) | ||
.map(a => getAssets(a, result)), | ||
); | ||
return result; | ||
} | ||
return result; | ||
}; | ||
module.exports = bundler => { | ||
const { outDir } = bundler.options; | ||
bundler.on('bundled', async (bundle) => { | ||
// console.log('BUNDLE', bundle); | ||
let pkg; | ||
try { | ||
pkg = await bundle.entryAsset.getPackage(); | ||
} catch (e) { | ||
console.error('ERROR', e); | ||
return; | ||
const createServiceWorker = async (bundle, outDir) => { | ||
const bundleId = bundle.entryAsset.id; | ||
const pkg = await bundle.entryAsset.getPackage(); | ||
const config = getConfig(pkg, bundleId); | ||
// console.log('BUNDLE', bundle); | ||
// console.log('PKG', pkg); | ||
// console.log('ID', bundleId); | ||
// console.log('CONFIG', config); | ||
if (config.bypass === true) { | ||
if (existsSync(config.path)) { | ||
unlinkSync(config.path); | ||
} | ||
// console.log('PKG', pkg); | ||
const bundleId = bundle.entryAsset.id; | ||
// console.log('ID', bundleId); | ||
const config = getConfig(pkg, bundleId); | ||
// console.log('CONFIG', config); | ||
console.log(`Not generating a precaching serviceworker for "${bundleId}".`); | ||
return; | ||
} | ||
if (config.bypass === true) { | ||
if (existsSync(config.path)) { | ||
unlinkSync(config.path); | ||
} | ||
console.log(`Not generating a precaching serviceworker for "${bundleId}".`); | ||
return; | ||
const assets = getAssets(bundle).filter((name) => { | ||
if (name.includes('/icon_')) { | ||
return false; | ||
} | ||
const ext = name.split('.').pop(); | ||
return config.allowed.includes(ext); | ||
}).map(name => name.replace(outDir, '/assets')); | ||
const assets = getAssets(bundle).filter((name) => { | ||
if (name.includes('/icon_')) { | ||
return false; | ||
} | ||
const ext = name.split('.').pop(); | ||
return config.allowed.includes(ext); | ||
}).map(name => name.replace(outDir, '/assets')); | ||
if (config.additional && config.additional.length > 0) { | ||
assets.push(...config.additional); | ||
} | ||
assets.push(config.offlineUrl); | ||
const cache = JSON.stringify(assets); | ||
const cacheName = `${pkg.name}-${bundle.entryAsset.hash.substr(0, 8)}`; | ||
if (config.additional && config.additional.length > 0) { | ||
assets.push(...config.additional); | ||
} | ||
assets.push(config.offlineUrl); | ||
const cache = JSON.stringify(assets); | ||
const cacheName = `${pkg.name}-${bundle.entryAsset.hash.substr(0, 8)}`; | ||
const template = readFileSync(path.resolve( | ||
__dirname, | ||
'./precaching-sw.template.js', | ||
), 'utf8'); | ||
const template = readFileSync(path.resolve( | ||
__dirname, | ||
'./precaching-sw.template.js' | ||
), 'utf8'); | ||
const sw = template | ||
.replace('%{created}', new Date()) | ||
.replace('\'%{caches}\'', cache) | ||
.replace('%{cacheName}', cacheName) | ||
.replace('%{offlineUrl}', config.offlineUrl) | ||
.replace(/"/g, '\''); | ||
const sw = template | ||
.replace('%{created}', new Date()) | ||
.replace('\'%{caches}\'', cache) | ||
.replace('%{cacheName}', cacheName) | ||
.replace('%{offlineUrl}', config.offlineUrl) | ||
.replace(/"/g, '\''); | ||
writeFileSync(config.path, sw); | ||
}; | ||
writeFileSync(config.path, sw); | ||
module.exports = (bundler) => { | ||
const { outDir } = bundler.options; | ||
bundler.on('bundled', async (bundle) => { | ||
if (bundle.entryAsset === null && bundle.childBundles) { | ||
bundle.childBundles.forEach((b) => { | ||
createServiceWorker(b, outDir); | ||
}); | ||
} else { | ||
createServiceWorker(bundle, outDir); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "parcel-plugin-precaching-sw", | ||
"description": "Parcel plugin that creates a precaching service worker", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "./index.js", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -108,4 +108,16 @@ # Parcel plugin that creates a precaching serviceworker | ||
## Use it in your project | ||
Run: | ||
`npm i -D parcel-plugin-precaching-sw` | ||
or: | ||
`yarn add -D parcel-plugin-precaching-sw` | ||
Then optionally add settings to your package.json file. | ||
*** | ||
Background image from: https://www.pexels.com/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111257
192
122