Skip to content

Commit 6098f79

Browse files
Karen BaneyKaren Baney
authored andcommitted
add vue.config.js. Other minor changes
1 parent 5dbdc35 commit 6098f79

File tree

7 files changed

+77
-55
lines changed

7 files changed

+77
-55
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# vue-cli-plugin-workbox-pwa
2+
**ALPA**
23
A Vue CLI Plugin with advanced support for workbox 6.x and Vue 3. Includes the ability to debug the service worker locally.
34

45
How is this different from the vue-cli-plugin-pwa?

generator/index.js

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
11
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+
}
4356
}

generator/template/_env

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
---
2-
extend: '@vue/cli-service/generator/template/_env'
3-
---
4-
5-
<%# REPLACE %>
61
VUE_APP_PWA_LOCAL_SERVE=false
7-
<%# END_REPLACE %>

generator/template/main.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

generator/template/vue.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
pwa: {
3+
name: 'KB Coder PWA App',
4+
shorName: 'KB Coder',
5+
themeColor: '#400080',
6+
msTileColor: '#800080',
7+
manifestOptions: {
8+
start_url: '/'
9+
},
10+
workboxPluginMode: 'InjectManifest',
11+
workboxOptions: {
12+
swSrc: './src/service-worker/sw.js',
13+
swDest: 'service-worker.js'
14+
}
15+
}
16+
}

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ module.exports = (api, options) => {
3535
.after('html')
3636

3737
webpackConfig
38-
.plugin('html')
39-
.tap(args => {
40-
args[0].title = userOptions.name // Add title to the app bar when installed as PWA.
41-
return args
42-
})
38+
.plugin('html')
39+
.tap(args => {
40+
args[0].title = userOptions.name // Add title to the app bar when installed as PWA.
41+
return args
42+
})
4343

4444
// generate /service-worker.js in production mode or pwalocalserve
4545
// Workbox requires NODE_ENV='development' to show debugging statements for the service worker, so

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cli-plugin-workbox-pwa",
3-
"version": "1.0.0",
3+
"version": "0.0.1-alpha",
44
"description": "vue-cli plugin for Workbox and Vue 3",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)