Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JSON imports in NodeJS 16.15 #1792

Merged
merged 5 commits into from Jun 17, 2022
Merged

Conversation

queengooborg
Copy link
Contributor

This PR is a quick fix that enables JSON module support by default in NodeJS v16.15 < v17 as well as v17.5 and up. The version number was selected based upon prior manual testing to determine when JSON module support was enabled by default.

This change was tested by attempting to run a script that uses a JSON import and performing the same tweak directly in node_modules.

@cspotcode
Copy link
Collaborator

Thanks, seems simple enough. Was this mentioned in the nodejs 16.15 changelog? Would be nice to keep a link to that changelog somewhere, in case questions arise in the future.

@queengooborg
Copy link
Contributor Author

@codecov
Copy link

codecov bot commented Jun 9, 2022

Codecov Report

Merging #1792 (c98b685) into main (14323f9) will not change coverage.
The diff coverage is n/a.

Impacted Files Coverage Δ
dist-raw/node-internal-modules-esm-get_format.js 89.18% <ø> (ø)

@cspotcode
Copy link
Collaborator

Awesome, thanks!

We run our tests against a bunch of node versions to test stuff like this, and we conditionally check the node version in the tests to figure out what we should test.

Here is the test for JSON imports, where we check the node version. Depending on the check, we either pass --experimental-json-modules or we don't.

test.suite('supports import assertions', (test) => {
test.runIf(nodeSupportsImportAssertions);
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!'
);
});
});
});

Can you also update that version check in the tests? Most of the version checks are declared as boolean exports from helpers.ts, like this:

//#region version checks
export const nodeSupportsEsmHooks = semver.gte(process.version, '12.16.0');
export const nodeSupportsSpawningChildProcess = semver.gte(
process.version,
'12.17.0'
);
export const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
export const nodeSupportsImportAssertions = semver.gte(
process.version,
'17.1.0'
);
/** Supports tsconfig "extends" >= v3.2.0 */
export const tsSupportsTsconfigInheritanceViaNodePackages = semver.gte(
ts.version,
'3.2.0'
);
/** Supports --showConfig: >= v3.2.0 */
export const tsSupportsShowConfig = semver.gte(ts.version, '3.2.0');
//#endregion

Can we move this version check into that file? Can be named something like export const nodeSupportUnflaggedJsonImports =

@cspotcode
Copy link
Collaborator

Looks like the nodeSupportsImportAssertions boolean should also be updated to include node >= 16.14.0

https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V16.md#2022-02-08-version-16140-gallium-lts-danielleadams

export const nodeSupportsImportAssertions = semver.gte(
process.version,
'17.1.0'
);

@cspotcode
Copy link
Collaborator

Tests on node nightly are failing due to a node bug; you can safely ignore those test failures.

@queengooborg
Copy link
Contributor Author

Test updates completed -- I opted for the variable name nodeSupportsImportAssertionsTypeJson to match the syntax (assert {type: 'json'}) and open and preemptively opening the door for any other import assertion properties and values!

@queengooborg
Copy link
Contributor Author

Hey there! Just checking in to see if this needs anything more from my end, or if we're just waiting on review?

@cspotcode cspotcode added this to the 10.8.2 milestone Jun 17, 2022
@cspotcode cspotcode merged commit bf13086 into TypeStrong:main Jun 17, 2022
@cspotcode
Copy link
Collaborator

@queengooborg Merged and will go out in the next release. Thanks!

@queengooborg queengooborg deleted the patch-1 branch June 17, 2022 23:03
crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Jul 11, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | patch | [`10.8.1` -> `10.8.2`](https://renovatebot.com/diffs/npm/ts-node/10.8.1/10.8.2) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.8.2`](https://github.com/TypeStrong/ts-node/releases/tag/v10.8.2)

[Compare Source](TypeStrong/ts-node@v10.8.1...v10.8.2)

**Fixed**

-   Revert "Use file URL for source map paths" ([#&#8203;1821](TypeStrong/ts-node#1821)) [@&#8203;cspotcode](https://github.com/cspotcode)
    -   Fixes [#&#8203;1790](TypeStrong/ts-node#1790): ts-node 10.8.1 regression where `nyc` code coverage reports had incorrect paths
    -   Fixes [#&#8203;1797](TypeStrong/ts-node#1797): ts-node 10.8.1 regression where breakpoints did not hit in VSCode debugging
-   Allow JSON imports in node 16.15 and up ([#&#8203;1792](TypeStrong/ts-node#1792)) [@&#8203;queengooborg](https://github.com/queengooborg)
    -   JSON imports were already supported in v17.5 and up
    -   this change extends support to >=16.15.0,<17.0.0
    -   These version ranges match vanilla node's support for JSON imports

https://github.com/TypeStrong/ts-node/milestone/15?closed=1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1446
Reviewed-by: Epsilon_02 <epsilon_02@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants