46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
/**
|
|
* Build manifest parsing
|
|
*/
|
|
|
|
import { join } from '../utils.js';
|
|
|
|
const _NS = '/_next/static/';
|
|
const _build_manifest_name = '_buildManifest.js';
|
|
const _ssg_manifest_name = '_ssgManifest.js';
|
|
const _build_manifest_path = `/${_build_manifest_name}`;
|
|
const _ssg_manifest_path = `/${_ssg_manifest_name}`;
|
|
|
|
export const _manifest_paths = [_build_manifest_path, _ssg_manifest_path];
|
|
|
|
/**
|
|
* Parse build manifest script
|
|
* @param {string} script - Build manifest script content
|
|
* @returns {Object} Parsed manifest object
|
|
*/
|
|
export function parseBuildManifest(script) {
|
|
const s = script.trim();
|
|
|
|
if (!s.startsWith('self.__BUILD_MANIFEST')) {
|
|
throw new Error('Invalid build manifest (not starting by `"self.__BUILD_MANIFEST"`).');
|
|
}
|
|
|
|
// Wrap in IIFE and evaluate
|
|
const func = `(function() {self={};${s.replace(/;$/, '')};return self.__BUILD_MANIFEST})();`;
|
|
|
|
try {
|
|
return eval(func);
|
|
} catch (e) {
|
|
console.warn(`Could not parse the given build manifest \`${s}\``);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get build manifest path
|
|
* @param {string} buildId - Build ID
|
|
* @param {string} basePath - Base path (optional)
|
|
* @returns {string} Build manifest path
|
|
*/
|
|
export function getBuildManifestPath(buildId, basePath = '') {
|
|
return join(basePath, _NS, buildId, _build_manifest_name);
|
|
}
|