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

Does this work with @rollup/plugin-typescript ? #13

Closed
lgirma opened this issue Jul 15, 2021 · 3 comments
Closed

Does this work with @rollup/plugin-typescript ? #13

lgirma opened this issue Jul 15, 2021 · 3 comments

Comments

@lgirma
Copy link

lgirma commented Jul 15, 2021

Asking this since the @rollup/plugin-typescript plugin supports transformers option for typescript.

I tried but getting this error:

TypeError: Cannot read property 'getTypeChecker' of undefined
@lgirma
Copy link
Author

lgirma commented Jul 15, 2021

Up on further investigation, I got it to work using (in rollup config):

import typescript from '@rollup/plugin-typescript'

...

plugins: [typescript({
  transformers: {
    before: [ { type: 'program', factory: (program) => di({program}).before[0] } ],
    after: [ { type: 'program', factory: (program) => di({program}).after[0] } ]
  }
})]

This generates the correct bundle like:

container.registerSingleton(undefined, { identifier: 'HackerNewsService', implementation: HackerNewsService });

But the javascript code for all of my classes (like HackerNewsService above) are omitted. And thus running into HackerNewsService is not defined errors.

@wessberg
Copy link
Owner

wessberg commented Jul 15, 2021

Hi there,

The DI-Compiler must be passed a TypeScript Program since it depends on a type checker. That's why you need to pass the program on to it.

Your example doesn't work, though, as you pass the DI Custom Transformer twice, one for the before hook and one for the after hook. That's not correct usage of it. The DI custom transformer must be passed a program, and it itself returns a structure of the form {before, after}.

If you experience issues with interoperability between @rollup/plugin-typescript and Custom Transformers such as DI-Compiler, I suggest that you open an issue with a minimal repro over at the Rollup plugins monorepo. Specifically, it should be able to handle Custom Transformers that implement multiple hooks, such as DI-compiler, that does things in both the before and after hooks.

In the meantime, you can use DI-Compiler with rollup-plugin-ts which is tested and verified to be working correctly.

@SitamMatt
Copy link

Hi,

I've wrote a workaround helper for @rollup/plugin-typescript to easly apply this transformer. Here is the npm link

Usage:

import typescript from "@rollup/plugin-typescript";
import { di } from "@wessberg/di-compiler";
import { applyTransformer } from "rollup-typescript-custom-transformer-helper";

(...)

typescript({
  sourceMap: true,
  inlineSources: true,
  transformers: {
    ...applyTransformer((program) => di({ program })),
  },
}),

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

No branches or pull requests

3 participants