Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: MercuryWorkshop/scramjet
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: YourAdIsHere/scramjet
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 8 commits
  • 3 files changed
  • 1 contributor

Commits on Jun 26, 2024

  1. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    1436d47 View commit details
  2. Copy the full SHA
    7237bba View commit details
  3. Copy the full SHA
    7130b31 View commit details
  4. Update README.md

    YourAdIsHere authored Jun 26, 2024
    Copy the full SHA
    1c09eda View commit details
  5. Copy the full SHA
    f846c73 View commit details
  6. remove changes to js.ts

    YourAdIsHere authored Jun 26, 2024
    Copy the full SHA
    b991ff6 View commit details
  7. Update html.ts

    YourAdIsHere authored Jun 26, 2024
    Copy the full SHA
    2a13683 View commit details
  8. sync with main

    YourAdIsHere authored Jun 26, 2024
    Copy the full SHA
    018f62e View commit details
Showing with 33 additions and 5 deletions.
  1. +0 −3 README.md
  2. +32 −1 src/bundle/rewriters/html.ts
  3. +1 −1 src/bundle/rewriters/js.ts
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,9 +12,6 @@ Running `pnpm dev` will build Scramjet and start a dev server on localhost:1337.


## TODO
- Finish HTML rewriting
- `<script type="importmap"></script>` rewriting
- Make an array of all possible import values and pass the array onto the JS rewriter, then rewrite all the URLs inside of it
- Finish JS rewriting
- Only thing rewritten currently are imports and exports
- Check imports/exports for values contained in the `importmap` array, don't rewrite the node value if present
33 changes: 32 additions & 1 deletion src/bundle/rewriters/html.ts
Original file line number Diff line number Diff line change
@@ -38,8 +38,33 @@ function traverseParsedHtml(node, origin?: URL) {
if (hasAttrib(node, "srcset")) node.attribs.srcset = rewriteSrcset(node.attribs.srcset, origin);
if (hasAttrib(node, "imagesrcset")) node.attribs.imagesrcset = rewriteSrcset(node.attribs.imagesrcset, origin);
if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin);
//importmaps!!!
if (node.type === "script" && node.attribs.type === "importmap" && node.children.length > 0) {
const scriptContent = node.children[0].data.trim();

let imports;
try {
imports = JSON.parse(scriptContent).imports || {};
} catch (error) {
console.error("Error parsing import map:", error);
imports = {};
}

for (const key in imports) {
if (Object.prototype.hasOwnProperty.call(imports, key)) {
imports[key] = encodeUrl(imports[key], origin);
}
}

const formattedScriptContent = JSON.stringify({ imports }, null, 2); // retain formatting

node.children[0].data = formattedScriptContent;

node.attribs.type = "importmap";
}

if (node.name === "style" && node.children[0] !== undefined) node.children[0].data = rewriteCss(node.children[0].data, origin);
//leaving this alone for now
if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin);
if (node.name === "meta" && hasAttrib(node, "http-equiv")) {
if (node.attribs["http-equiv"] === "content-security-policy") {
@@ -71,6 +96,12 @@ function traverseParsedHtml(node, origin?: URL) {
return node;
}


function parseImportValues(scriptContent: string): string[] {
const importMap = JSON.parse(scriptContent);
return Object.values(importMap.imports || {});
}

// stole from osana lmao
export function rewriteSrcset(srcset: string, origin?: URL) {
const urls = srcset.split(/ [0-9]+x,? ?/g);
@@ -84,4 +115,4 @@ export function rewriteSrcset(srcset: string, origin?: URL) {
});

return rewrittenUrls.join("");
}
}
2 changes: 1 addition & 1 deletion src/bundle/rewriters/js.ts
Original file line number Diff line number Diff line change
@@ -59,4 +59,4 @@ export function rewriteJs(js: string, origin?: URL) {

return js;
}
}
}