From 5c81e3dd675818681a3ed6bc99c5a5b467d41f75 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Wed, 6 Mar 2019 11:58:25 -0500 Subject: [PATCH] fixup: move parse to earlier phase --- lib/internal/modules/esm/translators.js | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 5ddddfa941..52eaa95aaa 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -104,7 +104,7 @@ translators.set('json', async (url) => { const pathname = fileURLToPath(url); const modulePath = isWindows ? StringReplace(pathname, winSepRegEx, '\\') : pathname; - const module = CJSModule._cache[modulePath]; + let module = CJSModule._cache[modulePath]; if (module && module.loaded) { const exports = module.exports; return createDynamicModule(['default'], url, (reflect) => { @@ -112,19 +112,20 @@ translators.set('json', async (url) => { }); } const content = await readFileAsync(pathname, 'utf-8'); + try { + const exports = JsonParse(stripBOM(content)); + module = { + exports, + loaded: true + }; + } catch (err) { + err.message = pathname + ': ' + err.message; + throw err; + } + CJSModule._cache[modulePath] = module; return createDynamicModule(['default'], url, (reflect) => { debug(`Parsing JSONModule ${url}`); - try { - const exports = JsonParse(stripBOM(content)); - const module = { - exports, - loaded: true - }; - CJSModule._cache[modulePath] = module; - reflect.exports.default.set(exports); - } catch (err) { - err.message = pathname + ': ' + err.message; - throw err; - } + reflect.exports.default.set(module.exports); + }); });