apply import maps
This commit is contained in:
parent
052dd13bb9
commit
d6a9a269a7
@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
|
import * as Util from "@able/";
|
||||||
|
import * as React from "react";
|
||||||
|
import {createElement} from "react/client";
|
||||||
|
|
||||||
import('https://esm.sh/react').then((module) => {
|
import('react').then((module) => {
|
||||||
console.log(module);
|
console.log(module);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -57,12 +57,17 @@ let Configuration:Configuration =
|
|||||||
Spoof: "/@able",
|
Spoof: "/@able",
|
||||||
async Serve(inReq, inURL, inExt, inMap, inConfig)
|
async Serve(inReq, inURL, inExt, inMap, inConfig)
|
||||||
{
|
{
|
||||||
if(inReq.headers.get("user-agent")?.startsWith("Deno"))
|
if( inReq.headers.get("user-agent")?.startsWith("Deno") || inURL.searchParams.get("deno") )
|
||||||
{
|
{
|
||||||
|
console.log("patching...")
|
||||||
const file = await fetch(inConfig.Proxy + inURL.pathname);
|
const file = await fetch(inConfig.Proxy + inURL.pathname);
|
||||||
const text = await file.text();
|
const text = await file.text();
|
||||||
|
|
||||||
return new Response(Transpile.Patch(text), {headers:{"content-type":"application/javascript"}} );
|
return new Response(Transpile.Patch(text, inMap), {headers:{"content-type":"application/javascript"}} );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.log("not a deno-able file")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Remap: (inImports, inConfig)=>
|
Remap: (inImports, inConfig)=>
|
||||||
@ -139,20 +144,60 @@ export const Transpile =
|
|||||||
ImportMapReload();
|
ImportMapReload();
|
||||||
return size;
|
return size;
|
||||||
},
|
},
|
||||||
Patch(inText)
|
/**
|
||||||
|
* Converts dynamic module imports in to static, also can resolve paths with an import map
|
||||||
|
*/
|
||||||
|
Patch(inText:string, inMap?:DenoConfig)
|
||||||
{
|
{
|
||||||
const regex = /(?<![\w.])import\(([^)]+)(?!import\b)\)/g;
|
const remap = inMap ? (inPath:string)=>
|
||||||
const staticImports = [];
|
{
|
||||||
|
const match = inMap.imports[inPath];
|
||||||
let match;
|
if(match)
|
||||||
|
{
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
else if(inPath.includes("/"))
|
||||||
|
{
|
||||||
|
let bestKey = "";
|
||||||
|
let bestLength = 0;
|
||||||
|
Object.keys(inMap.imports).forEach((key, i, arr)=>
|
||||||
|
{
|
||||||
|
if(key.endsWith("/") && inPath.startsWith(key) && key.length > bestLength)
|
||||||
|
{
|
||||||
|
bestKey = key;
|
||||||
|
bestLength = bestLength;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if(bestKey)
|
||||||
|
{
|
||||||
|
return inMap.imports[bestKey]+inPath.substring(bestKey.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inPath;
|
||||||
|
}
|
||||||
|
: (inPath:string)=>inPath;
|
||||||
|
let match, regex;
|
||||||
let convertedBody = inText;
|
let convertedBody = inText;
|
||||||
|
|
||||||
|
// remap static imports
|
||||||
|
regex = /from\s+(['"`])(.*?)\1/g;
|
||||||
|
while ((match = regex.exec(inText)))
|
||||||
|
{
|
||||||
|
const importStatement = match[0];
|
||||||
|
const importPath = match[2];
|
||||||
|
convertedBody = convertedBody.replace(importStatement, `from "${remap(importPath)}"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert dynamic imports into static (to work around deno deploy)
|
||||||
|
const staticImports = [];
|
||||||
|
regex = /(?<![\w.])import\(([^)]+)(?!import\b)\)/g;
|
||||||
while ((match = regex.exec(inText)))
|
while ((match = regex.exec(inText)))
|
||||||
{
|
{
|
||||||
const importStatement = match[0];
|
const importStatement = match[0];
|
||||||
const importContent = match[1];
|
const importPath = remap(match[1].substring(1, match[1].length-1));
|
||||||
const moduleName = `_dyn_${staticImports.length}`;
|
const moduleName:string = `_dyn_${staticImports.length}`;
|
||||||
|
|
||||||
staticImports.push(`import ${moduleName} from ${importContent};`);
|
staticImports.push(`import ${moduleName} from ${importPath};`);
|
||||||
convertedBody = convertedBody.replace(importStatement, `Promise.resolve(${moduleName})`);
|
convertedBody = convertedBody.replace(importStatement, `Promise.resolve(${moduleName})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user