From dafadccd7706b0bb539669d4f6d13431b961cc0d Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Mon, 17 Jun 2024 22:06:33 +0200 Subject: [PATCH] fix #617 --- CHANGELOG.md | 2 + plugins/esbuild.ts | 9 +++- plugins/source_maps.ts | 2 +- tests/__snapshots__/esbuild.test.ts.snap | 60 ++++++++++++++++++++++++ tests/assets/esbuild/other/_data.yml | 1 + tests/assets/esbuild/other/script.ts | 1 + 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 tests/assets/esbuild/other/_data.yml create mode 100644 tests/assets/esbuild/other/script.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d42931d..9f0adc9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Go to the `v1` branch to see the changelog of Lume 1. ## [2.2.2] - Unreleased ### Fixed +- esbuild plugin: Fixed `basename` support [#617]. - Apply `mergedKeys` configuration in layouts [#618]. - Extended Preact types with Lume's custom attributes [#619]. - Hot reload: Ensure sockets are open before send updates [#614], [#615]. @@ -411,6 +412,7 @@ Go to the `v1` branch to see the changelog of Lume 1. [#610]: https://github.com/lumeland/lume/issues/610 [#614]: https://github.com/lumeland/lume/issues/614 [#615]: https://github.com/lumeland/lume/issues/615 +[#617]: https://github.com/lumeland/lume/issues/617 [#618]: https://github.com/lumeland/lume/issues/618 [#619]: https://github.com/lumeland/lume/issues/619 diff --git a/plugins/esbuild.ts b/plugins/esbuild.ts index 8139e345..45a2f9a8 100644 --- a/plugins/esbuild.ts +++ b/plugins/esbuild.ts @@ -245,12 +245,13 @@ export default function (userOptions?: Options) { const url = normalizePath( normalizePath(file.path).replace(basePath, ""), ); + const urlWithoutExt = pathWithoutExtension(url); const entryPoint = pages.find((page) => { const outdir = posix.join( "/", options.options.outdir || ".", - pathWithoutExtension(page.data.url), + pathWithoutExtension(page.sourcePath), ); return outdir === urlWithoutExt; @@ -263,7 +264,11 @@ export default function (userOptions?: Options) { // The page is an entry point if (entryPoint) { - entryPoint.data.url = url; // Update the url to .js extension + entryPoint.data.url = posix.join( + "/", + options.options.outdir || ".", + replaceExtension(entryPoint.data.url, ".js"), + ); saveAsset(site, entryPoint, content, map?.text); } else { // The page is a chunk diff --git a/plugins/source_maps.ts b/plugins/source_maps.ts index 4cb0e284..150c9d92 100644 --- a/plugins/source_maps.ts +++ b/plugins/source_maps.ts @@ -112,7 +112,7 @@ export function prepareAsset(site: Site, page: Page): PrepareResult { ? page.data.sourceMap as SourceMap | undefined : undefined; const filename = page.src.path - ? site.src(page.src.path + page.src.ext) + ? site.src(page.sourcePath) : site.src(page.outputPath); return { content, sourceMap, filename, enableSourceMap }; } diff --git a/tests/__snapshots__/esbuild.test.ts.snap b/tests/__snapshots__/esbuild.test.ts.snap index 721bbfeb..d80fc4c0 100644 --- a/tests/__snapshots__/esbuild.test.ts.snap +++ b/tests/__snapshots__/esbuild.test.ts.snap @@ -115,7 +115,10 @@ snapshot[`esbuild plugin 1`] = ` "/main.vto", "/modules", "/modules/to_uppercase.ts", + "/other", "/other.ts", + "/other/_data.yml", + "/other/script.ts", ], } `; @@ -240,6 +243,33 @@ document.querySelectorAll(".other")?.forEach((el) => { remote: undefined, }, }, + { + content: 'console.log("Hello from script.ts!"); +', + data: { + basename: "script", + content: 'console.log("Hello from script.ts!")', + date: [], + mergedKeys: [ + "tags", + ], + page: [ + "src", + "data", + "asset", + ], + paginate: "paginate", + search: [], + tags: "Array(0)", + url: "/foo/bar/script.js", + }, + src: { + asset: true, + ext: ".ts", + path: "/other/script", + remote: undefined, + }, + }, ] `; @@ -358,7 +388,10 @@ snapshot[`esbuild plugin with splitting as true 1`] = ` "/main.vto", "/modules", "/modules/to_uppercase.ts", + "/other", "/other.ts", + "/other/_data.yml", + "/other/script.ts", ], } `; @@ -502,6 +535,33 @@ document.querySelectorAll(".other")?.forEach((el) => { remote: undefined, }, }, + { + content: 'console.log("Hello from script.ts!"); +', + data: { + basename: "script", + content: 'console.log("Hello from script.ts!")', + date: [], + mergedKeys: [ + "tags", + ], + page: [ + "src", + "data", + "asset", + ], + paginate: "paginate", + search: [], + tags: "Array(0)", + url: "/foo/foo/bar/script.js", + }, + src: { + asset: true, + ext: ".ts", + path: "/other/script", + remote: undefined, + }, + }, ] `; diff --git a/tests/assets/esbuild/other/_data.yml b/tests/assets/esbuild/other/_data.yml new file mode 100644 index 00000000..3b82bf5e --- /dev/null +++ b/tests/assets/esbuild/other/_data.yml @@ -0,0 +1 @@ +basename: "/foo/bar" \ No newline at end of file diff --git a/tests/assets/esbuild/other/script.ts b/tests/assets/esbuild/other/script.ts new file mode 100644 index 00000000..b48b6756 --- /dev/null +++ b/tests/assets/esbuild/other/script.ts @@ -0,0 +1 @@ +console.log("Hello from script.ts!");