From 1c703aa80f49d55fad22a3e8bdb38ebf91591bc6 Mon Sep 17 00:00:00 2001 From: cenfun Date: Wed, 18 Dec 2024 08:43:35 +0800 Subject: [PATCH] fixed sourcemap file JSON error (#97) --- CHANGELOG.md | 3 +++ lib/converter/collect-source-maps.js | 4 ++-- lib/platform/share.js | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac28141c..439e04c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Changelog +- 2.11.5 + - fixed sourcemap file JSON error (#97) + - 2.11.4 - fixed encoded source url issue - fixed filter for encoded url diff --git a/lib/converter/collect-source-maps.js b/lib/converter/collect-source-maps.js index b9ce1a44..28a8eef2 100644 --- a/lib/converter/collect-source-maps.js +++ b/lib/converter/collect-source-maps.js @@ -17,7 +17,7 @@ const loadSourceMap = async (url = '') => { Util.logDebug(EC.red(`failed to load sourcemap ${p}`)); return; } - return JSON.parse(content); + return Util.jsonParse(content); } const [err, res] = await Util.request(url); @@ -31,7 +31,7 @@ const loadSourceMap = async (url = '') => { // could be string not json, if Content-Type is application/octet-stream if (typeof content === 'string') { - return JSON.parse(content); + return Util.jsonParse(content); } return content; diff --git a/lib/platform/share.js b/lib/platform/share.js index a0e17611..ef5fb6e4 100644 --- a/lib/platform/share.js +++ b/lib/platform/share.js @@ -402,6 +402,30 @@ const Util = { return string.charAt(0).toUpperCase() + string.slice(1); }, + jsonParse: function(str) { + + if (typeof str !== 'string') { + return null; + } + + if (!str) { + return null; + } + + let json = null; + + // remove BOM \ufeff + // str = str.replace(/^\uFEFF/, ''); + + try { + json = JSON.parse(str); + } catch (e) { + // console.log(e); + } + + return json; + }, + // ============================================================================= // formatter