Skip to content

Commit

Permalink
feat: proxy request to esm.sh and convert to systemjs
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed May 15, 2023
1 parent d7878f8 commit 6895382
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 0 deletions.
7 changes: 7 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"importMap": "./importmap.json",
"tasks": {
"dev": "deno run --watch src/main.ts",
"test": "deno test --allow-env --allow-read"
}
}
516 changes: 516 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions importmap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"imports": {
"@rollup/plugin-virtual": "https://esm.sh/@rollup/plugin-virtual",
"rollup": "https://esm.sh/rollup",
"testing/asserts": "https://deno.land/std/testing/asserts.ts",
"testing/mock": "https://deno.land/std/testing/mock.ts",
"http": "https://deno.land/std/http/mod.ts"
}
}
14 changes: 14 additions & 0 deletions src/esm-proxy-request-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assertEquals } from 'testing/asserts';
import { returnsNext, stub } from 'testing/mock';
import { esmProxyRequestHandler } from './esm-proxy-request-handler.ts';

Deno.test('should return an string of code in systemjs format', async () => {
const fetchStub = stub(globalThis, 'fetch', returnsNext([
Promise.resolve({ text: () => Promise.resolve('export * from "https://esm.sh/stable/vue@3.3.2/es2022/vue.mjs";') } as unknown as Response),
]));
const req = new Request('https://esm.sh/vue');
const res = await esmProxyRequestHandler(req);
const systemjsCode = await res.text();
assertEquals(systemjsCode.startsWith('System.register('), true);
fetchStub.restore();
});
8 changes: 8 additions & 0 deletions src/esm-proxy-request-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { toSystemjs } from './to-systemjs.ts';

export async function esmProxyRequestHandler(req: Request) {
const modifiedUrl = new URL(req.url, 'https://esm.sh');
const esmCode = await fetch(modifiedUrl.toString(), { headers: req.headers }).then((res) => res.text());
const systemjsCode = await toSystemjs(esmCode);
return new Response(systemjsCode);
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { serve } from 'http';
import { esmProxyRequestHandler } from './esm-proxy-request-handler.ts';

serve(esmProxyRequestHandler, { port: 8000 });
9 changes: 9 additions & 0 deletions src/main_bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { add } from "./main.ts";

Deno.bench(function addSmall() {
add(1, 2);
});

Deno.bench(function addBig() {
add(2 ** 32, 2 ** 32);
});
27 changes: 27 additions & 0 deletions src/to-systemjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ModuleFormat, rollup } from 'rollup';
import virtual from '@rollup/plugin-virtual';
//import { minify } from 'npm:rollup-plugin-esbuild';

export const toSystemjs = async (esmCode: string): Promise<string> => {

const inputOptions = {
external: () => true,
input: 'esmCode',
plugins: [
virtual({ esmCode }),
//minify(),
],
treeshake: false,
};

const outputOptions = {
file: 'out.mjs',
format: 'systemjs' as ModuleFormat,
sourcemap: false,
};

const bundle = await rollup(inputOptions);
const { output } = await bundle.generate(outputOptions);
await bundle.close();
return output[0].code.replace(/https:\/\/esm\.sh\//g, 'https://systemjs.sh/');
}

0 comments on commit 6895382

Please sign in to comment.