diff --git a/local-cli/bundle/output/bundle.js b/local-cli/bundle/output/bundle.js index 2cbb7be08..54eab0587 100644 --- a/local-cli/bundle/output/bundle.js +++ b/local-cli/bundle/output/bundle.js @@ -9,7 +9,7 @@ 'use strict'; const Promise = require('promise'); -const hash = require('./hash'); +const meta = require('./meta'); const writeFile = require('./writeFile'); function buildBundle(packagerClient, requestOptions) { @@ -37,15 +37,20 @@ function saveBundleAndMap(bundle, options, log) { log('Writing bundle output to:', bundleOutput); - const code = hash.appendToString(codeWithMap.code, encoding); + const {code} = codeWithMap; const writeBundle = writeFile(bundleOutput, code, encoding); - writeBundle.then(() => log('Done writing bundle output')); + const writeMetadata = writeFile( + bundleOutput + '.meta', + meta(code, encoding), + 'binary'); + Promise.all([writeBundle, writeMetadata]) + .then(() => log('Done writing bundle output')); if (sourcemapOutput) { log('Writing sourcemap output to:', sourcemapOutput); const writeMap = writeFile(sourcemapOutput, codeWithMap.map, null); writeMap.then(() => log('Done writing sourcemap output')); - return Promise.all([writeBundle, writeMap]); + return Promise.all([writeBundle, writeMetadata, writeMap]); } else { return writeBundle; } diff --git a/local-cli/bundle/output/hash.js b/local-cli/bundle/output/meta.js similarity index 56% rename from local-cli/bundle/output/hash.js rename to local-cli/bundle/output/meta.js index c02c95746..d7f837a0e 100644 --- a/local-cli/bundle/output/hash.js +++ b/local-cli/bundle/output/meta.js @@ -10,14 +10,23 @@ const crypto = require('crypto'); -const createHash = () => crypto.createHash('sha1'); const isUTF8 = encoding => /^utf-?8$/i.test(encoding); -exports.appendToString = (string, encoding) => { - const hash = createHash(); - hash.update(string, encoding); - encoding = tryAsciiPromotion(string, encoding); - return string + formatSignature(encoding, hash); +const constantFor = encoding => + /^ascii$/i.test(encoding) ? 1 : + isUTF8(encoding) ? 2 : + /^(?:utf-?16(?:le)?|ucs-?2)$/ ? 3 : 0; + +module.exports = function(code, encoding) { + const hash = crypto.createHash('sha1'); + hash.update(code, encoding); + const digest = hash.digest('binary'); + const signature = Buffer(digest.length + 1); + signature.write(digest, 'binary'); + signature.writeUInt8( + constantFor(tryAsciiPromotion(code, encoding)), + signature.length - 1); + return signature; }; function tryAsciiPromotion(string, encoding) { @@ -27,7 +36,3 @@ function tryAsciiPromotion(string, encoding) { } return 'ascii'; } - -function formatSignature(encoding, hash) { - return `/*${encoding}:${hash.digest('hex')}*/`; -}