1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/build/workbox.js
2018-03-21 12:15:47 +02:00

51 lines
1.5 KiB
JavaScript

'use strict'
const fs = require('fs')
const path = require('path')
const swBuild = require('workbox-build')
const config = require('./workbox.config.json')
const buildPrefix = '_gh_pages/'
const workboxSWSrcPath = require.resolve('workbox-sw')
const wbFileName = path.basename(workboxSWSrcPath)
const workboxSWDestPath = `${buildPrefix}assets/js/vendor/${wbFileName}`
const workboxSWSrcMapPath = `${workboxSWSrcPath}.map`
const workboxSWDestMapPath = `${workboxSWDestPath}.map`
fs.createReadStream(workboxSWSrcPath).pipe(fs.createWriteStream(workboxSWDestPath))
fs.createReadStream(workboxSWSrcMapPath).pipe(fs.createWriteStream(workboxSWDestMapPath))
const updateUrl = (manifestEntries) => {
const manifest = manifestEntries.map((entry) => {
if (entry.url.startsWith(buildPrefix)) {
const regex = new RegExp(buildPrefix, 'g')
entry.url = entry.url.replace(regex, '')
}
return entry
})
return {
manifest,
warnings: []
}
}
config.manifestTransforms = [updateUrl]
swBuild.injectManifest(config).then(({
count,
size
}) => {
const wbSwRegex = /{fileName}/g
fs.readFile(config.swDest, 'utf8', (err, data) => {
if (err) {
throw err
}
const swFileContents = data.replace(wbSwRegex, wbFileName)
fs.writeFile(config.swDest, swFileContents, () => {
console.log(`Pre-cache Manifest generated. Pre-cached ${count} files, totalling ${size} bytes.`)
})
})
}).catch((error) => {
console.error(`Something went wrong: ${error}`)
})