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

Make explicit extensions in imports work. #659

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions packages/core/__tests__/cli/custom-extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ describe('CLI: custom extensions', () => {
'Greeting.gts': stripIndent`
<template>Hello!</template>
`,
're-export.gts': stripIndent`
export { default as Greeting } from './Greeting.gts';
`,
'vanilla.ts': 'export const two = 2;',
'barrel.ts': stripIndent`
export { default as Greeting } from './Greeting.gts';
export { Greeting as Greeting2 } from './re-export.gts';
export { two } from './vanilla.ts';
`,
});
});

Expand All @@ -147,6 +156,26 @@ describe('CLI: custom extensions', () => {

1 import Greeting from './Greeting.gts';
~~~~~~~~~~~~~~~~

barrel.ts:1:37 - error TS2307: Cannot find module './Greeting.gts' or its corresponding type declarations.

1 export { default as Greeting } from './Greeting.gts';
~~~~~~~~~~~~~~~~

barrel.ts:2:39 - error TS2307: Cannot find module './re-export.gts' or its corresponding type declarations.

2 export { Greeting as Greeting2 } from './re-export.gts';
~~~~~~~~~~~~~~~~~

barrel.ts:3:21 - error TS2691: An import path cannot end with a '.ts' extension. Consider importing './vanilla.js' instead.

3 export { two } from './vanilla.ts';
~~~~~~~~~~~~~~

re-export.gts:1:37 - error TS2307: Cannot find module './Greeting.gts' or its corresponding type declarations.

1 export { default as Greeting } from './Greeting.gts';
~~~~~~~~~~~~~~~~
"
`);
});
Expand All @@ -165,5 +194,35 @@ describe('CLI: custom extensions', () => {
expect(result.stderr).toBe('');
}
);

test.runIf(semver.gte(typescript.version, '5.0.0'))(
'declarations work with `allowImportingTsExtensions: true`',
async () => {
project.updateTsconfig((config) => {
config.compilerOptions ??= {};
config.compilerOptions['allowImportingTsExtensions'] = true;
});

let emitResult = await project.check({ flags: ['--declaration'] });

expect(emitResult.exitCode).toBe(0);

expect(project.read('re-export.d.ts')).toMatchInlineSnapshot(`
"export { default as Greeting } from './Greeting';
"
`);
expect(project.read('barrel.d.ts')).toMatchInlineSnapshot(`
"export { default as Greeting } from './Greeting';
export { Greeting as Greeting2 } from './re-export';
export { two } from './vanilla';
"
`);
expect(project.read('./Greeting.d.ts')).toMatchInlineSnapshot(`
"declare const _default: import(\\"@ember/component/template-only\\").TemplateOnlyComponent<never> & (abstract new () => import(\\"@glint/template/-private/integration\\").InvokableInstance<() => import(\\"@glint/template/-private/integration\\").ComponentReturn<{}>> & import(\\"@glint/template/-private/integration\\").HasContext<import(\\"@glint/template/-private/integration\\").TemplateContext<void, {}, {}, void>>);
export default _default;
"
`);
}
);
});
});
12 changes: 12 additions & 0 deletions packages/core/src/cli/perform-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ function createCompilerHost(
host.readFile = transformManager.readTransformedFile;
host.readDirectory = transformManager.readDirectory;

// Postprocess .d.ts files to temporarily patch '.gts' to '.ts'
// https://github.com/typed-ember/glint/issues/628
const tsWriteFile = host.writeFile;
const matchGtsImport = /\.gts';/gm;
host.writeFile = (fileName, data, writeByteOrderMark, onError) => {
const isDts = fileName.endsWith('.d.ts');
if (isDts && matchGtsImport.test(data)) {
data = data.replace(matchGtsImport, ".ts';");
}
tsWriteFile(fileName, data, writeByteOrderMark, onError);
};

return host;
}

Expand Down
Loading