Using Rollup to bundle my Sapper app, I am getting an error in service-worker.js where process.env
is not defined. This doesn’t appear to be an issue using Webpack.
I sourced the code back to https://github.com/moovweb/xdn/blob/master/packages/prefetch/src/constants.ts
As a workaround, I added the references to the replace
plugin based on the default values. In rollup.config.js
, I have:
serviceworker: {
input: config.serviceworker.input(),
output: config.serviceworker.output(),
plugins: [
resolve(),
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.XDN_PREFETCH_CACHE_NAME': '"prefetch"',
'process.env.BACKEND_REQUESTS_RESPONSE_HEADER_NAME': '"x-xdn-upstream-requests"',
'process.env.XDN_PREFETCH_HEADER_VALUE': '"1"'
}),
commonjs(),
!dev && terser(),
],
preserveEntrySignatures: false,
onwarn,
},
To reproduce:
npx degit "sveltejs/sapper-template#rollup" sapper-rollup
cd sapper-rollup
npm i
xdn init
Add the following to service-worker.js
:
/* begin: add this to src/service-worker.js */
import { precacheAndRoute } from 'workbox-precaching'
import { Prefetcher } from '@xdn/prefetch/sw'
precacheAndRoute([])
new Prefetcher().route()
/* end: add this to src/service-worker.js */
Then start the service:
xdn run
Open http://127.0.0.1:3000 and observe the error in the dev console.