Skip to content

Commit

Permalink
feat: support manifest.fileName with template
Browse files Browse the repository at this point in the history
closes #193. Huge thanks to @mdawar and @AlexAsh
  • Loading branch information
pooya parsa committed Mar 12, 2020
1 parent 4484e6c commit f05353b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
18 changes: 8 additions & 10 deletions lib/manifest/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function addManifest (pwa) {
background_color: '#ffffff',
theme_color: this.options.loading && this.options.loading.color,
lang: 'en',
useWebmanifestExtension: false
useWebmanifestExtension: false,
fileName: 'manifest.[hash].[ext]'
}

const options = { ...defaults, ...pwa.manifest }
Expand All @@ -39,16 +40,12 @@ function addManifest (pwa) {
delete manifest.src
delete manifest.publicPath
delete manifest.useWebmanifestExtension
delete manifest.fileName

// Stringify manifest & generate hash
const manifestSource = JSON.stringify(manifest)
const manifestFileName = `manifest.${
hash(manifestSource).substr(0, 8)
}.${
options.useWebmanifestExtension
? 'webmanifest'
: 'json'
}`
// Generate file name
const manifestFileName = options.fileName
.replace('[hash]', hash(JSON.stringify(manifest)).substr(0, 8))
.replace('[ext]', options.useWebmanifestExtension ? 'webmanifest' : 'json')

// Merge final manifest into options.manifest for other modules
if (!this.options.manifest) {
Expand All @@ -57,6 +54,7 @@ function addManifest (pwa) {
Object.assign(this.options.manifest, manifest)

// Register webpack plugin to emit manifest
const manifestSource = JSON.stringify(manifest, null, 2)
this.options.build.plugins.push({
apply (compiler) {
compiler.hooks.emit.tap('nuxt-pwa-manifest', (compilation) => {
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/pwa.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Array [
"fixture/.nuxt/dist/client/icons/icon_384.b8f3a1.png",
"fixture/.nuxt/dist/client/icons/icon_512.b8f3a1.png",
"fixture/.nuxt/dist/client/icons/icon_64.b8f3a1.png",
"fixture/.nuxt/dist/client/manifest.bee706d5.webmanifest",
"fixture/.nuxt/dist/client/manifest_test.webmanifest",
"fixture/.nuxt/dist/server",
"fixture/.nuxt/dist/server/index.spa.html",
"fixture/.nuxt/dist/server/index.ssr.html",
Expand All @@ -44,7 +44,7 @@ Array [
"fixture/dist/_nuxt/icons/icon_384.b8f3a1.png",
"fixture/dist/_nuxt/icons/icon_512.b8f3a1.png",
"fixture/dist/_nuxt/icons/icon_64.b8f3a1.png",
"fixture/dist/_nuxt/manifest.bee706d5.webmanifest",
"fixture/dist/_nuxt/manifest_test.webmanifest",
"fixture/dist/icon.png",
"fixture/dist/index.html",
"fixture/dist/offline.html",
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
manifest: {
name: 'Test Project Name',
description: 'Test Project Description',
useWebmanifestExtension: true
useWebmanifestExtension: true,
fileName: 'manifest_test.[ext]?[hash]'
},

workbox: {
Expand Down
4 changes: 2 additions & 2 deletions test/pwa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ describe('pwa', () => {

test('icons purpose', () => {
const assetDir = path.join(nuxt.options.generate.dir, '_nuxt')
const manifestFileName = fs.readdirSync(assetDir).find(item => item.match(/^manifest.+\.json$/i))
const manifestContent = JSON.parse(fs.readFileSync(path.join(assetDir, manifestFileName)))
const manifestFileName = fs.readdirSync(assetDir).find(item => item.match(/^manifest./i))
const manifestContent = JSON.parse(fs.readFileSync(path.join(assetDir, manifestFileName.split('?')[0])))
expect(manifestContent.icons).toEqual(
expect.arrayContaining([
expect.objectContaining({
Expand Down

0 comments on commit f05353b

Please sign in to comment.