-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: proxy request to esm.sh and convert to systemjs
- Loading branch information
Showing
8 changed files
with
594 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'); | ||
} |