Skip to content

Commit

Permalink
Merge pull request #782 from iarroyo/typed-ember/resolve-config-from-…
Browse files Browse the repository at this point in the history
…extends-package

Typed ember/resolve config from extends package
  • Loading branch information
NullVoxPopuli authored Jan 3, 2025
2 parents 34c4510 + 84898f3 commit d8f63e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
48 changes: 47 additions & 1 deletion packages/core/__tests__/config/load-config.test.ts
Original file line number Diff line number Diff line change
@@ -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}`;
Expand Down Expand Up @@ -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);
});
});
19 changes: 16 additions & 3 deletions packages/core/src/config/loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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);
/**
* @private
*
* Only exported for testing purposes. Do not import.
*/
export const require = createRequire(import.meta.url);

type TypeScript = typeof TS;

Expand Down Expand Up @@ -75,8 +81,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);
Expand Down

0 comments on commit d8f63e9

Please sign in to comment.