From 91e5b859552852c68ad6fa7d4e46a6ed83ac67c2 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 22 Mar 2021 19:41:23 -0700 Subject: [PATCH] Excluding sourcemaps comment in production builds (#10695) * Excluding sourcemaps comment in production builds Fixes MetaMask/metamask-extension#7077 * Fix source map explorer script The source map explorer script now re-adds the source map comment to each file to ensure the source map visualization still works. Each module with a sourcemap is copied to a temporary directory along with the module it corresponds to, and from there it's passed into `source-map-explorer`. This should ensure the resulting visualization matches what it was before. Everything has been moved inside of functions to generally improve readability, and to allow the use of local variables. Co-authored-by: Mark Stacey --- development/build/scripts.js | 4 +++- development/source-map-explorer.sh | 35 +++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/development/build/scripts.js b/development/build/scripts.js index f3dcc8de0..c91794ec4 100644 --- a/development/build/scripts.js +++ b/development/build/scripts.js @@ -254,7 +254,9 @@ function createScriptTasks({ browserPlatforms, livereload }) { // note: sourcemaps call arity is important buildPipeline.push(sourcemaps.write()); } else { - buildPipeline.push(sourcemaps.write('../sourcemaps')); + buildPipeline.push( + sourcemaps.write('../sourcemaps', { addComment: false }), + ); } // write completed bundles diff --git a/development/source-map-explorer.sh b/development/source-map-explorer.sh index a4130d0f0..ae9563f9f 100755 --- a/development/source-map-explorer.sh +++ b/development/source-map-explorer.sh @@ -5,11 +5,30 @@ set -e set -u set -o pipefail -mkdir -p build-artifacts/source-map-explorer -yarn source-map-explorer dist/chrome/inpage.js --html build-artifacts/source-map-explorer/inpage.html -yarn source-map-explorer dist/chrome/contentscript.js --html build-artifacts/source-map-explorer/contentscript.html -yarn source-map-explorer dist/chrome/background.js --html build-artifacts/source-map-explorer/background.html -yarn source-map-explorer dist/chrome/bg-libs.js --html build-artifacts/source-map-explorer/bg-libs.html -yarn source-map-explorer dist/chrome/ui.js --html build-artifacts/source-map-explorer/ui.html -yarn source-map-explorer dist/chrome/ui-libs.js --html build-artifacts/source-map-explorer/ui-libs.html -yarn source-map-explorer dist/chrome/phishing-detect.js --html build-artifacts/source-map-explorer/phishing-detect.html +function generate_sourcemap() { + local temp_dir="${1}"; shift + local module_name="${1}"; shift + + cp "dist/chrome/${module_name}.js" "${temp_dir}/" + cp "dist/sourcemaps/${module_name}.js.map" "${temp_dir}/" + printf '//# sourceMappingURL=%s.js.map' "${module_name}" >> "${temp_dir}/${module_name}.js" + yarn source-map-explorer "${temp_dir}/${module_name}.js" "${temp_dir}/${module_name}.js.map" --html "build-artifacts/source-map-explorer/${module_name}.html" +} + +function main() { + mkdir -p build-artifacts/source-map-explorer + + local temp_dir + temp_dir="$(mktemp -d)" + + for file in dist/sourcemaps/*.js.map; do + [[ -e $file ]] || (echo 'Failed to find any JavaScript modules' && exit 1) + local filename + filename="$(basename "${file}")" + local module_name + module_name="${filename%.js.map}" + generate_sourcemap "${temp_dir}" "${module_name}" + done +} + +main