Skip to content

Commit

Permalink
Allow json modules to be resolved in Node >=17.5.0 without flag (#1665)
Browse files Browse the repository at this point in the history
* 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 <cspotcode@gmail.com>
  • Loading branch information
Jamesernator and cspotcode committed Mar 2, 2022
1 parent 30f03e1 commit 20cbbf5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
6 changes: 5 additions & 1 deletion dist-raw/node-esm-default-get-format.js
Expand Up @@ -11,7 +11,11 @@ const {
const { extname } = require('path');
const { getOptionValue } = require('./node-options');

const experimentalJsonModules = getOptionValue('--experimental-json-modules');
const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(s => parseInt(s, 10));
const experimentalJsonModules =
nodeMajor > 17
|| (nodeMajor === 17 && nodeMinor >= 5)
|| getOptionValue('--experimental-json-modules');
const experimentalSpeciferResolution =
getOptionValue('--experimental-specifier-resolution');
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -171,7 +171,7 @@
"singleQuote": true
},
"volta": {
"node": "16.9.1",
"node": "17.5.0",
"npm": "6.14.15"
}
}
43 changes: 32 additions & 11 deletions src/test/esm-loader.spec.ts
Expand Up @@ -261,17 +261,38 @@ test.suite('esm', (test) => {
test.suite('supports import assertions', (test) => {
test.runIf(nodeSupportsImportAssertions);

test('Can import JSON using the appropriate flag and assertion', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-json-modules ./importJson.ts`,
{
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
}
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
'A fuchsia car has 2 seats and the doors are open.\nDone!'
);
test.suite('node >=17.5.0', (test) => {
test.runIf(semver.gte(process.version, '17.5.0'));

test('Can import JSON modules with appropriate assertion', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./importJson.ts`,
{
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
}
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
'A fuchsia car has 2 seats and the doors are open.\nDone!'
);
});
});

test.suite('node <17.5.0', (test) => {
test.runIf(semver.lt(process.version, '17.5.0'));

test('Can import JSON using the appropriate flag and assertion', async (t) => {
const { err, stdout } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} --experimental-json-modules ./importJson.ts`,
{
cwd: resolve(TEST_DIR, 'esm-import-assertions'),
}
);
expect(err).toBe(null);
expect(stdout.trim()).toBe(
'A fuchsia car has 2 seats and the doors are open.\nDone!'
);
});
});
});

Expand Down

0 comments on commit 20cbbf5

Please sign in to comment.