UNPKG

@typed/content-hash

Version:

Content hash a directory of HTML/JS/CSS files and other static assets

70 lines 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rewriteDocumentContents = void 0; const tslib_1 = require("tslib"); const remapping_1 = (0, tslib_1.__importDefault)(require("@ampproject/remapping")); const Env_1 = require("@typed/fp/Env"); const FxEnv_1 = require("@typed/fp/FxEnv"); const function_1 = require("fp-ts/function"); const Option_1 = require("fp-ts/Option"); const magic_string_1 = (0, tslib_1.__importDefault)(require("magic-string")); const path_1 = require("path"); const getFileExtension_1 = require("./plugins/getFileExtension"); const sha512Hash_1 = require("./sha512Hash"); const sourceMapExt = '.map'; const proxyJsExt = '.proxy.js'; const rewriteContentHash = (document) => (0, function_1.pipe)(document.contentHash, (0, Option_1.match)(() => document, (hash) => ({ ...document, contentHash: (0, Option_1.some)(hash.type === 'hash' ? { type: 'hash', hash: (0, sha512Hash_1.sha512Hash)(document.contents) } : hash), }))); const remapSourceMaps = (current, updated) => ({ ...updated, contentHash: current.contentHash, contents: JSON.stringify(JSON.parse((0, remapping_1.default)([updated.contents, current.contents], () => null).toString()), null, 2), }); function rewriteDocumentContents(document, f, sourceMaps, skipHashUpdate) { return (0, FxEnv_1.Do)(function* (_) { const { documentRegistry } = yield* _((0, Env_1.ask)()); const { filePath, contents, sourceMap, isBase64Encoded } = document; const filename = path_1.posix.basename(filePath); const ext = (0, getFileExtension_1.getFileExtension)(filename); // We don't rewrite source maps or base64 encoded documents if (ext.endsWith(sourceMapExt) || isBase64Encoded) { return documentRegistry; } const magicString = new magic_string_1.default(contents, { filename, indentExclusionRanges: [], }); f(magicString); const updatedContents = magicString.toString(); const updatedDocument = (0, function_1.pipe)({ ...document, contents: updatedContents, }, skipHashUpdate ? function_1.identity : rewriteContentHash); const updatedRegistry = new Map([...documentRegistry, [filePath, updatedDocument]]); if (!sourceMaps || (0, Option_1.isNone)(sourceMap) || ext.endsWith(proxyJsExt)) { return updatedRegistry; } const sourceMapPath = sourceMap.value; const updatedSourceMapContents = magicString .generateMap({ hires: true, file: filename, source: magicString.original, includeContent: true }) .toString(); const updatedSourceMap = { filePath: sourceMapPath, fileExtension: document.fileExtension + sourceMapExt, contents: updatedSourceMapContents, contentHash: (0, Option_1.some)({ type: 'hashFor', filePath: document.filePath }), dependencies: [], sourceMap: Option_1.none, isBase64Encoded: false, }; const currentSourceMap = documentRegistry.get(sourceMapPath); return new Map([ ...updatedRegistry, [sourceMapPath, currentSourceMap ? remapSourceMaps(currentSourceMap, updatedSourceMap) : updatedSourceMap], ]); }); } exports.rewriteDocumentContents = rewriteDocumentContents; //# sourceMappingURL=rewriteDocumentContents.js.map