|
1 | 1 | module.exports = (api, options, rootOptions) => {
|
2 |
| - if (options.workboxPwa) { |
| 2 | + const fs = require('fs') |
| 3 | + const ext = api.hasPlugin('typescript') ? 'ts' : 'js' |
| 4 | + const fileEncoding = { encoding: 'utf-8' } |
| 5 | + |
| 6 | + api.extendPackage({ |
| 7 | + scripts: { |
| 8 | + 'pwa-build': 'vue-cli-service build --mode pwalocalserve', |
| 9 | + 'pwa-serve': 'npm run pwa-build && serve -s dist -l 8080' |
| 10 | + }, |
| 11 | + dependencies: { |
| 12 | + 'workbox-cacheable-response': '^6.1.5', |
| 13 | + 'workbox-core': '^6.1.5', |
| 14 | + 'workbox-expiration': '^6.1.5', |
| 15 | + 'workbox-routing': '^6.1.5', |
| 16 | + 'workbox-strategies': '^6.1.5', |
| 17 | + 'workbox-window': '^6.1.5' |
| 18 | + }, |
| 19 | + devDependencies: { |
| 20 | + 'workbox-webpack-plugin': '^6.1.5' |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + |
| 25 | + // set up pwaconfiguration |
| 26 | + let pwaConfig = { |
| 27 | + name: 'PWA App', |
| 28 | + shorName: 'App', |
| 29 | + themeColor: '#4DBA87', |
| 30 | + msTileColor: '#000000', |
| 31 | + manifestOptions: { |
| 32 | + start_url: '/' |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const manifestType = options.manifestType[0] |
| 37 | + console.log(`manifestType: ${options.manifestType}`) |
| 38 | + pwaConfig.workboxPluginMode = manifestType |
| 39 | + |
| 40 | + if (manifestType == 'InjectManifest') { |
| 41 | + pwaConfig. workboxOptions = { |
| 42 | + swSrc: './src/sw.js', |
| 43 | + swDest: 'service-worker.js' |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (options.vue?.pwa) { |
| 48 | + // update existing? |
| 49 | + console.log('has vue pwa') |
| 50 | + } else { |
3 | 51 | 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' |
| 52 | + vue: { |
| 53 | + pwa: pwaConfig |
18 | 54 | }
|
19 | 55 | })
|
20 | 56 |
|
21 |
| - api.render('./template', { |
22 |
| - ...options, |
23 |
| - }) |
| 57 | + console.log('pwa config set up complete') |
| 58 | + } |
24 | 59 |
|
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}`) |
| 60 | + // TODO: this is putting it in the root :( |
| 61 | + api.render('./template/public', { |
| 62 | + ...options, |
| 63 | + }) |
32 | 64 |
|
33 |
| - // get existing content |
34 |
| - let contentMain = fs.readFileSync(mainPath, { encoding: 'utf-8' }) |
| 65 | + api.render('./template/src', { |
| 66 | + ...options, |
| 67 | + }) |
35 | 68 |
|
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' |
| 69 | + // TODO: this errors with cwd command. It has to be a path. So I need another way to copy env files? |
| 70 | + // api.render('./template/_env.pwalocalserve') |
42 | 71 |
|
43 |
| - contentMain += addedContent |
| 72 | + // console.log('copying files complete') |
44 | 73 |
|
45 |
| - const lines = contentMain.split(/\r?\n/g).reverse() |
| 74 | + // console.log('starting .env setup') |
| 75 | + // if (fs.existsSync('.env')) { |
| 76 | + // const envFile = api.resolve('.env') |
| 77 | + // let envContent = fs.readFileSync(envFile, fileEncoding) |
| 78 | + // envContent += '\nVUE_APP_PWA_LOCAL_SERVE=false' |
| 79 | + // fs.writeFileSync(envFile, envContent, fileEncoding) |
| 80 | + // } else { |
| 81 | + // api.render('./template/_env') |
| 82 | + // } |
46 | 83 |
|
47 |
| - // inject import |
48 |
| - const lastImportIndex = lines.findIndex(line => line.match(/^import/)) |
49 |
| - lines[lastImportIndex] += importRegister |
| 84 | + // // if there is an .env.development file, copy it to .env.pwalocalserve and add the new setting |
| 85 | + // if (fs.existsSync('.env.development')) { |
| 86 | + // fs.copyFileSync('.env.development', '.env.pwalocalserve') |
| 87 | + // let envPwaFile = api.resolve('.env.pwalocalserve') |
| 88 | + // let envPwaContent = fs.readFileSync(envPwaFile, fileEncoding) |
| 89 | + // envPwaContent += '\nVUE_APP_PWA_LOCAL_SERVE=true' |
| 90 | + // fs.writeFileSync(envPwaFile, envPwaContent, fileEncoding) |
| 91 | + // } else { |
| 92 | + // api.render('./template/_env.pwalocalserve') |
| 93 | + // } |
50 | 94 |
|
51 |
| - // rebuild content |
52 |
| - contentMain = lines.reverse().join(`\n`) |
53 |
| - fs.writeFileSync(mainPath, contentMain, { encoding: 'utf-8' }) |
54 |
| - }) |
55 |
| - } |
| 95 | + // console.log('finished .env setup') |
| 96 | + |
| 97 | + // Inject service worker registration into main.js/.ts |
| 98 | + api.onCreateComplete(() => { |
| 99 | + const importRegister = `\nimport register from './service-worker/register-service-worker'` |
| 100 | + const mainPath = api.resolve(`./src/main.${ext}`) |
| 101 | + // get existing content |
| 102 | + let contentMain = fs.readFileSync(mainPath, fileEncoding) |
| 103 | + |
| 104 | + // modify add to content |
| 105 | + let addedContent = '\nregister()\n' |
| 106 | + addedContent += `\nif (process.env.NODE_ENV === 'development' || process.env.VUE_APP_PWA_LOCAL_SERVE === 'true') {` |
| 107 | + addedContent += '\n console.log(`PWA Local Serve: ${process.env.VUE_APP_PWA_LOCAL_SERVE}`) // eslint-disable no-console' |
| 108 | + addedContent += '\n console.log(`Node Env: ${process.env.NODE_ENV}`) // eslint-disable no-console' |
| 109 | + addedContent += '\n}\n' |
| 110 | + |
| 111 | + contentMain += addedContent |
| 112 | + const lines = contentMain.split(/\r?\n/g).reverse() |
| 113 | + |
| 114 | + // inject import |
| 115 | + const lastImportIndex = lines.findIndex(line => line.match(/^import/)) |
| 116 | + lines[lastImportIndex] += importRegister |
| 117 | + |
| 118 | + // rebuild content |
| 119 | + contentMain = lines.reverse().join(`\n`) |
| 120 | + fs.writeFileSync(mainPath, contentMain, fileEncoding) |
| 121 | + }) |
56 | 122 | }
|
0 commit comments