Skip to content

Commit

Permalink
Merge pull request #2 from dpikt/no-head
Browse files Browse the repository at this point in the history
Refactor + update injection logic
  • Loading branch information
dpikt authored Dec 23, 2023
2 parents 2723736 + 7a1cf8c commit d6966a0
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
21
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modulepreload",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "cli.js",
"bin": {
Expand Down
21 changes: 16 additions & 5 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ class ImportCollector {
resolvedImport = new URL(specifier, parent.origin)
}
if (!resolvedImport) {
throw 'could not resolve import: ' + specifier
console.warn(`WARNING: could not resolve import specifier: ${specifier} from parent: ${parent} - skipping`)
return
};
if (this.dependencies.has(resolvedImport.href)) return;
this.dependencies.add(resolvedImport.href);
if (resolvedImport.protocol === "file:") {
let contents = null
try {
const contents = await fs.promises.readFile(resolvedImport, "utf8");
contents = await fs.promises.readFile(resolvedImport, "utf8");
} catch (e) {
console.warn('WARNING: could not read file: ' + resolvedImport.href + ' - skipping')
}
if (contents) {
const deps = await getImports(contents);
await Promise.all(
deps.map((dep) => this.visit(dep, resolvedImport))
);
} catch (e) {
console.warn('WARNING: could not read file: ' + resolvedImport.href + ' - skipping')
}
} else if (resolvedImport.protocol.startsWith("http")) {
if (!this.noFetch) {
Expand Down Expand Up @@ -118,7 +122,14 @@ export function injectPreloads(contents, dependencies) {
for (const dep of dependencies) {
preloads += `<link rel="modulepreload" href="${dep}" />\n`;
}
return contents.replace("</head>", `${preloads}</head>`);
if (contents.includes("</head>")) {
return contents.replace("</head>", `${preloads}</head>`);
} else if (contents.includes("</html>")) {
return contents.replace("<html>", `<html>\n${preloads}`);
} else {
console.warn('WARNING: could not find <head> or <html> in HTML - skipping.')
return contents;
}
}

export async function link(htmlContentsOrUrl, { baseUrl: providedBaseUrl, noFetch } = {}) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions tests/fixtures/link/no-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>

<body>
<h1>Hello, world!</h1>
</body>
<script type="module">
import { hello } from './absolute-dep.js';
</script>

</html>
7 changes: 0 additions & 7 deletions tests/inject.test.js

This file was deleted.

19 changes: 19 additions & 0 deletions tests/link.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { link } from '../src/inject.js';
import { describe, it } from 'node:test';
import fs from 'node:fs';
import assert from 'node:assert';

describe('link', () => {
it('injects dependencies into HTML', async () => {
const htmlUrl = new URL('./fixtures/link/index.html', import.meta.url)
const result = await link(htmlUrl, { noFetch: true })
const expected = await fs.promises.readFile(new URL('./snapshots/link/index.snapshot.html', import.meta.url), 'utf8')
assert.strictEqual(result, expected)
})
it('injects dependencies to top of <html> if there is no <head> tag', async () => {
const htmlUrl = new URL('./fixtures/link/no-head.html', import.meta.url)
const result = await link(htmlUrl, { noFetch: true })
const expected = await fs.promises.readFile(new URL('./snapshots/link/no-head.snapshot.html', import.meta.url), 'utf8')
assert.strictEqual(result, expected)
});
})
26 changes: 26 additions & 0 deletions tests/snapshots/link/index.snapshot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>

<head>
<title>My HTML File</title>
<script type="importmap">
{
"imports": {
"jquery": "https://cdn.skypack.dev/jquery"
}
}
</script>
<link rel="modulepreload" href="/js/dep.js" />
<link rel="modulepreload" href="/js/secondary-dep.js" />
<link rel="modulepreload" href="/absolute-dep.js" />
<link rel="modulepreload" href="https://cdn.skypack.dev/jquery" />
</head>

<body>
<h1>Hello, world!</h1>
</body>
<script type="module">
import { hello } from './js/dep.js';
</script>

</html>
13 changes: 13 additions & 0 deletions tests/snapshots/link/no-head.snapshot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<link rel="modulepreload" href="/absolute-dep.js" />


<body>
<h1>Hello, world!</h1>
</body>
<script type="module">
import { hello } from './absolute-dep.js';
</script>

</html>

0 comments on commit d6966a0

Please sign in to comment.