From 5f653b3ff90a72b3f6fef99edcbbdcebdebc783e Mon Sep 17 00:00:00 2001 From: Aaron Date: Sun, 18 Aug 2024 15:18:23 -0500 Subject: [PATCH] fix: Ensure TSConfig paths start with `../` or `./` so they are valid (#927) --- .../wxt/e2e/tests/typescript-project.test.ts | 16 ++++++++++++++++ .../src/core/utils/building/generate-wxt-dir.ts | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/wxt/e2e/tests/typescript-project.test.ts b/packages/wxt/e2e/tests/typescript-project.test.ts index 40542b18..0e31ed41 100644 --- a/packages/wxt/e2e/tests/typescript-project.test.ts +++ b/packages/wxt/e2e/tests/typescript-project.test.ts @@ -379,6 +379,22 @@ describe('TypeScript Project', () => { `); }); + it('should start path aliases with "./" for paths inside the .wxt dir', async () => { + const project = new TestProject(); + project.addFile('src/entrypoints/unlisted.html', ''); + project.setConfigFileConfig({ + srcDir: 'src', + alias: { + example: '.wxt/example.ts', + }, + }); + + await project.prepare(); + + const output = await project.serializeFile('.wxt/tsconfig.json'); + expect(output).toContain('./example.ts'); + }); + // TODO: Once a module has been published, use it here for testing - local files are never added to the .wxt/wxt.d.ts file it.todo( 'should add modules from NPM to the TS project if they have a configKey', diff --git a/packages/wxt/src/core/utils/building/generate-wxt-dir.ts b/packages/wxt/src/core/utils/building/generate-wxt-dir.ts index 36780f6d..08d172d9 100644 --- a/packages/wxt/src/core/utils/building/generate-wxt-dir.ts +++ b/packages/wxt/src/core/utils/building/generate-wxt-dir.ts @@ -233,7 +233,11 @@ function getMainDeclarationEntry(references: WxtDirEntry[]): WxtDirFileEntry { async function getTsConfigEntry(): Promise { const dir = wxt.config.wxtDir; - const getTsconfigPath = (path: string) => normalizePath(relative(dir, path)); + const getTsconfigPath = (path: string) => { + const res = normalizePath(relative(dir, path)); + if (res.startsWith('.') || res.startsWith('/')) return res; + return './' + res; + }; const paths = Object.entries(wxt.config.alias) .flatMap(([alias, absolutePath]) => { const aliasPath = getTsconfigPath(absolutePath);