☕️ Classic, reliable, trusted test framework for Node.js and the browser
chore: fix header generation and production build crashes (#5100)
Josh Goldberg ✨51502abdd8bf
diff --git a/package-scripts.js b/package-scripts.jsindex 6fb05949..1e4c0729 100644--- a/package-scripts.js+++ b/package-scripts.js@@ -259,7 +259,7 @@ module.exports = {docs: {default: {script:- 'nps docs.clean && nps docs.api && eleventy && nps docs.linkcheck && node scripts/netlify-headers.js docs/_site >> docs/_site/_headers',+ 'nps docs.clean && nps docs.api && eleventy && nps docs.linkcheck',description: 'Build documentation'},production: {@@ -277,7 +277,7 @@ module.exports = {},postbuild: {script:- 'buildProduction docs/_site/index.html --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --svgo --inlinehtmlimage 9400 --inlinehtmlscript 0 --asyncscripts && cp docs/_headers docs/_dist/_headers && node scripts/netlify-headers.js docs/_dist >> docs/_dist/_headers',+ 'node node_modules/assetgraph-builder/bin/buildProduction docs/_site/index.html --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --svgo --inlinehtmlimage 9400 --inlinehtmlscript 0 --asyncscripts && cp docs/_headers docs/_dist/_headers',description: 'Post-process docs after build',hiddenFromHelp: true},diff --git a/scripts/netlify-headers.js b/scripts/netlify-headers.jsdeleted file mode 100644index e881423d..00000000--- a/scripts/netlify-headers.js+++ /dev/null@@ -1,126 +0,0 @@-'use strict';--const AssetGraph = require('assetgraph');--const dest = process.argv[2];--if (!dest) {- console.error('usage: node netlify-headers.js <docs-output-dir>');- console.error('example: node netlify-headers.js docs/_dist');- process.exit(1);-}--const headers = ['Content-Security-Policy'];--const resourceHintTypeMap = {- HtmlPreloadLink: 'preload',- HtmlPrefetchLink: 'prefetch',- HtmlPreconnectLink: 'preconnect',- HtmlDnsPrefetchLink: 'dns-prefetch'-};--function getHeaderForRelation(rel) {- let header = `Link: <${rel.href}>; rel=${resourceHintTypeMap[rel.type]}; as=${- rel.as- }; type=${rel.to.contentType}`;-- if (rel.as === 'font') {- header = `${header}; crossorigin=anonymous`;- }-- return header;-}--console.error('Generating optimal netlify headers...');--new AssetGraph({root: dest})- .loadAssets('*.html')- .populate({- followRelations: {type: 'HtmlAnchor', crossorigin: false}- })- .queue(function (assetGraph) {- const assets = assetGraph.findAssets({- type: 'Html',- isInline: false,- isLoaded: true- });-- const headerMap = {};-- assets.forEach(function (asset) {- const url =- '/' +- asset.url- .replace(assetGraph.root, '')- .replace(/#.*/, '')- .replace('index.html', '');- if (!headerMap[url]) {- headerMap[url] = [];- }-- headers.forEach(function (header) {- const node = asset.parseTree.querySelector(- 'meta[http-equiv=' + header + ']'- );-- if (node) {- headerMap[url].push(`${header}: ${node.getAttribute('content')}`);-- node.parentNode.removeChild(node);- asset.markDirty();- }- });-- const firstCssRel = asset.outgoingRelations.filter(r => {- return (- r.type === 'HtmlStyle' &&- r.crossorigin === false &&- r.href !== undefined- );- })[0];-- if (firstCssRel) {- const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`;-- headerMap[url].push(header);- }-- const resourceHintRelations = asset.outgoingRelations.filter(r =>- ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type)- );-- resourceHintRelations.forEach(rel => {- headerMap[url].push(getHeaderForRelation(rel));-- rel.detach();- });-- const preconnectRelations = asset.outgoingRelations.filter(r =>- ['HtmlPreconnectLink'].includes(r.type)- );-- preconnectRelations.forEach(rel => {- const header = `Link: <${rel.href}>; rel=preconnect`;-- headerMap[url].push(header);-- rel.detach();- });- });-- console.log('\n## Autogenerated headers:\n');-- Object.keys(headerMap).forEach(function (url) {- console.log(url);-- const httpHeaders = headerMap[url];-- httpHeaders.forEach(function (header) {- console.log(` ${header}`);- });-- console.log('');- });- console.error('netlify headers done!');- })- .run();