microsoft / microsoft/TypeScript

Add method host.createCustomSourceFile for custom file extensions

Open
#52,983 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Suggestion

Hello, I like the new option --allowArbitraryExtensions in 5 TS, but this only works for created files on the file system or overriding a set of fs methods (readDirectory , readFile , etc).

Generating files on real time is not very efficient, so we have to override different methods so that the TS receives the created instance SourceFile without creating a file on filesystem.

I propose to add a new method createCustomSourceFile to the host, which is undefined by default, if the user has defined this method in the host, then the TS should call it for all files with extensions that the TS does not support (.css, .svg, .vue, etc).

📃 Motivating Example

host.createCustomSourceFile = function (fileName, sourceText): SourceFile {
  switch (fileName.slice(fileName.lastIndexOf('.'))) {
    case '.css':
      return ts.createSourceFile(fileName, getDtsOfCss(sourceText));

    case '.svg':
      return ts.createSourceFile(fileName, getDtsOfSvg(sourceText));
  }
};

💻 Use Cases

  1. Bundle systems that always take care of importing files that TS doesn't support.
  2. Custom language servers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file or test; start by tracing the host file-loading path and the existing fs overrides used with --allowArbitraryExtensions. Compare how unsupported extensions are handled, then define the host API behavior and tests for an optional createCustomSourceFile callback that supplies SourceFile instances without filesystem creation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.