forked from shadowwalker/next-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-fallback-worker.js
146 lines (130 loc) · 4.24 KB
/
build-fallback-worker.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const getFallbackEnvs = ({ fallbacks, basedir, id, pageExtensions }) => {
let { document, data } = fallbacks
if (!document) {
let pagesDir = undefined
if (fs.existsSync(path.join(basedir, 'pages'))) {
pagesDir = path.join(basedir, 'pages')
} else if (fs.existsSync(path.join(basedir, 'src', 'pages'))) {
pagesDir = path.join(basedir, 'src', 'pages')
}
if (!pagesDir) return
const offlines = pageExtensions
.map(ext => path.join(pagesDir, `_offline.${ext}`))
.filter(entry => fs.existsSync(entry))
if (offlines.length === 1) {
document = '/_offline'
}
}
if (data && data.endsWith('.json')) {
data = path.posix.join('/_next/data', id, data)
}
const envs = {
__PWA_FALLBACK_DOCUMENT__: document || false,
__PWA_FALLBACK_IMAGE__: fallbacks.image || false,
__PWA_FALLBACK_AUDIO__: fallbacks.audio || false,
__PWA_FALLBACK_VIDEO__: fallbacks.video || false,
__PWA_FALLBACK_FONT__: fallbacks.font || false,
__PWA_FALLBACK_DATA__: data || false
}
if (Object.values(envs).filter(v => !!v).length === 0) return
console.log('> [PWA] Fallback to precache routes when fetch failed from cache or network:')
if (envs.__PWA_FALLBACK_DOCUMENT__) console.log(`> [PWA] document (page): ${envs.__PWA_FALLBACK_DOCUMENT__}`)
if (envs.__PWA_FALLBACK_IMAGE__) console.log(`> [PWA] image: ${envs.__PWA_FALLBACK_IMAGE__}`)
if (envs.__PWA_FALLBACK_AUDIO__) console.log(`> [PWA] audio: ${envs.__PWA_FALLBACK_AUDIO__}`)
if (envs.__PWA_FALLBACK_VIDEO__) console.log(`> [PWA] video: ${envs.__PWA_FALLBACK_VIDEO__}`)
if (envs.__PWA_FALLBACK_FONT__) console.log(`> [PWA] font: ${envs.__PWA_FALLBACK_FONT__}`)
if (envs.__PWA_FALLBACK_DATA__) console.log(`> [PWA] data (/_next/data/**/*.json): ${envs.__PWA_FALLBACK_DATA__}`)
return envs
}
const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, minify, pageExtensions }) => {
const envs = getFallbackEnvs({ fallbacks, basedir, id, pageExtensions })
if (!envs) return
const name = `fallback-${id}.js`
const fallbackJs = path.join(__dirname, `fallback.js`)
webpack({
mode: 'none',
target: 'webworker',
entry: {
main: fallbackJs
},
resolve: {
extensions: ['.js'],
fallback: {
module: false,
dgram: false,
dns: false,
path: false,
fs: false,
os: false,
crypto: false,
stream: false,
http2: false,
net: false,
tls: false,
zlib: false,
child_process: false
}
},
module: {
rules: [
{
test: /\.js$/i,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'next/babel',
{
'transform-runtime': {
corejs: false,
helpers: true,
regenerator: false,
useESModules: true
},
'preset-env': {
modules: false,
targets: 'chrome >= 56'
}
}
]
]
}
}
]
}
]
},
output: {
path: destdir,
filename: name
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(destdir, 'fallback-*.js'), path.join(destdir, 'fallback-*.js.map')]
}),
new webpack.EnvironmentPlugin(envs)
],
optimization: minify
? {
minimize: true,
minimizer: [new TerserPlugin()]
}
: undefined
}).run((error, status) => {
if (error || status.hasErrors()) {
console.error(`> [PWA] Failed to build fallback worker`)
console.error(status.toString({ colors: true }))
process.exit(-1)
}
})
return { fallbacks, name, precaches: Object.values(envs).filter(v => !!v) }
}
module.exports = buildFallbackWorker