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

Commit

Permalink
fix: support data urls (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 29, 2022
1 parent 32b4d86 commit 54e4e40
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/loaders-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const _transformSource: transformSource = async function (
defaultTransformSource,
) {
const { url } = context;
const filePath = fileURLToPath(url);
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;

if (process.send) {
process.send({
Expand Down
2 changes: 1 addition & 1 deletion src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const load: load = async function (
return loaded;
}

const filePath = fileURLToPath(url);
const filePath = url.startsWith('file://') ? fileURLToPath(url) : url;
const code = loaded.source.toString();

if (
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/package-module/lib/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const base64Module = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;
const dataUrl = base64Module('console.log(123)');
import(dataUrl);
4 changes: 4 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const nodeVersions = [
import('./specs/wasm'),
node,
);
runTestSuite(
import('./specs/data'),
node,
);
});

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

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

test('Loads text/javascript data URLs', async () => {
const nodeProcess = await node.load(importPath);

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

0 comments on commit 54e4e40

Please sign in to comment.