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

Commit 54e4e40

Browse files
fix: support data urls (#49)
1 parent 32b4d86 commit 54e4e40

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

src/loaders-deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const _transformSource: transformSource = async function (
6969
defaultTransformSource,
7070
) {
7171
const { url } = context;
72-
const filePath = fileURLToPath(url);
72+
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
7373

7474
if (process.send) {
7575
process.send({

src/loaders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export const load: load = async function (
215215
return loaded;
216216
}
217217

218-
const filePath = fileURLToPath(url);
218+
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
219219
const code = loaded.source.toString();
220220

221221
if (
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const base64Module = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;
2+
const dataUrl = base64Module('console.log(123)');
3+
import(dataUrl);

tests/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const nodeVersions = [
3838
import('./specs/wasm'),
3939
node,
4040
);
41+
runTestSuite(
42+
import('./specs/data'),
43+
node,
44+
);
4145
});
4246

4347
runTestSuite(

tests/specs/data.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { testSuite, expect } from 'manten';
2+
import type { NodeApis } from '../utils/node-with-loader';
3+
4+
export default testSuite(async ({ describe }, node: NodeApis) => {
5+
describe('data', async ({ test }) => {
6+
const importPath = './lib/data/index.js';
7+
8+
test('Loads text/javascript data URLs', async () => {
9+
const nodeProcess = await node.load(importPath);
10+
11+
expect(nodeProcess.exitCode).toBe(0);
12+
expect(nodeProcess.stdout).toMatch('123');
13+
});
14+
});
15+
});

0 commit comments

Comments
 (0)