From 3396ce9ce66ac50296322fa6bf23ceaba7150938 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 9 Oct 2024 19:18:01 +0200 Subject: [PATCH] Fix hbs plugin not resolving .hbs due to broken Regex The Regex in `syntheticJStoHBS` was broken. Given a TO .hbs such as `components/json/index.hbs`, the hbs rollup would need to resolve that from a module id of `components/json/index.js` to the actual `components/json/index.hbs` file on disk. But with the broken regex, it tried instead to find `components.hbson/index.hbs`! Added some unit tests (that failed without the fix) to prevent regressions. --- packages/shared-internals/src/colocation.ts | 2 +- .../shared-internals/tests/colocation.test.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 packages/shared-internals/tests/colocation.test.ts diff --git a/packages/shared-internals/src/colocation.ts b/packages/shared-internals/src/colocation.ts index b83f11a9a..3c635a7df 100644 --- a/packages/shared-internals/src/colocation.ts +++ b/packages/shared-internals/src/colocation.ts @@ -7,7 +7,7 @@ export function syntheticJStoHBS(source: string): string | null { // only ever JS (never TS or anything else). And extensionless imports are // handled by the default resolving system doing extension search. if (cleanUrl(source).endsWith('.js')) { - return source.replace(/.js(\?.*)?/, '.hbs$1'); + return source.replace(/\.js(\?.*)?$/, '.hbs$1'); } return null; diff --git a/packages/shared-internals/tests/colocation.test.ts b/packages/shared-internals/tests/colocation.test.ts new file mode 100644 index 000000000..9bdd22bb2 --- /dev/null +++ b/packages/shared-internals/tests/colocation.test.ts @@ -0,0 +1,25 @@ +import { syntheticJStoHBS } from '../src'; + +describe('colocation utils', function () { + describe('syntheticJStoHBS', function () { + test('it returns .hbs files for .js', function () { + const testCases = [ + ['foo.js', 'foo.hbs'], + ['foo.js?qp', 'foo.hbs?qp'], + ['foo/json.js', 'foo/json.hbs'], + ]; + + for (const [from, to] of testCases) { + expect(syntheticJStoHBS(from)).toEqual(to); + } + }); + + test('it ignores non .js files', function () { + const testCases = ['foo.ts', 'foo.hbs', 'foo.js.xxx']; + + for (const from of testCases) { + expect(syntheticJStoHBS(from)).toEqual(null); + } + }); + }); +});