Skip to content

Commit

Permalink
json / wasm import assertion support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokute authored and cspotcode committed Dec 24, 2021
1 parent 3a2848c commit 60c7d59
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion dist-raw/node-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function parseArgv(argv) {
'--es-module-specifier-resolution': '--experimental-specifier-resolution',
'--experimental-policy': String,
'--conditions': [String],
'--pending-deprecation': Boolean
'--pending-deprecation': Boolean,
'--experimental-json-modules': Boolean,
'--experimental-wasm-modules': Boolean,
}, {
argv,
permissive: true
Expand Down
19 changes: 16 additions & 3 deletions src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export namespace NodeLoaderHooksAPI2 {
) => Promise<{ url: string }>;
export type LoadHook = (
url: string,
context: { format: NodeLoaderHooksFormat | null | undefined },
context: {
format: NodeLoaderHooksFormat | null | undefined;
importAssertions?: NodeImportAssertions;
},
defaultLoad: NodeLoaderHooksAPI2['load']
) => Promise<{
format: NodeLoaderHooksFormat;
Expand All @@ -83,6 +86,10 @@ export type NodeLoaderHooksFormat =
| 'module'
| 'wasm';

export type NodeImportAssertions = {
type: 'json' | 'wasm';
};

/** @internal */
export function registerAndCreateEsmHooks(opts?: RegisterOptions) {
// Automatically performs registration just like `-r ts-node/register`
Expand Down Expand Up @@ -159,7 +166,10 @@ export function createEsmHooks(tsNodeService: Service) {
// `load` from new loader hook API (See description at the top of this file)
async function load(
url: string,
context: { format: NodeLoaderHooksFormat | null | undefined },
context: {
format: NodeLoaderHooksFormat | null | undefined;
importAssertions?: NodeImportAssertions;
},
defaultLoad: typeof load
): Promise<{
format: NodeLoaderHooksFormat;
Expand All @@ -176,7 +186,10 @@ export function createEsmHooks(tsNodeService: Service) {
// Call the new defaultLoad() to get the source
const { source: rawSource } = await defaultLoad(
url,
{ format },
{
format,
importAssertions: context.importAssertions,
},
defaultLoad
);

Expand Down
5 changes: 5 additions & 0 deletions tests/esm-import-assertions/car.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"color": "fuchsia",
"doors": "open",
"seats": 2
}
14 changes: 14 additions & 0 deletions tests/esm-import-assertions/importJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import carData from './car.json' assert { type: 'json' };

if (carData.color !== 'fuchsia') throw new Error('failed to import json');

const { default: dynamicCarData } = await import('./car.json', {
assert: { type: 'json' },
});

if (dynamicCarData.doors !== 'open')
throw new Error('failed to dynamically import json');

console.log(
`A ${carData.color} car has ${carData.seats} seats and the doors are ${dynamicCarData.doors}.`
);
3 changes: 3 additions & 0 deletions tests/esm-import-assertions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
10 changes: 10 additions & 0 deletions tests/esm-import-assertions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"resolveJsonModule": true,
"allowJs": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}

0 comments on commit 60c7d59

Please sign in to comment.