forked from GoogleChromeLabs/squoosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_headers & _redirects generation (GoogleChromeLabs#240)
* Generate `_headers` and `_redirects` by passing assets through ejs templates. * PR feedback * Excluding service worker stuff from prerender * Build SW in dev * Let's give this a try * lol * Is this how it works?
- Loading branch information
1 parent
9b572f9
commit 6994cc3
Showing
6 changed files
with
100 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Long-term cache by default. | ||
/* | ||
Cache-Control: max-age=31536000 | ||
|
||
# And here are the exceptions: | ||
/ | ||
Cache-Control: no-cache | ||
|
||
/serviceworker.js | ||
Cache-Control: no-cache | ||
|
||
# URLs in /assets do not include a hash and are mutable. | ||
# But it isn't a big deal if the user gets an old version. | ||
/assets/* | ||
Cache-Control: must-revalidate, max-age=3600 |
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,47 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const ejs = require('ejs'); | ||
const AssetsPlugin = require('assets-webpack-plugin'); | ||
|
||
module.exports = class AssetTemplatePlugin extends AssetsPlugin { | ||
constructor(options) { | ||
options = options || {}; | ||
if (!options.template) throw Error('AssetTemplatePlugin: template option is required.'); | ||
super({ | ||
useCompilerPath: true, | ||
filename: options.filename, | ||
processOutput: files => this._processOutput(files) | ||
}); | ||
this._template = path.resolve(process.cwd(), options.template); | ||
const ignore = options.ignore || /(manifest\.json|\.DS_Store)$/; | ||
this._ignore = typeof ignore === 'function' ? ({ test: ignore }) : ignore; | ||
} | ||
|
||
_processOutput(files) { | ||
const mapping = { | ||
all: [], | ||
byType: {}, | ||
entries: {} | ||
}; | ||
for (const entryName in files) { | ||
// non-entry-point-derived assets are collected under an empty string key | ||
// since that's a bit awkward, we'll call them "assets" | ||
const name = entryName === '' ? 'assets' : entryName; | ||
const listing = files[entryName]; | ||
const entry = mapping.entries[name] = { | ||
all: [], | ||
byType: {} | ||
}; | ||
for (let type in listing) { | ||
const list = [].concat(listing[type]).filter(file => !this._ignore.test(file)); | ||
if (!list.length) continue; | ||
mapping.all = mapping.all.concat(list); | ||
mapping.byType[type] = (mapping.byType[type] || []).concat(list); | ||
entry.all = entry.all.concat(list); | ||
entry.byType[type] = (entry.byType[type] || []).concat(list); | ||
} | ||
} | ||
mapping.files = mapping.all; | ||
return ejs.render(fs.readFileSync(this._template, 'utf8'), mapping); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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