/** * After building the project it embeds the scripts/styles resources with the rel="preload" */ const fs = require('fs'); const pathToEntry = './build/index.html'; const bundlesRegExp = /\/static\/\w+\/\w+.[\da-z]+.\w{2,8}.\w{2,3}/g; const splitBy = ''; const builtHTMLContent = fs.readFileSync(pathToEntry).toString(); const links = builtHTMLContent.match(bundlesRegExp); const parts = builtHTMLContent.split(splitBy); let fileWithPreload = [parts[0], splitBy]; if (links) { for (const link of links) { let fileType = 'script'; if (/\.css$/.test(link)) { fileType = 'style'; } fileWithPreload = [ ...fileWithPreload, ``, ]; } } fileWithPreload = [...fileWithPreload, parts[1]]; fs.writeFileSync(pathToEntry, fileWithPreload.join(''));