Created
March 18, 2019 11:19
-
-
Save ofhouse/8ab6d377275e839965b64842a274fb4d to your computer and use it in GitHub Desktop.
Patch for the `encoding` npm-package to use with webpack. (From: https://github.com/andris9/encoding/issues/18#issuecomment-319721150)
This file contains 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 characters
// IIFE | |
(function() { | |
'use strict'; | |
// node | |
var fs = require('fs'); | |
var path = require('path'); | |
// Patch encoding module due to iconv issues -> make it use iconv-lite | |
(function() { | |
var PATCH_VERSION = '0.1.12'; | |
var PATCH_MODULE = 'encoding'; | |
var PATCH_REASON = 'Use iconv-lite instead of iconv, helpful for webpack bundling'; | |
console.log('patching `%s`(%s) module', PATCH_MODULE, PATCH_VERSION); | |
var pathToModule = path.dirname(require.resolve(PATCH_MODULE + '/package.json')); | |
var pathToModulePackage = path.join(pathToModule, 'package.json'); | |
var pathToModulePatchedFile1 = path.join(pathToModule, 'lib/iconv-loader.js'); | |
var pathToModulePatchedFile2 = path.join(pathToModule, 'lib/encoding.js'); | |
var moduleInfo = require(pathToModulePackage); | |
if (moduleInfo.version !== PATCH_VERSION) { | |
console.error( | |
'patching `encoding` failed - expected `%s` but detected `%s`', | |
PATCH_VERSION, | |
moduleInfo.version | |
); | |
process.exit(1); | |
} | |
var contents; | |
if (fs.existsSync(pathToModulePatchedFile1)) { | |
contents = [ | |
'\'use strict\';', | |
'module.exports = require(\'iconv-lite\');', | |
'', | |
].join('\n'); | |
fs.writeFileSync(pathToModulePatchedFile1, contents); | |
} else { | |
console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule); | |
process.exit(1); | |
} | |
if (fs.existsSync(pathToModulePatchedFile2)) { | |
contents = fs.readFileSync(pathToModulePatchedFile2).toString(); | |
contents = contents.replace('console.error(E);',''); | |
fs.writeFileSync(pathToModulePatchedFile2, contents); | |
} else { | |
console.error('patching `%s` failed because the file does not exist in', PATCH_MODULE, pathToModule); | |
process.exit(1); | |
} | |
console.log('patching `%s`, reason: `%s` - completed', PATCH_MODULE, PATCH_REASON); | |
})(); | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment