|
1 | 1 | module.exports = (api, options, rootOptions) => {
|
2 |
| - api.extendPackage({ |
3 |
| - scripts: { |
4 |
| - 'pwa-build': 'vue-cli-service build --mode pwalocalserve', |
5 |
| - 'pwa-serve': 'npm run pwa-build && serve -s dist -l 8080' |
6 |
| - }, |
7 |
| - dependencies: { |
8 |
| - 'workbox-cacheable-response': '^6.1.5', |
9 |
| - 'workbox-core': '^6.1.5', |
10 |
| - 'workbox-expiration': '^6.1.5', |
11 |
| - 'workbox-routing': '^6.1.5', |
12 |
| - 'workbox-strategies': '^6.1.5', |
13 |
| - 'workbox-window': '^6.1.5' |
14 |
| - }, |
15 |
| - devDependencies: { |
16 |
| - 'workbox-webpack-plugin': '^6.1.5' |
17 |
| - } |
18 |
| - }) |
19 |
| - |
20 |
| - api.render('./template') |
21 |
| - |
22 |
| - // Inject service worker registration into main.js/.ts |
23 |
| - const importRegister = `import register from './service-worker/register-service-worker'` |
24 |
| - api.onCreateComplete(() => { |
25 |
| - // inject to main.js |
26 |
| - const fs = require('fs') |
27 |
| - const ext = api.hasPlugin('typescript') ? 'ts' : 'js' |
28 |
| - const mainPath = api.resolve(`./src/main.${ext}`) |
29 |
| - |
30 |
| - // get existing content |
31 |
| - let contentMain = fs.readFileSync(mainPath, { encoding: 'utf-8' }) |
32 |
| - const lines = contentMain.split(/\r?\n/g).reverse() |
33 |
| - |
34 |
| - // inject import |
35 |
| - const lastImportIndex = lines.findIndex(line => line.match(/^import/)) |
36 |
| - lines[lastImportIndex] += importRegister |
37 |
| - |
38 |
| - // modify add to content |
39 |
| - const addedContent = `register()` |
40 |
| - contentMain = lines.reverse().join(`\n${addedContent}\n`) |
41 |
| - fs.writeFileSync(mainPath, contentMain, { encoding: 'utf-8' }) |
42 |
| - }) |
| 2 | + if (options.workboxPwa) { |
| 3 | + api.extendPackage({ |
| 4 | + scripts: { |
| 5 | + 'pwa-build': 'vue-cli-service build --mode pwalocalserve', |
| 6 | + 'pwa-serve': 'npm run pwa-build && serve -s dist -l 8080' |
| 7 | + }, |
| 8 | + dependencies: { |
| 9 | + 'workbox-cacheable-response': '^6.1.5', |
| 10 | + 'workbox-core': '^6.1.5', |
| 11 | + 'workbox-expiration': '^6.1.5', |
| 12 | + 'workbox-routing': '^6.1.5', |
| 13 | + 'workbox-strategies': '^6.1.5', |
| 14 | + 'workbox-window': '^6.1.5' |
| 15 | + }, |
| 16 | + devDependencies: { |
| 17 | + 'workbox-webpack-plugin': '^6.1.5' |
| 18 | + } |
| 19 | + }) |
| 20 | + |
| 21 | + api.render('./template', { |
| 22 | + ...options, |
| 23 | + }) |
| 24 | + |
| 25 | + // Inject service worker registration into main.js/.ts |
| 26 | + const importRegister = `\nimport register from './service-worker/register-service-worker'` |
| 27 | + api.onCreateComplete(() => { |
| 28 | + // inject to main.js |
| 29 | + const fs = require('fs') |
| 30 | + const ext = api.hasPlugin('typescript') ? 'ts' : 'js' |
| 31 | + const mainPath = api.resolve(`./src/main.${ext}`) |
| 32 | + |
| 33 | + // get existing content |
| 34 | + let contentMain = fs.readFileSync(mainPath, { encoding: 'utf-8' }) |
| 35 | + |
| 36 | + // modify add to content |
| 37 | + let addedContent = '\nregister()\n' |
| 38 | + addedContent += `\nif (process.env.NODE_ENV === 'development' || process.env.VUE_APP_PWA_LOCAL_SERVE === 'true') {` |
| 39 | + addedContent += '\n console.log(`PWA Local Serve: ${process.env.VUE_APP_PWA_LOCAL_SERVE}`) // eslint-disable no-console' |
| 40 | + addedContent += '\n console.log(`Node Env: ${process.env.NODE_ENV}`) // eslint-disable no-console' |
| 41 | + addedContent += '\n}\n' |
| 42 | + |
| 43 | + contentMain += addedContent |
| 44 | + |
| 45 | + const lines = contentMain.split(/\r?\n/g).reverse() |
| 46 | + |
| 47 | + // inject import |
| 48 | + const lastImportIndex = lines.findIndex(line => line.match(/^import/)) |
| 49 | + lines[lastImportIndex] += importRegister |
| 50 | + |
| 51 | + // rebuild content |
| 52 | + contentMain = lines.reverse().join(`\n`) |
| 53 | + fs.writeFileSync(mainPath, contentMain, { encoding: 'utf-8' }) |
| 54 | + }) |
| 55 | + } |
43 | 56 | }
|
0 commit comments