aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/twbs/bootstrap/build/workbox.js
blob: 3a7ba450a81bd31c46ec52766a1c8d5437bef74e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*!
 * 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)
 */

'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}docs/4.1/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}`)
})