2018-04-09 11:08:46 +02:00
|
|
|
/*!
|
|
|
|
* Script to generate our docs service worker.
|
|
|
|
* Copyright 2017-2018 The Bootstrap Authors
|
|
|
|
* Copyright 2017-2018 Twitter, Inc.
|
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
*/
|
|
|
|
|
2017-09-28 16:25:28 +02:00
|
|
|
'use strict'
|
|
|
|
|
2017-09-15 12:37:34 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const swBuild = require('workbox-build')
|
|
|
|
const config = require('./workbox.config.json')
|
2017-12-16 13:00:38 +01:00
|
|
|
|
2017-09-15 12:37:34 +02:00
|
|
|
const buildPrefix = '_gh_pages/'
|
|
|
|
|
|
|
|
const workboxSWSrcPath = require.resolve('workbox-sw')
|
|
|
|
const wbFileName = path.basename(workboxSWSrcPath)
|
2018-06-21 06:31:38 +02:00
|
|
|
const workboxSWDestPath = `${buildPrefix}docs/4.1/assets/js/vendor/${wbFileName}`
|
2017-09-15 12:37:34 +02:00
|
|
|
const workboxSWSrcMapPath = `${workboxSWSrcPath}.map`
|
|
|
|
const workboxSWDestMapPath = `${workboxSWDestPath}.map`
|
|
|
|
|
|
|
|
fs.createReadStream(workboxSWSrcPath).pipe(fs.createWriteStream(workboxSWDestPath))
|
|
|
|
fs.createReadStream(workboxSWSrcMapPath).pipe(fs.createWriteStream(workboxSWDestMapPath))
|
|
|
|
|
2018-03-16 21:46:58 +01:00
|
|
|
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: []
|
2017-09-15 12:37:34 +02:00
|
|
|
}
|
2018-03-16 21:46:58 +01:00
|
|
|
}
|
2017-09-15 12:37:34 +02:00
|
|
|
|
|
|
|
config.manifestTransforms = [updateUrl]
|
|
|
|
|
2018-03-16 21:46:58 +01:00
|
|
|
swBuild.injectManifest(config).then(({
|
|
|
|
count,
|
|
|
|
size
|
|
|
|
}) => {
|
2017-09-15 12:37:34 +02:00
|
|
|
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, () => {
|
2018-03-20 16:24:49 +01:00
|
|
|
console.log(`Pre-cache Manifest generated. Pre-cached ${count} files, totalling ${size} bytes.`)
|
2017-09-15 12:37:34 +02:00
|
|
|
})
|
|
|
|
})
|
2018-03-16 21:46:58 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
console.error(`Something went wrong: ${error}`)
|
2017-09-15 12:37:34 +02:00
|
|
|
})
|