Skip to content

Instantly share code, notes, and snippets.

@jdalton
Last active July 3, 2024 02:00
Show Gist options
  • Save jdalton/57ea855c036f28be6235884d96f69b8d to your computer and use it in GitHub Desktop.
Save jdalton/57ea855c036f28be6235884d96f69b8d to your computer and use it in GitHub Desktop.

Revisions

  1. jdalton created this gist Mar 1, 2017.
    19 changes: 19 additions & 0 deletions gz.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    'use strict'

    const fs = require('fs')
    const path = require('path')
    const zlib = require('zlib')

    require.extensions['.gz'] =
    require.extensions['.js.gz'] = (module, filename) => {
    if (path.extname(filename.slice(0, -3)) === '.js') {
    let content = zlib.unzipSync(fs.readFileSync(filename)).toString()
    content = content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content
    module._compile(content, filename)
    }
    }

    require.extensions['.js'] = ((func) => (module, filename) => {
    const gz = filename + '.gz'
    return fs.existsSync(gz) ? require.extensions['.gz'](module, gz) : func(module, filename)
    })(require.extensions['.js'])