-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-offline): Allow appending custom scripts (#11626)
- Loading branch information
1 parent
dc8e9f3
commit 275344e
Showing
7 changed files
with
293 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/gatsby-plugin-offline/src/__tests__/fixtures/custom-sw-code.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log(`Hello, world!`) |
59 changes: 59 additions & 0 deletions
59
packages/gatsby-plugin-offline/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { onPostBuild } = require(`../gatsby-node`) | ||
const fs = require(`fs`) | ||
|
||
jest.mock(`fs`) | ||
jest.mock(`../get-resources-from-html`, () => () => []) | ||
|
||
let swText = `` | ||
|
||
jest.mock(`workbox-build`, () => { | ||
return { | ||
generateSW() { | ||
return Promise.resolve({ count: 1, size: 1, warnings: [] }) | ||
}, | ||
} | ||
}) | ||
|
||
describe(`onPostBuild`, () => { | ||
const componentChunkName = `chunkName` | ||
const chunks = [`chunk1`, `chunk2`] | ||
|
||
// Mock webpack.stats.json | ||
const stats = { | ||
assetsByChunkName: { | ||
[componentChunkName]: chunks, | ||
}, | ||
} | ||
|
||
// Mock out filesystem functions | ||
fs.readFileSync.mockImplementation(file => { | ||
if (file === `${process.cwd()}/public/webpack.stats.json`) { | ||
return JSON.stringify(stats) | ||
} else if (file === `public/sw.js`) { | ||
return swText | ||
} else if (file.match(/\/sw-append\.js/)) { | ||
return `` | ||
} else { | ||
return jest.requireActual(`fs`).readFileSync(file) | ||
} | ||
}) | ||
|
||
fs.appendFileSync.mockImplementation((file, text) => { | ||
swText += text | ||
}) | ||
|
||
fs.createReadStream.mockImplementation(() => { | ||
return { pipe() {} } | ||
}) | ||
|
||
fs.createWriteStream.mockImplementation(() => {}) | ||
|
||
it(`appends to sw.js`, async () => { | ||
await onPostBuild( | ||
{ pathPrefix: `` }, | ||
{ appendScript: `${__dirname}/fixtures/custom-sw-code.js` } | ||
) | ||
|
||
expect(swText).toContain(`console.log(\`Hello, world!\`)`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.