From 3d9debd9702222a63df0b72a5359c0437b463a0c Mon Sep 17 00:00:00 2001 From: iarroyo Date: Tue, 17 Dec 2024 16:32:20 +0100 Subject: [PATCH 1/4] Added resolver for tsconfig.json that extends from a package --- packages/core/src/config/loader.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/core/src/config/loader.ts b/packages/core/src/config/loader.ts index 8f1ad479c..0567a5325 100644 --- a/packages/core/src/config/loader.ts +++ b/packages/core/src/config/loader.ts @@ -1,11 +1,12 @@ import { createRequire } from 'node:module'; import * as path from 'node:path'; +import * as fs from 'node:fs'; import SilentError from 'silent-error'; import { GlintConfig } from './config.js'; import { GlintConfigInput } from '@glint/core/config-types'; import type TS from 'typescript'; -const require = createRequire(import.meta.url); +export const require = createRequire(import.meta.url); type TypeScript = typeof TS; @@ -75,8 +76,15 @@ function loadConfigInput(ts: TypeScript, entryPath: string): GlintConfigInput | ); fullGlintConfig = { ...currentGlintConfig, ...fullGlintConfig }; - currentPath = - currentContents.extends && path.resolve(path.dirname(currentPath), currentContents.extends); + + if (currentContents.extends) { + currentPath = path.resolve(path.dirname(currentPath), currentContents.extends); + if (!fs.existsSync(currentPath)) { + currentPath = require.resolve(currentContents.extends); + } + } else { + currentPath = undefined; + } } return validateConfigInput(fullGlintConfig); From 91d02c5d79d8af4051a97874d8e0ed13f0199e43 Mon Sep 17 00:00:00 2001 From: iarroyo Date: Tue, 17 Dec 2024 16:33:27 +0100 Subject: [PATCH 2/4] Created test to resolve tsconfig.json that extends from a package --- .../core/__tests__/config/load-config.test.ts | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/core/__tests__/config/load-config.test.ts b/packages/core/__tests__/config/load-config.test.ts index ddc2e2d2a..d76d6d171 100644 --- a/packages/core/__tests__/config/load-config.test.ts +++ b/packages/core/__tests__/config/load-config.test.ts @@ -1,8 +1,9 @@ import * as fs from 'node:fs'; import * as os from 'node:os'; -import { describe, beforeEach, afterEach, test, expect } from 'vitest'; +import { describe, beforeEach, afterEach, test, expect, vi } from 'vitest'; import { loadConfig } from '../../src/config/index.js'; import { normalizePath } from '../../src/config/config.js'; +import { require } from '../../src/config/loader.js'; describe('Config: loadConfig', () => { const testDir = `${os.tmpdir()}/glint-config-test-load-config-${process.pid}`; @@ -51,4 +52,49 @@ describe('Config: loadConfig', () => { expect(config.environment.getConfiguredTemplateTags()).toEqual({ test: {} }); expect(config.checkStandaloneTemplates).toBe(false); }); + + test('locates config in package', () => { + const directory = `${testDir}/package-glint-config`; + const nodeModulePackageDir = `${directory}/node_modules/@package1`; + + vi.spyOn(require, 'resolve').mockImplementation((id: string | undefined) => { + if (id === '@package1/tsconfig.json') { + return id.replace('@package1', nodeModulePackageDir); + } + throw Error(`Cannot resolve module ${id}`); + }); + + fs.mkdirSync(nodeModulePackageDir, { recursive: true }); + fs.writeFileSync( + `${nodeModulePackageDir}/package.json`, + JSON.stringify({ + name: '@package1', + version: '1.0.0', + }) + ); + fs.writeFileSync( + `${nodeModulePackageDir}/tsconfig.json`, + JSON.stringify({ + glint: { + environment: 'kaboom', + checkStandaloneTemplates: false, + }, + }) + ); + fs.writeFileSync( + `${directory}/tsconfig.json`, + JSON.stringify({ + extends: '@package1/tsconfig.json', + glint: { + environment: '../local-env', + }, + }) + ); + + let config = loadConfig(`${directory}`); + + expect(config.rootDir).toBe(normalizePath(`${directory}`)); + expect(config.environment.getConfiguredTemplateTags()).toEqual({ test: {} }); + expect(config.checkStandaloneTemplates).toBe(false); + }); }); From 6622ebf8f5843d479808c7d6080c5773c839ed74 Mon Sep 17 00:00:00 2001 From: iarroyo Date: Sat, 28 Dec 2024 10:10:19 +0100 Subject: [PATCH 3/4] Added a block comment to caution against using the exported 'require' function --- packages/core/src/config/loader.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/core/src/config/loader.ts b/packages/core/src/config/loader.ts index 0567a5325..48893dab8 100644 --- a/packages/core/src/config/loader.ts +++ b/packages/core/src/config/loader.ts @@ -6,6 +6,11 @@ import { GlintConfig } from './config.js'; import { GlintConfigInput } from '@glint/core/config-types'; import type TS from 'typescript'; +/** + * @private + * + * Only exported for testing purposes. Do not import. + */ export const require = createRequire(import.meta.url); type TypeScript = typeof TS; From 84898f3623fd708c34b391161c18ff580760abb5 Mon Sep 17 00:00:00 2001 From: iarroyo Date: Fri, 3 Jan 2025 18:26:52 +0100 Subject: [PATCH 4/4] fixed prettier issue --- packages/core/__tests__/config/load-config.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/__tests__/config/load-config.test.ts b/packages/core/__tests__/config/load-config.test.ts index d76d6d171..f279cbfdc 100644 --- a/packages/core/__tests__/config/load-config.test.ts +++ b/packages/core/__tests__/config/load-config.test.ts @@ -70,7 +70,7 @@ describe('Config: loadConfig', () => { JSON.stringify({ name: '@package1', version: '1.0.0', - }) + }), ); fs.writeFileSync( `${nodeModulePackageDir}/tsconfig.json`, @@ -79,7 +79,7 @@ describe('Config: loadConfig', () => { environment: 'kaboom', checkStandaloneTemplates: false, }, - }) + }), ); fs.writeFileSync( `${directory}/tsconfig.json`, @@ -88,7 +88,7 @@ describe('Config: loadConfig', () => { glint: { environment: '../local-env', }, - }) + }), ); let config = loadConfig(`${directory}`);