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

Typescript ~ Project References #1036

Closed
Kabe0 opened this issue Nov 8, 2021 · 7 comments
Closed

Typescript ~ Project References #1036

Kabe0 opened this issue Nov 8, 2021 · 7 comments

Comments

@Kabe0
Copy link

Kabe0 commented Nov 8, 2021

  • Rollup Plugin Name: Typescript
  • Rollup Plugin Version: latest

Feature Use Case

Project References
https://www.typescriptlang.org/docs/handbook/project-references.html

The current version of the TypeScript plugin has no awareness of the multiple-project configuration that can be setup with a tsconfig file. This will result in errors such as Plugin typescript: @rollup/plugin-typescript TS6305: Output file '....d.ts' has not been built from source file '....ts'

Feature Proposal

Provide a toggle flag inside the typescript plugin, or detect when references is set. If set, load reference project files first before building out the main project.

ts-loader in https://github.com/TypeStrong/ts-loader/blob/47e374bab698676c62d09d515580f4c847ea157a/src/instances.ts already has a good example of this on line 489 -> buildSolutionReferences.

The command from the API is ts.createSolutionBuilder which requires a SolutionBuilderHost object as detailed here microsoft/TypeScript#31432. Then once created, the builder.build(); function is called to generate the necessary files.

I have created a very rough prototype of the function I am using for my own local project. Please take a look below...

Changed the watchProgram.ts from line 146 with the following modification

function createWatchHost(
  ts: typeof import('typescript'),
  context: PluginContext,
  {
    formatHost,
    parsedOptions,
    writeFile,
    status,
    resolveModule,
    transformers
  }: CreateProgramOptions
): WatchCompilerHostOfFilesAndCompilerOptions<BuilderProgram> {
  const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
  const diagnostics = buildDiagnosticReporter( ts, context, formatHost );

  //TODO this may also use createSolutionBuilderWithWatchHost to allow file watching.
  const hostBuilder = ts.createSolutionBuilderHost(
    ts.sys,
    createProgram,
    diagnostics
    //TODO implement reportSolutionBuilderStatus, reportWatchStatus
  );

  // Create a instance of the builder to be used to generate file changes.
  // TODO this may also use createSolutionBuilderWithWatch to watch files.
  const builder = ts.createSolutionBuilder( hostBuilder, parsedOptions.projectReferences!.map( ref => ref.path ), {verbose: true} )
  // ts.createSolutionBuilderWithWatch( solutionBuilderHost, )
  builder.build();
  //TODO check timestamps if applicable.

  const baseHost = ts.createWatchCompilerHost(
    parsedOptions.fileNames,
    parsedOptions.options,
    ts.sys,
    createProgram,
    diagnostics,
    status,
    parsedOptions.projectReferences
  );

Putting it in createWatchHost is a bit weird, but it had all the variables ready so made it a bit easier to integrate. It is very rough, but it does work and build the modules as expected...

@Kabe0
Copy link
Author

Kabe0 commented Nov 9, 2021

Note: This is not hooking into the plugin's resolveId's so any files built by the builder here is not being added to the overall rollup stack.

It's also causing files to move around as it's following the tsconfig.json settings over the rollup settings.

@github-maxime-liege
Copy link

Following it, as i face the same problem inside my project

@Kabe0
Copy link
Author

Kabe0 commented Dec 11, 2021

@github-maxime-liege I have just written a rough prototype for rollup working with references. It's not yet on NPM as it's quite raw and I want to have some time to get a feel for how it works in our production environment, but feel free to take a look and provide input https://github.com/Kabe0/rollup-plugin-typescript-projects

@stale stale bot added the x⁷ ⋅ stale label Feb 10, 2022
@stale
Copy link

stale bot commented Feb 11, 2022

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

@stale stale bot closed this as completed Feb 11, 2022
@oxc
Copy link

oxc commented Jan 19, 2023

@Kabe0, I see that you have continued working on this in your private repository. Any chances of getting this into a PR and back into rollup? Can I help?

@Kabe0
Copy link
Author

Kabe0 commented Jan 23, 2023

@oxc
It will need some work to pull it into the main project, the Typescript API commands used for their projects at least when I wrote that plugin is totally different than what is used by this plugin. If you wanted to take a look though feel free!

I have not yet handled the build metadata files which will be needed for it to be fast. It also has some memory issues at this time.

If you have any questions about some code let me know. I might be able to spend some time to assist, but I have been pretty swamped lately. What I wrote though has been working in a project we have been using for quite some time so it is a good proof of concept.

@demurgos

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants