Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit d178e75

Browse files
committed
esm: store json modules in CJS cache
1 parent 842ce61 commit d178e75

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

lib/internal/modules/esm/translators.js

+14
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,25 @@ translators.set('json', async (url) => {
102102
debug(`Translating JSONModule ${url}`);
103103
debug(`Loading JSONModule ${url}`);
104104
const pathname = fileURLToPath(url);
105+
const modulePath = isWindows ?
106+
StringReplace(pathname, winSepRegEx, '\\') : pathname;
107+
const module = CJSModule._cache[modulePath];
108+
if (module && module.loaded) {
109+
const exports = module.exports;
110+
return createDynamicModule(['default'], url, (reflect) => {
111+
reflect.exports.default.set(exports);
112+
});
113+
}
105114
const content = await readFileAsync(pathname, 'utf-8');
106115
return createDynamicModule(['default'], url, (reflect) => {
107116
debug(`Parsing JSONModule ${url}`);
108117
try {
109118
const exports = JsonParse(stripBOM(content));
119+
const module = {
120+
exports,
121+
loaded: true
122+
};
123+
CJSModule._cache[modulePath] = module;
110124
reflect.exports.default.set(exports);
111125
} catch (err) {
112126
err.message = pathname + ': ' + err.message;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Flags: --experimental-modules --experimental-json-modules
2+
/* eslint-disable node-core/required-modules */
3+
import '../common/index.mjs';
4+
import {strictEqual} from 'assert';
5+
6+
import mod from '../fixtures/es-modules/json-cache/mod.cjs';
7+
import another from '../fixtures/es-modules/json-cache/another.cjs';
8+
import test from '../fixtures/es-modules/json-cache/test.json';
9+
10+
11+
strictEqual(mod.one, 1);
12+
strictEqual(another.one, 'zalgo');
13+
strictEqual(test.one, 'it comes');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('./test.json');
2+
3+
module.exports = {
4+
...test
5+
};
6+
7+
test.one = 'it comes';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('./test.json');
2+
3+
module.exports = {
4+
...test
5+
}
6+
7+
test.one = 'zalgo';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"one": 1,
3+
"two": 2,
4+
"three": 3
5+
}

0 commit comments

Comments
 (0)