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

Commit cca6cc2

Browse files
committed
esm: add experimental .json support to loader
With the new flag `--experimental-json-modules` it is now possible to import .json files. It piggy backs on the current cjs loader implementation, so it only exports a default. This is a bit of a hack, and it should potentially have it's own loader, especially if we change the cjs loader at all. The behavior for .json in the cjs loader matches the current planned behavior if json modules were to be standardized, specifically that a .json module only exports a default. Refs: nodejs/modules#255 Refs: whatwg/html#4315 Refs: WICG/webcomponents#770
1 parent a4955e2 commit cca6cc2

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/internal/modules/esm/default_resolve.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { realpathSync } = require('fs');
77
const { getOptionValue } = require('internal/options');
88
const preserveSymlinks = getOptionValue('--preserve-symlinks');
99
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
10+
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
1011
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
1112
const { resolve: moduleWrapResolve } = internalBinding('module_wrap');
1213
const { pathToFileURL, fileURLToPath } = require('internal/url');
@@ -25,11 +26,20 @@ const legacyExtensionFormatMap = {
2526
'__proto__': null,
2627
'.cjs': 'cjs',
2728
'.js': 'cjs',
28-
'.json': 'cjs',
2929
'.mjs': 'esm',
3030
'.node': 'cjs'
3131
};
3232

33+
if (experimentalJsonModules) {
34+
// This is a total hack
35+
Object.assign(extensionFormatMap, {
36+
'.json': 'cjs'
37+
});
38+
Object.assign(legacyExtensionFormatMap, {
39+
'.json': 'cjs'
40+
});
41+
}
42+
3343
function resolve(specifier, parentURL) {
3444
if (NativeModule.canBeRequiredByUsers(specifier)) {
3545
return {

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ DebugOptionsParser::DebugOptionsParser() {
160160
}
161161

162162
EnvironmentOptionsParser::EnvironmentOptionsParser() {
163+
AddOption("--experimental-json-modules",
164+
"experimental JSON interop support for the ES Module loader",
165+
&EnvironmentOptions::experimental_json_modules,
166+
kAllowedInEnvironment);
163167
AddOption("--experimental-modules",
164168
"experimental ES Module support and caching modules",
165169
&EnvironmentOptions::experimental_modules,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class DebugOptions : public Options {
8585
class EnvironmentOptions : public Options {
8686
public:
8787
bool abort_on_uncaught_exception = false;
88+
bool experimental_json_modules = false;
8889
bool experimental_modules = false;
8990
std::string module_type;
9091
std::string experimental_policy;

0 commit comments

Comments
 (0)