-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from hypothesis/generate-manifest
Add function for generating asset manifests
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { buildJS, watchJS } from './lib/rollup.js'; | ||
export { buildCSS } from './lib/sass.js'; | ||
export { generateManifest } from './lib/manifest.js'; | ||
export { runTests } from './lib/tests.js'; | ||
export { run } from './lib/run.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,55 @@ | ||
import { createHash } from 'crypto'; | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import * as path from 'path'; | ||
|
||
import glob from 'glob'; | ||
|
||
/** | ||
* Generate a manifest that maps asset paths to cache-busted URLs. | ||
* | ||
* The generated manifest file is suitable for use with the h-assets Python | ||
* package (https://pypi.org/project/h-assets/) used by backend Hypothesis | ||
* projects for serving static assets. The manifest looks like: | ||
* | ||
* ``` | ||
* { | ||
* "scripts/app.bundle.js": "scripts/app.bundle.js?abc123", | ||
* "styles/app.css": "styles/app.css?def456", | ||
* ... | ||
* } | ||
* ``` | ||
* | ||
* Returns the data that was written to the manifest. | ||
* | ||
* @param {object} options | ||
* @param {string} [options.pattern] - Glob pattern that specifies which assets to include | ||
* @param {string} [options.manifestPath] - File path to write the manifest to | ||
* @return {Promise<Record<string, string>>} | ||
*/ | ||
export async function generateManifest({ | ||
pattern = 'build/**/*.{css,js,map}', | ||
manifestPath = 'build/manifest.json', | ||
} = {}) { | ||
const manifestDir = path.dirname(manifestPath); | ||
const files = glob.sync(pattern); | ||
|
||
/** @type {Record<string, string>} */ | ||
const manifest = {}; | ||
|
||
await Promise.all( | ||
files.map(async file => { | ||
const fileContent = await readFile(file); | ||
const hash = await createHash('sha1'); | ||
hash.update(fileContent); | ||
|
||
const hashSuffix = hash.digest('hex').slice(0, 6); | ||
const relativePath = path.relative(manifestDir, file); | ||
manifest[relativePath] = `${relativePath}?${hashSuffix}`; | ||
}) | ||
); | ||
|
||
const manifestData = Buffer.from(JSON.stringify(manifest, null, 2), 'utf-8'); | ||
await writeFile(manifestPath, manifestData); | ||
|
||
return manifest; | ||
} |