Skip to content

Commit df218ce

Browse files
guybedfordBridgeAR
authored andcommitted
Revert "esm: remove experimental status from JSON modules"
This reverts commit ec8776d. PR-URL: #29754 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent b76a2e5 commit df218ce

File tree

8 files changed

+44
-8
lines changed

8 files changed

+44
-8
lines changed

doc/api/cli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ added: v12.7.0
165165

166166
Enable experimental resolution using the `exports` field in `package.json`.
167167

168+
### `--experimental-json-modules`
169+
<!-- YAML
170+
added: v12.9.0
171+
-->
172+
173+
Enable experimental JSON support for the ES Module loader.
174+
168175
### `--experimental-modules`
169176
<!-- YAML
170177
added: v8.5.0
@@ -1019,6 +1026,7 @@ Node.js options that are allowed are:
10191026
* `--enable-source-maps`
10201027
* `--es-module-specifier-resolution`
10211028
* `--experimental-exports`
1029+
* `--experimental-json-modules`
10221030
* `--experimental-loader`
10231031
* `--experimental-modules`
10241032
* `--experimental-policy`

doc/api/esm.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,16 @@ syncBuiltinESMExports();
587587
fs.readFileSync === readFileSync;
588588
```
589589
590-
## JSON Modules
590+
## Experimental JSON Modules
591591
592-
JSON modules follow the [WHATWG JSON modules specification][].
592+
Currently importing JSON modules are only supported in the `commonjs` mode
593+
and are loaded using the CJS loader. [WHATWG JSON modules specification][] are
594+
still being standardized, and are experimentally supported by including the
595+
additional flag `--experimental-json-modules` when running Node.js.
593596
594-
The imported JSON only exposes a `default`. There is no
597+
When the `--experimental-json-modules` flag is included both the
598+
`commonjs` and `module` mode will use the new experimental JSON
599+
loader. The imported JSON only exposes a `default`, there is no
595600
support for named exports. A cache entry is created in the CommonJS
596601
cache, to avoid duplication. The same object will be returned in
597602
CommonJS if the JSON module has already been imported from the
@@ -604,6 +609,14 @@ Assuming an `index.mjs` with
604609
import packageConfig from './package.json';
605610
```
606611
612+
The `--experimental-json-modules` flag is needed for the module
613+
to work.
614+
615+
```bash
616+
node --experimental-modules index.mjs # fails
617+
node --experimental-modules --experimental-json-modules index.mjs # works
618+
```
619+
607620
## Experimental Wasm Modules
608621
609622
Importing Web Assembly modules is supported under the

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Requires Node.js to be built with
108108
.It Fl -es-module-specifier-resolution
109109
Select extension resolution algorithm for ES Modules; either 'explicit' (default) or 'node'
110110
.
111+
.It Fl -experimental-json-modules
112+
Enable experimental JSON interop support for the ES Module loader.
113+
.
111114
.It Fl -experimental-modules
112115
Enable experimental ES module support and caching modules.
113116
.

lib/internal/modules/esm/default_resolve.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { getOptionValue } = require('internal/options');
88

99
const preserveSymlinks = getOptionValue('--preserve-symlinks');
1010
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
11+
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
1112
const typeFlag = getOptionValue('--input-type');
1213
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
1314
const { resolve: moduleWrapResolve,
@@ -28,22 +29,24 @@ const extensionFormatMap = {
2829
'__proto__': null,
2930
'.cjs': 'commonjs',
3031
'.js': 'module',
31-
'.json': 'json',
3232
'.mjs': 'module'
3333
};
3434

3535
const legacyExtensionFormatMap = {
3636
'__proto__': null,
3737
'.cjs': 'commonjs',
3838
'.js': 'commonjs',
39-
'.json': 'json',
39+
'.json': 'commonjs',
4040
'.mjs': 'module',
4141
'.node': 'commonjs'
4242
};
4343

4444
if (experimentalWasmModules)
4545
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
4646

47+
if (experimentalJsonModules)
48+
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
49+
4750
function resolve(specifier, parentURL) {
4851
try {
4952
const parsed = new URL(specifier);

src/node_options.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
135135
}
136136
}
137137

138+
if (experimental_json_modules && !experimental_modules) {
139+
errors->push_back("--experimental-json-modules requires "
140+
"--experimental-modules be enabled");
141+
}
142+
138143
if (experimental_wasm_modules && !experimental_modules) {
139144
errors->push_back("--experimental-wasm-modules requires "
140145
"--experimental-modules be enabled");
@@ -320,6 +325,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
320325
"experimental support for exports in package.json",
321326
&EnvironmentOptions::experimental_exports,
322327
kAllowedInEnvironment);
328+
AddOption("--experimental-json-modules",
329+
"experimental JSON interop support for the ES Module loader",
330+
&EnvironmentOptions::experimental_json_modules,
331+
kAllowedInEnvironment);
323332
AddOption("--experimental-loader",
324333
"(with --experimental-modules) use the specified file as a "
325334
"custom loader",

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class EnvironmentOptions : public Options {
102102
bool abort_on_uncaught_exception = false;
103103
bool enable_source_maps = false;
104104
bool experimental_exports = false;
105+
bool experimental_json_modules = false;
105106
bool experimental_modules = false;
106107
std::string es_module_specifier_resolution;
107108
bool experimental_wasm_modules = false;

test/es-module/test-esm-json-cache.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-modules
1+
// Flags: --experimental-modules --experimental-json-modules
22
import '../common/index.mjs';
33

44
import { strictEqual, deepStrictEqual } from 'assert';

test/es-module/test-esm-json.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Flags: --experimental-modules
2-
1+
// Flags: --experimental-modules --experimental-json-modules
32
import '../common/index.mjs';
43
import { strictEqual } from 'assert';
54

0 commit comments

Comments
 (0)