Skip to content

Commit 20cbbf5

Browse files
Allow json modules to be resolved in Node >=17.5.0 without flag (#1665)
* Enable JSON modules by default in Node >=17.5.0 * Added tests for experimental json modules no longer requiring flag * bump node version used for development; just more convenient this way Co-authored-by: Andrew Bradley <[email protected]>
1 parent 30f03e1 commit 20cbbf5

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

dist-raw/node-esm-default-get-format.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ const {
1111
const { extname } = require('path');
1212
const { getOptionValue } = require('./node-options');
1313

14-
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
14+
const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(s => parseInt(s, 10));
15+
const experimentalJsonModules =
16+
nodeMajor > 17
17+
|| (nodeMajor === 17 && nodeMinor >= 5)
18+
|| getOptionValue('--experimental-json-modules');
1519
const experimentalSpeciferResolution =
1620
getOptionValue('--experimental-specifier-resolution');
1721
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
"singleQuote": true
172172
},
173173
"volta": {
174-
"node": "16.9.1",
174+
"node": "17.5.0",
175175
"npm": "6.14.15"
176176
}
177177
}

src/test/esm-loader.spec.ts

+32-11
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,38 @@ test.suite('esm', (test) => {
261261
test.suite('supports import assertions', (test) => {
262262
test.runIf(nodeSupportsImportAssertions);
263263

264-
test('Can import JSON using the appropriate flag and assertion', async (t) => {
265-
const { err, stdout } = await exec(
266-
`${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-json-modules ./importJson.ts`,
267-
{
268-
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
269-
}
270-
);
271-
expect(err).toBe(null);
272-
expect(stdout.trim()).toBe(
273-
'A fuchsia car has 2 seats and the doors are open.\nDone!'
274-
);
264+
test.suite('node >=17.5.0', (test) => {
265+
test.runIf(semver.gte(process.version, '17.5.0'));
266+
267+
test('Can import JSON modules with appropriate assertion', async (t) => {
268+
const { err, stdout } = await exec(
269+
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./importJson.ts`,
270+
{
271+
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
272+
}
273+
);
274+
expect(err).toBe(null);
275+
expect(stdout.trim()).toBe(
276+
'A fuchsia car has 2 seats and the doors are open.\nDone!'
277+
);
278+
});
279+
});
280+
281+
test.suite('node <17.5.0', (test) => {
282+
test.runIf(semver.lt(process.version, '17.5.0'));
283+
284+
test('Can import JSON using the appropriate flag and assertion', async (t) => {
285+
const { err, stdout } = await exec(
286+
`${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-json-modules ./importJson.ts`,
287+
{
288+
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
289+
}
290+
);
291+
expect(err).toBe(null);
292+
expect(stdout.trim()).toBe(
293+
'A fuchsia car has 2 seats and the doors are open.\nDone!'
294+
);
295+
});
275296
});
276297
});
277298

0 commit comments

Comments
 (0)