Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed ember/resolve config from extends package #782

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading