Skip to content

Instantly share code, notes, and snippets.

@mrienstra
Last active January 12, 2018 08:18
Show Gist options
  • Select an option

  • Save mrienstra/218c0836208af352951a663652a6d7c7 to your computer and use it in GitHub Desktop.

Select an option

Save mrienstra/218c0836208af352951a663652a6d7c7 to your computer and use it in GitHub Desktop.

Revisions

  1. mrienstra revised this gist Jan 12, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions export_postprocess.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ const options = {
    ],
    to: [
    '',
    (match, p1) => {
    (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></body></html>`;
    return output + `}, 0);\n</script>` + (p2 || '') + `</body></html>`;
    },
    ],
    };
  2. mrienstra revised this gist Jan 12, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion export_postprocess.js
    Original 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>/,
    /(<script.+<\/script>)(<\/div>)*<\/body><\/html>/, // optional `</div>`: appears in next.js 4.2.1 but not in 4.3.0-canary.1
    ],
    to: [
    '',
  3. mrienstra created this gist Jan 12, 2018.
    49 changes: 49 additions & 0 deletions export_postprocess.js
    Original 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);
    });