diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..b5045cc --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +21 \ No newline at end of file diff --git a/package.json b/package.json index 2b3ccf7..412da00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "modulepreload", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "cli.js", "bin": { diff --git a/src/inject.js b/src/inject.js index b6202f1..048b084 100644 --- a/src/inject.js +++ b/src/inject.js @@ -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) { @@ -118,7 +122,14 @@ export function injectPreloads(contents, dependencies) { for (const dep of dependencies) { preloads += `\n`; } - return contents.replace("", `${preloads}`); + if (contents.includes("")) { + return contents.replace("", `${preloads}`); + } else if (contents.includes("")) { + return contents.replace("", `\n${preloads}`); + } else { + console.warn('WARNING: could not find or in HTML - skipping.') + return contents; + } } export async function link(htmlContentsOrUrl, { baseUrl: providedBaseUrl, noFetch } = {}) { diff --git a/tests/fixtures/absolute-dep.js b/tests/fixtures/link/absolute-dep.js similarity index 100% rename from tests/fixtures/absolute-dep.js rename to tests/fixtures/link/absolute-dep.js diff --git a/tests/fixtures/index.html b/tests/fixtures/link/index.html similarity index 100% rename from tests/fixtures/index.html rename to tests/fixtures/link/index.html diff --git a/tests/fixtures/js/dep.js b/tests/fixtures/link/js/dep.js similarity index 100% rename from tests/fixtures/js/dep.js rename to tests/fixtures/link/js/dep.js diff --git a/tests/fixtures/js/secondary-dep.js b/tests/fixtures/link/js/secondary-dep.js similarity index 100% rename from tests/fixtures/js/secondary-dep.js rename to tests/fixtures/link/js/secondary-dep.js diff --git a/tests/fixtures/link/no-head.html b/tests/fixtures/link/no-head.html new file mode 100644 index 0000000..d764bc7 --- /dev/null +++ b/tests/fixtures/link/no-head.html @@ -0,0 +1,11 @@ + + + + +

Hello, world!

+ + + + \ No newline at end of file diff --git a/tests/inject.test.js b/tests/inject.test.js deleted file mode 100644 index 9cafbf4..0000000 --- a/tests/inject.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import { link } from '../src/inject.js'; -import test from 'node:test'; - -test('inject', async (t) => { - const htmlUrl = new URL('./fixtures/index.html', import.meta.url) - console.log(await link(htmlUrl, { noFetch: true })) -}); diff --git a/tests/link.test.js b/tests/link.test.js new file mode 100644 index 0000000..5d90ea9 --- /dev/null +++ b/tests/link.test.js @@ -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 if there is no 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) + }); +}) diff --git a/tests/snapshots/link/index.snapshot.html b/tests/snapshots/link/index.snapshot.html new file mode 100644 index 0000000..6169a9f --- /dev/null +++ b/tests/snapshots/link/index.snapshot.html @@ -0,0 +1,26 @@ + + + + + My HTML File + + + + + + + + +

Hello, world!

+ + + + \ No newline at end of file diff --git a/tests/snapshots/link/no-head.snapshot.html b/tests/snapshots/link/no-head.snapshot.html new file mode 100644 index 0000000..81e5156 --- /dev/null +++ b/tests/snapshots/link/no-head.snapshot.html @@ -0,0 +1,13 @@ + + + + + + +

Hello, world!

+ + + + \ No newline at end of file