From d178e7543ef61172582f849b9be6bf3eaca8bae3 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Tue, 5 Mar 2019 20:31:06 -0500 Subject: [PATCH] esm: store json modules in CJS cache --- lib/internal/modules/esm/translators.js | 14 ++++++++++++++ test/es-module/test-esm-json-cache.mjs | 13 +++++++++++++ test/fixtures/es-modules/json-cache/another.cjs | 7 +++++++ test/fixtures/es-modules/json-cache/mod.cjs | 7 +++++++ test/fixtures/es-modules/json-cache/test.json | 5 +++++ 5 files changed, 46 insertions(+) create mode 100644 test/es-module/test-esm-json-cache.mjs create mode 100644 test/fixtures/es-modules/json-cache/another.cjs create mode 100644 test/fixtures/es-modules/json-cache/mod.cjs create mode 100644 test/fixtures/es-modules/json-cache/test.json diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 8873e84316..5ddddfa941 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -102,11 +102,25 @@ translators.set('json', async (url) => { debug(`Translating JSONModule ${url}`); debug(`Loading JSONModule ${url}`); const pathname = fileURLToPath(url); + const modulePath = isWindows ? + StringReplace(pathname, winSepRegEx, '\\') : pathname; + const module = CJSModule._cache[modulePath]; + if (module && module.loaded) { + const exports = module.exports; + return createDynamicModule(['default'], url, (reflect) => { + reflect.exports.default.set(exports); + }); + } const content = await readFileAsync(pathname, 'utf-8'); 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; diff --git a/test/es-module/test-esm-json-cache.mjs b/test/es-module/test-esm-json-cache.mjs new file mode 100644 index 0000000000..53a5123f09 --- /dev/null +++ b/test/es-module/test-esm-json-cache.mjs @@ -0,0 +1,13 @@ +// Flags: --experimental-modules --experimental-json-modules +/* eslint-disable node-core/required-modules */ +import '../common/index.mjs'; +import {strictEqual} from 'assert'; + +import mod from '../fixtures/es-modules/json-cache/mod.cjs'; +import another from '../fixtures/es-modules/json-cache/another.cjs'; +import test from '../fixtures/es-modules/json-cache/test.json'; + + +strictEqual(mod.one, 1); +strictEqual(another.one, 'zalgo'); +strictEqual(test.one, 'it comes'); diff --git a/test/fixtures/es-modules/json-cache/another.cjs b/test/fixtures/es-modules/json-cache/another.cjs new file mode 100644 index 0000000000..8c8e9f1c0f --- /dev/null +++ b/test/fixtures/es-modules/json-cache/another.cjs @@ -0,0 +1,7 @@ +const test = require('./test.json'); + +module.exports = { + ...test +}; + +test.one = 'it comes'; diff --git a/test/fixtures/es-modules/json-cache/mod.cjs b/test/fixtures/es-modules/json-cache/mod.cjs new file mode 100644 index 0000000000..1af7ae6a6f --- /dev/null +++ b/test/fixtures/es-modules/json-cache/mod.cjs @@ -0,0 +1,7 @@ +const test = require('./test.json'); + +module.exports = { + ...test +} + +test.one = 'zalgo'; diff --git a/test/fixtures/es-modules/json-cache/test.json b/test/fixtures/es-modules/json-cache/test.json new file mode 100644 index 0000000000..120cbb2840 --- /dev/null +++ b/test/fixtures/es-modules/json-cache/test.json @@ -0,0 +1,5 @@ +{ + "one": 1, + "two": 2, + "three": 3 +}