Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
fix: support wasm extension (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 23, 2022
1 parent 61b2ac8 commit 32b4d86
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 44 deletions.
38 changes: 25 additions & 13 deletions src/loaders-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
applySourceMap,
tsconfigRaw,
tsExtensionsPattern,
getFormatFromExtension,
getFormatFromFileUrl,
fileProtocol,
type ModuleFormat,
type MaybePromise,
} from './utils';
import { getPackageType } from './package-json';

type getFormat = (
url: string,
Expand All @@ -35,12 +35,21 @@ const _getFormat: getFormat = async function (
return { format: 'module' };
}

if (url.startsWith('file:')) {
const format = getFormatFromExtension(url) ?? await getPackageType(url);
return { format };
}
try {
return await defaultGetFormat(url, context, defaultGetFormat);
} catch (error) {
if (
(error as any).code === 'ERR_UNKNOWN_FILE_EXTENSION'
&& url.startsWith(fileProtocol)
) {
const format = await getFormatFromFileUrl(url);
if (format) {
return { format };
}
}

return await defaultGetFormat(url, context, defaultGetFormat);
throw error;
}
};

type Source = string | SharedArrayBuffer | Uint8Array;
Expand Down Expand Up @@ -87,12 +96,15 @@ const _transformSource: transformSource = async function (
}

const result = await defaultTransformSource(source, context, defaultTransformSource);
const dynamicImportTransformed = transformDynamicImport(filePath, result.source.toString());
if (dynamicImportTransformed) {
result.source = applySourceMap(
dynamicImportTransformed,
url,
);

if (context.format === 'module') {
const dynamicImportTransformed = transformDynamicImport(filePath, result.source.toString());
if (dynamicImportTransformed) {
result.source = applySourceMap(
dynamicImportTransformed,
url,
);
}
}

return result;
Expand Down
46 changes: 17 additions & 29 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import {
tsconfigRaw,
tsconfigPathsMatcher,
tsExtensionsPattern,
getFormatFromExtension,
getFormatFromFileUrl,
fileProtocol,
type ModuleFormat,
type MaybePromise,
} from './utils';
import { getPackageType } from './package-json';

type Resolved = {
url: string;
format: ModuleFormat;
format: ModuleFormat | undefined;
};

type Context = {
Expand Down Expand Up @@ -81,7 +81,6 @@ async function tryDirectory(
}
}

const fileProtocol = 'file://';
const isPathPattern = /^\.{0,2}\//;

const supportsNodePrefix = (
Expand Down Expand Up @@ -169,27 +168,14 @@ export const resolve: resolve = async function (
throw error;
}

if (resolved.url.endsWith('.json')) {
return {
...resolved,
format: 'json',
};
}

let { format } = resolved;

if (resolved.url.startsWith(fileProtocol)) {
format = getFormatFromExtension(resolved.url) ?? format;

if (!format) {
format = await getPackageType(resolved.url);
}
if (
!resolved.format
&& resolved.url.startsWith(fileProtocol)
) {
resolved.format = await getFormatFromFileUrl(resolved.url);
}

return {
...resolved,
format,
};
return resolved;
};

type load = (
Expand Down Expand Up @@ -250,12 +236,14 @@ export const load: load = async function (
};
}

const dynamicImportTransformed = transformDynamicImport(filePath, code);
if (dynamicImportTransformed) {
loaded.source = applySourceMap(
dynamicImportTransformed,
url,
);
if (loaded.format === 'module') {
const dynamicImportTransformed = transformDynamicImport(filePath, code);
if (dynamicImportTransformed) {
loaded.source = applySourceMap(
dynamicImportTransformed,
url,
);
}
}

return loaded;
Expand Down
24 changes: 22 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
parseTsconfig,
createPathsMatcher,
} from 'get-tsconfig';
import { getPackageType } from './package-json';

export const applySourceMap = installSourceMapSupport();

Expand All @@ -20,10 +21,16 @@ const tsconfig = (
export const tsconfigRaw = tsconfig?.config;
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);

export const fileProtocol = 'file://';

export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)$/;

export const getFormatFromExtension = (filePath: string): ModuleFormat | undefined => {
const extension = path.extname(filePath);
const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
const extension = path.extname(fileUrl);

if (extension === '.json') {
return 'json';
}

if (extension === '.mjs' || extension === '.mts') {
return 'module';
Expand All @@ -34,6 +41,19 @@ export const getFormatFromExtension = (filePath: string): ModuleFormat | undefin
}
};

export const getFormatFromFileUrl = (fileUrl: string) => {
const format = getFormatFromExtension(fileUrl);

if (format) {
return format;
}

// ts, tsx, jsx
if (tsExtensionsPattern.test(fileUrl)) {
return getPackageType(fileUrl);
}
};

export type ModuleFormat =
| 'builtin'
| 'dynamic'
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/package-module/lib/wasm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { myValue } from './test.wasm';

console.log(myValue.valueOf());
Binary file added tests/fixtures/package-module/lib/wasm/test.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const nodeVersions = [
import('./specs/json'),
node,
);
runTestSuite(
import('./specs/wasm'),
node,
);
});

runTestSuite(
Expand Down
24 changes: 24 additions & 0 deletions tests/specs/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { testSuite, expect } from 'manten';
import type { NodeApis } from '../utils/node-with-loader';

export default testSuite(async ({ describe }, node: NodeApis) => {
describe('WASM', async ({ test }) => {
const importPath = './lib/wasm/index.js';

test('Unsupported extension error', async () => {
const nodeProcess = await node.load(importPath);

expect(nodeProcess.exitCode).toBe(1);
expect(nodeProcess.stderr).toMatch('ERR_UNKNOWN_FILE_EXTENSION');
});

test('Loads with experimental flag', async () => {
const nodeProcess = await node.load(importPath, {
nodeOptions: ['--experimental-wasm-modules'],
});

expect(nodeProcess.exitCode).toBe(0);
expect(nodeProcess.stdout).toBe('1234');
});
});
});

0 comments on commit 32b4d86

Please sign in to comment.