MCPcopy
hub / github.com/shadowwalker/next-pwa / buildCustomWorker

Function buildCustomWorker

build-custom-worker.js:9–118  ·  view source on GitHub ↗
({ id, basedir, customWorkerDir, destdir, plugins, minify })

Source from the content-addressed store, hash-verified

7const TerserPlugin = require('terser-webpack-plugin')
8
9const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, minify }) => {
10 let workerDir = undefined
11
12 if (fs.existsSync(path.join(basedir, customWorkerDir))) {
13 workerDir = path.join(basedir, customWorkerDir)
14 } else if (fs.existsSync(path.join(basedir, 'src', customWorkerDir))) {
15 workerDir = path.join(basedir, 'src', customWorkerDir)
16 }
17
18 if (!workerDir) return
19
20 const name = `worker-${id}.js`
21 const customWorkerEntries = ['ts', 'js']
22 .map(ext => path.join(workerDir, `index.${ext}`))
23 .filter(entry => fs.existsSync(entry))
24
25 if (customWorkerEntries.length === 0) return
26
27 if (customWorkerEntries.length > 1) {
28 console.warn(
29 `> [PWA] WARNING: More than one custom worker found (${customWorkerEntries.join(
30 ','
31 )}), not building a custom worker`
32 )
33 return
34 }
35
36 const customWorkerEntry = customWorkerEntries[0]
37 console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`)
38 console.log(`> [PWA] Build custom worker: ${path.join(destdir, name)}`)
39 webpack({
40 mode: 'none',
41 target: 'webworker',
42 entry: {
43 main: customWorkerEntry
44 },
45 resolve: {
46 extensions: ['.ts', '.js'],
47 fallback: {
48 module: false,
49 dgram: false,
50 dns: false,
51 path: false,
52 fs: false,
53 os: false,
54 crypto: false,
55 stream: false,
56 http2: false,
57 net: false,
58 tls: false,
59 zlib: false,
60 child_process: false
61 }
62 },
63 module: {
64 rules: [
65 {
66 test: /\.(t|j)s$/i,

Callers 1

webpackFunction · 0.85

Calls 1

webpackFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…