Skip to content
Closed
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
3 changes: 3 additions & 0 deletions fixtures/dts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@fixtures/dts"
}
1 change: 1 addition & 0 deletions fixtures/dts/src/assets.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '*.html?raw';
1 change: 1 addition & 0 deletions fixtures/dts/src/block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>content</div>
4 changes: 4 additions & 0 deletions fixtures/dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import b from './block.html?raw';
import n from './normal';

console.log(b, n);
1 change: 1 addition & 0 deletions fixtures/dts/src/normal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 1;
6 changes: 6 additions & 0 deletions fixtures/dts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"include": ["./src/assets.d.ts", "./src"],
"compilerOptions": {
"skipLibCheck": true
}
}
1 change: 0 additions & 1 deletion src/ProjectPrincipal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const baseCompilerOptions = {
esModuleInterop: true,
skipDefaultLibCheck: true,
skipLibCheck: true,
lib: [],
target: ts.ScriptTarget.Latest,
module: ts.ModuleKind.CommonJS,
moduleResolution: ts.ModuleResolutionKind.NodeNext,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {

deputy.addWorkspace({ name, dir, manifestPath, manifest, ignoreDependencies, ignoreBinaries });

const compilerOptions = await loadCompilerOptions(join(dir, tsConfigFile ?? 'tsconfig.json'));
const { compilerOptions, definitionPaths } = await loadCompilerOptions(join(dir, tsConfigFile ?? 'tsconfig.json'));

const principal = factory.getPrincipal({ cwd: dir, paths, compilerOptions, compilers });

// Pushing declaration files to the program as source files seems not enough
principal.addEntryPaths(definitionPaths);

const worker = new WorkspaceWorker({
name,
dir,
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { hasDependency, load } from '../../util/plugin.js';
import { loadTSConfig } from '../../util/tsconfig-loader.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';
import type { TsConfigJson } from 'type-fest';
import type { CompilerOptions } from 'typescript';

// https://www.typescriptlang.org/tsconfig

Expand Down Expand Up @@ -34,7 +33,7 @@ const resolveExtensibleConfig = async (configFilePath: string) => {
};

const findTypeScriptDependencies: GenericPluginCallback = async configFilePath => {
const compilerOptions: CompilerOptions = await loadTSConfig(configFilePath);
const { compilerOptions } = await loadTSConfig(configFilePath);
const config: TsConfigJson = await resolveExtensibleConfig(configFilePath); // Dual loader to get external `extends` dependencies

if (!compilerOptions || !config) return [];
Expand Down
1 change: 1 addition & 0 deletions src/typescript/SourceFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class SourceFileManager {

createSourceFile(filePath: string, contents: string) {
const setParentNodes = isInternal(filePath);
// When added explicitly, declaration files are created properly with `.isDeclarationFile: true`
const sourceFile = ts.createSourceFile(filePath, contents, ts.ScriptTarget.Latest, setParentNodes);
this.sourceFileCache.set(filePath, sourceFile);
return sourceFile;
Expand Down
1 change: 1 addition & 0 deletions src/typescript/getImportsAndExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const getImportsAndExports = (sourceFile: BoundSourceFile, options: GetIm
const { specifier, symbol, identifier = '__anonymous', isReExport = false } = options;
if (isBuiltin(specifier)) return;

// The specifier exists, but no resolved module
const module = sourceFile.resolvedModules?.get(specifier, /* mode */ undefined);

if (module?.resolvedModule) {
Expand Down
1 change: 1 addition & 0 deletions src/typescript/resolveModuleNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function createCustomModuleResolver(
}

function resolveModuleName(name: string, containingFile: string): ts.ResolvedModule | undefined {
// How to let TypeScript know about the declaration files to resolve e.g. the `*.html?raw` import specifier?
const tsResolvedModule = ts.resolveModuleName(name, containingFile, compilerOptions, ts.sys).resolvedModule;

if (virtualFileExtensions.length === 0) return tsResolvedModule;
Expand Down
12 changes: 10 additions & 2 deletions src/util/tsconfig-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ export const loadTSConfig = async (tsConfigFilePath: string) => {
if (isFile(tsConfigFilePath)) {
const config = ts.readConfigFile(tsConfigFilePath, ts.sys.readFile);
const parsedConfig = ts.parseJsonConfigFileContent(config.config, ts.sys, dirname(tsConfigFilePath));
return parsedConfig.options ?? {};
const compilerOptions = parsedConfig.options ?? {};

// Local declaration files pushed to `lib: []` seem not be taken into account by ts.resolveModuleName()
// const definitionPaths = parsedConfig.fileNames.filter(filePath => filePath.endsWith('.d.ts'));
// compilerOptions.lib = compilerOptions.lib ?? [];
// compilerOptions.lib.push(...definitionPaths);
const definitionPaths: string[] = [];

return { compilerOptions, definitionPaths };
}
return {};
return { compilerOptions: {}, definitionPaths: [] };
};