Skip to content

Commit e5d7ba5

Browse files
committed
json / wasm import assertion support.
1 parent d5bebe3 commit e5d7ba5

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

dist-raw/node-options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ function parseArgv(argv) {
3434
'--es-module-specifier-resolution': '--experimental-specifier-resolution',
3535
'--experimental-policy': String,
3636
'--conditions': [String],
37-
'--pending-deprecation': Boolean
37+
'--pending-deprecation': Boolean,
38+
'--experimental-json-modules': Boolean,
39+
'--experimental-wasm-modules': Boolean,
3840
}, {
3941
argv,
4042
permissive: true

src/esm.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ export namespace NodeLoaderHooksAPI2 {
6767
) => Promise<{ url: string }>;
6868
export type LoadHook = (
6969
url: string,
70-
context: { format: NodeLoaderHooksFormat | null | undefined },
70+
context: {
71+
format: NodeLoaderHooksFormat | null | undefined;
72+
importAssertions?: NodeImportAssertions;
73+
},
7174
defaultLoad: NodeLoaderHooksAPI2['load']
7275
) => Promise<{
7376
format: NodeLoaderHooksFormat;
@@ -83,6 +86,10 @@ export type NodeLoaderHooksFormat =
8386
| 'module'
8487
| 'wasm';
8588

89+
export type NodeImportAssertions = {
90+
type: 'json' | 'wasm';
91+
};
92+
8693
/** @internal */
8794
export function registerAndCreateEsmHooks(opts?: RegisterOptions) {
8895
// Automatically performs registration just like `-r ts-node/register`
@@ -159,7 +166,10 @@ export function createEsmHooks(tsNodeService: Service) {
159166
// `load` from new loader hook API (See description at the top of this file)
160167
async function load(
161168
url: string,
162-
context: { format: NodeLoaderHooksFormat | null | undefined },
169+
context: {
170+
format: NodeLoaderHooksFormat | null | undefined;
171+
importAssertions?: NodeImportAssertions;
172+
},
163173
defaultLoad: typeof load
164174
): Promise<{
165175
format: NodeLoaderHooksFormat;
@@ -176,7 +186,10 @@ export function createEsmHooks(tsNodeService: Service) {
176186
// Call the new defaultLoad() to get the source
177187
const { source: rawSource } = await defaultLoad(
178188
url,
179-
{ format },
189+
{
190+
format,
191+
importAssertions: context.importAssertions,
192+
},
180193
defaultLoad
181194
);
182195

tests/esm-import-assertions/car.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"color": "fuchsia",
3+
"doors": "open",
4+
"seats": 2
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import carData from './car.json' assert { type: 'json' };
2+
3+
if (carData.color !== 'fuchsia') throw new Error('failed to import json');
4+
5+
const { default: dynamicCarData } = await import('./car.json', {
6+
assert: { type: 'json' },
7+
});
8+
9+
if (dynamicCarData.doors !== 'open')
10+
throw new Error('failed to dynamically import json');
11+
12+
console.log(
13+
`A ${carData.color} car has ${carData.seats} seats and the doors are ${dynamicCarData.doors}.`
14+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"target": "ESNext",
5+
"resolveJsonModule": true,
6+
"allowJs": true,
7+
"moduleResolution": "node",
8+
"allowSyntheticDefaultImports": true
9+
}
10+
}

0 commit comments

Comments
 (0)