From 3cee6d6d6f89f3bcfa6417fb25a35c28e6f63bce Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 6 Dec 2023 10:23:43 -0500 Subject: [PATCH 1/2] Specify rootDir so that declarations can always be emitted with the correct paths. --- files/__addonLocation__/tsconfig.json | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/files/__addonLocation__/tsconfig.json b/files/__addonLocation__/tsconfig.json index 5f392b1b..ab8d5f82 100644 --- a/files/__addonLocation__/tsconfig.json +++ b/files/__addonLocation__/tsconfig.json @@ -11,6 +11,17 @@ "allowJs": true, "declarationDir": "declarations", + /** + https://www.typescriptlang.org/tsconfig#rootDir + "Default: The longest common path of all non-declaration input files." + Because we want our declarations' structure to match our rollup output, + we need this "rootDir" to match the "srcDir" in the rollup.config.mjs. + + This way, we can have simpler `package.json#exports` that matches + imports to files on disk + */ + "rootDir": "./src", + /** We don't want to include types dependencies in our compiled output, so tell TypeScript to enforce using `import type` instead of `import` for Types. From 29c4e78a207a7c74e9f9f7bbd747ffb301fedcdd Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 6 Dec 2023 10:42:36 -0500 Subject: [PATCH 2/2] Add test --- .github/workflows/ci.yml | 2 + files/__addonLocation__/tsconfig.json | 9 +++- tests/rollup-build-tests/declarations.test.ts | 51 +++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/rollup-build-tests/declarations.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9f08aaea..1a2be85c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,6 +62,8 @@ jobs: # build-only tests for testing if the rollup config works at all - rollup-build + - declarations-configuration + steps: - uses: actions/checkout@v3 - uses: wyvox/action-setup-pnpm@v2 diff --git a/files/__addonLocation__/tsconfig.json b/files/__addonLocation__/tsconfig.json index ab8d5f82..29adbb49 100644 --- a/files/__addonLocation__/tsconfig.json +++ b/files/__addonLocation__/tsconfig.json @@ -14,21 +14,26 @@ /** https://www.typescriptlang.org/tsconfig#rootDir "Default: The longest common path of all non-declaration input files." + Because we want our declarations' structure to match our rollup output, we need this "rootDir" to match the "srcDir" in the rollup.config.mjs. - - This way, we can have simpler `package.json#exports` that matches + + This way, we can have simpler `package.json#exports` that matches imports to files on disk */ "rootDir": "./src", /** + https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax + We don't want to include types dependencies in our compiled output, so tell TypeScript to enforce using `import type` instead of `import` for Types. */ "verbatimModuleSyntax": true, /** + https://www.typescriptlang.org/tsconfig#allowImportingTsExtensions + We want our tooling to know how to resolve our custom files so the appropriate plugins can do the proper transformations on those files. */ diff --git a/tests/rollup-build-tests/declarations.test.ts b/tests/rollup-build-tests/declarations.test.ts new file mode 100644 index 00000000..5e0d2b79 --- /dev/null +++ b/tests/rollup-build-tests/declarations.test.ts @@ -0,0 +1,51 @@ +import fs from 'node:fs/promises'; +import path from 'node:path'; + +import fse from 'fs-extra'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; + +import { AddonHelper, dirContents } from '../helpers.js'; + +/** + * These tests are to ensure that for typescript, we've configured the tsconfig.json correctly + */ +describe(`declarations-configuration`, () => { + let declarationsDir = ''; + let helper = new AddonHelper({ + packageManager: 'pnpm', + args: ['--typescript'], + scenario: 'explicit-imports', + }); + + beforeAll(async () => { + await helper.setup(); + await helper.installDeps(); + + declarationsDir = path.join(helper.addonFolder, 'declarations'); + }); + + afterAll(async () => { + await helper.clean(); + }); + + describe('rootDir', () => { + it('there are no top-level files, only nested in folders', async () => { + await fse.rm(path.join(helper.addonFolder, 'src'), { recursive: true }); + await fse.mkdirp(path.join(helper.addonFolder, 'src/components')); + await fs.writeFile(path.join(helper.addonFolder, 'src/components/example.ts'), '/* empty file */'); + + let buildResult = await helper.build(); + + expect(buildResult.exitCode).toEqual(0); + + expect(await dirContents(declarationsDir)).to.deep.equal([ + 'components', + ]); + + expect(await dirContents(path.join(declarationsDir, 'components'))).to.deep.equal([ + 'example.d.ts', + 'example.d.ts.map' + ]); + }); + }); +});