Skip to content

Commit

Permalink
refactor: use SyntheticModule for json data URI import
Browse files Browse the repository at this point in the history
  • Loading branch information
tbossi committed Feb 16, 2022
1 parent a6ffa89 commit 67f7e63
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
8 changes: 4 additions & 4 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,17 @@ test('imports from "data:text/javascript" URI with invalid data fail', async ()
test('imports from "data:application/wasm" URI not supported', async () => {
await expect(
async () => await import('data:application/wasm,96cafe00babe'),
).rejects.toThrow('Unsupported MIME type');
).rejects.toThrow('WASM is currently not supported');
});

test('supports imports from "data:application/json" URI', async () => {
const data = await import('data:application/json,{foo: "bar"}');
expect(data.default).toStrictEqual({foo: 'bar'});
const data = await import('data:application/json,{"foo": "bar"}');
expect(data.default).toEqual({foo: 'bar'});
});

test('supports static "data:" URI import', async () => {
const module = await import('../staticDataImport.js');
expect(module.value()).toStrictEqual({bar: {obj: 456}, foo: '123'});
expect(module.value()).toEqual({bar: {obj: 456}, foo: '123'});
});

test('imports from "data:" URI is properly cached', async () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/native-esm/staticDataImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import bar from 'data:application/json,{obj: 456}';
import bar from 'data:application/json,{"obj": 456}';
import {foo} from 'data:text/javascript,export const foo = "123"';

export function value() {
Expand Down
59 changes: 34 additions & 25 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,33 +594,42 @@ export default class Runtime {
throw new Error(`Invalid data URI encoding: ${encoding}`);
}

let module;
if (mime === 'application/json') {
code = 'export default ' + code;
}

const module = new SourceTextModule(code, {
context,
identifier: specifier,
importModuleDynamically: async (
specifier: string,
referencingModule: VMModule,
) => {
invariant(
runtimeSupportsVmModules,
'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules',
);
const module = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context,
);
module = new SyntheticModule(
['default'],
function () {
const obj = JSON.parse(code);
// @ts-expect-error: TS doesn't know what `this` is
this.setExport('default', obj);
},
{context, identifier: specifier},
);
} else {
module = new SourceTextModule(code, {
context,
identifier: specifier,
importModuleDynamically: async (
specifier: string,
referencingModule: VMModule,
) => {
invariant(
runtimeSupportsVmModules,
'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules',
);
const module = await this.resolveModule(
specifier,
referencingModule.identifier,
referencingModule.context,
);

return this.linkAndEvaluateModule(module);
},
initializeImportMeta(meta: ImportMeta) {
meta.url = specifier;
},
});
return this.linkAndEvaluateModule(module);
},
initializeImportMeta(meta: ImportMeta) {
meta.url = specifier;
},
});
}

this._esmoduleRegistry.set(specifier, module);
return module;
Expand Down

0 comments on commit 67f7e63

Please sign in to comment.