Last active
January 12, 2018 08:18
-
-
Save mrienstra/218c0836208af352951a663652a6d7c7 to your computer and use it in GitHub Desktop.
Revisions
-
mrienstra revised this gist
Jan 12, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ const options = { ], to: [ '', (match, p1, p2) => { let output = `<script> setTimeout(function(){ var insertScript = function (src, id) { @@ -35,7 +35,7 @@ var insertScript = function (src, id) { } output += `);\n`; } return output + `}, 0);\n</script>` + (p2 || '') + `</body></html>`; }, ], }; -
mrienstra revised this gist
Jan 12, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const options = { ], from: [ /<link rel="preload" href="\/_next\/[^\/]+\/[^.]+\.js" as="script"\/>/g, /(<script.+<\/script>)(<\/div>)*<\/body><\/html>/, // optional `</div>`: appears in next.js 4.2.1 but not in 4.3.0-canary.1 ], to: [ '', -
mrienstra created this gist
Jan 12, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ const replace = require('replace-in-file'); // https://www.npmjs.com/package/replace-in-file const path = require('path'); const scriptName = path.basename(process.argv[1]); const options = { files: [ 'out/*.html', 'out/**/*.html', ], from: [ /<link rel="preload" href="\/_next\/[^\/]+\/[^.]+\.js" as="script"\/>/g, /(<script.+<\/script>)<\/div><\/body><\/html>/, ], to: [ '', (match, p1) => { let output = `<script> setTimeout(function(){ var insertScript = function (src, id) { var scriptEl = document.createElement('script'); id && (scriptEl.id = id); scriptEl.src = src; document.body.appendChild(scriptEl); } `; const scripts = p1.split('</script>'); let i, l, matches; for (i = 0, l = scripts.length; i < l; i++) { if (!scripts[i]) continue; output += ` insertScript('` + scripts[i].match(/ src="([^"]+)"/)[1] + `'`; matches = scripts[i].match(/ id="([^"]+)"/); if (matches) { output += `, '` + matches[1] + `'`; } output += `);\n`; } return output + `}, 0);\n</script></body></html>`; }, ], }; replace(options) .then(changes => { console.log(scriptName + ': modified files:', changes.join(', ')); }) .catch(error => { console.error(scriptName + ': error occurred:', error); });