Skip to content

bug(typescript): Breaks on .d.ts declaration inputs #992

Closed

Description

@ctjlewis

Expected Behavior

The plugin should not break on .d.ts inputs, including module declarations.

Actual Behavior

The plugin throws:

src/env.d.ts → dist...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/env.d.ts (1:8)
1: declare module test {}
           ^

Additional Information

I would happily submit a PR quickly for this issue if @shellscape, @lukastaegert, or @Andarist have any thoughts on where to start.

Activity

  1. ctjlewis commented on Sep 5, 2021

    @ctjlewis
    ContributorAuthor

    Thought this might've been something weird with the module keyword, but it doesn't appear to be, as declare const ... also throws:

    src/env.d.ts → dist...
    [!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    src/env.d.ts (2:8)
    1: // declare module test {}
    2: declare const helloWorld: RegExp;
               ^
    
  2. changed the title [-]bug(typescript): Breaks on declaration inputs[/-] [+]bug(typescript): Breaks on .d.ts declaration inputs[/+] on Sep 5, 2021
  3. ctjlewis commented on Sep 5, 2021

    @ctjlewis
    ContributorAuthor

    Very confusing stuff. declare statements are (of course) supported in regular TS files, so adding a declare statement to src/input.ts does not change anything—but .d.ts files with only declare statements break completely.

    It's pretty unfeasible to run a Rollup pass on this TS project if it can't eat the env.d.ts file. Shocked this has been in production this long?

    If any of this logic is ringing any bells for anyone, I can have a PR in by tomorrow for sure with a little guidance—if I don't hear back I'll just start dumpster diving through the plugin source, probably stick to same timeframe.

  4. shellscape commented on Sep 5, 2021

    @shellscape
    Collaborator

    Side Note: Please use the "edit" feature on issues to cut down on notification noise. It's also easier to track all info if it's not split up between N successive replies

  5. FPDK commented on Oct 10, 2021

    @FPDK

    @ctjlewis Did you find a solution? I'm dealing with the same issue right now.

    EDIT:
    I changed the order of my plugin that used the .d.ts file and the problem is now resolved.

  6. stale commented on Dec 11, 2021

    @stale

    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. ⓘ

  7. wakaztahir commented on Jul 13, 2023

    @wakaztahir

    Still an issue , This is required for example I am building a component library , I need to also export a type with styled-components to override the DefaultTheme type

  8. samboylett commented on Sep 14, 2023

    @samboylett

    Still an issue , This is required for example I am building a component library , I need to also export a type with styled-components to override the DefaultTheme type

    This is exactly my use case. I have a modules.d.ts which declares the types fine while developing, but I need to build/publish this with the rest of the code. Just importing it to index.ts results in the above error

  9. tangerball commented on Dec 11, 2024

    @tangerball

    Still an issue , This is required for example I am building a component library , I need to also export a type with styled-components to override the DefaultTheme type

    This is exactly my use case. I have a modules.d.ts which declares the types fine while developing, but I need to build/publish this with the rest of the code. Just importing it to index.ts results in the above error

    This happens to me too, is there a way to fix this problem?

  10. tangerball commented on Dec 11, 2024

    @tangerball

    @ctjlewis Did you find a solution? I'm dealing with the same issue right now.

    EDIT: I changed the order of my plugin that used the .d.ts file and the problem is now resolved.

    Is there any example? I adjusted the plugin and the problem still occurs

  11. ctjlewis commented on Dec 11, 2024

    @ctjlewis
    ContributorAuthor
  12. tangerball commented on Dec 13, 2024

    @tangerball

    Lin, the solution is to not use Rollup. Use Bun. Get Outlook for iOShttps://aka.ms/o0ukef
    …
    ________________________________ From: Lin Xiaoyan @.> Sent: Tuesday, December 10, 2024 6:47:54 PM To: rollup/plugins @.> Cc: Lewis @.>; Mention @.> Subject: Re: [rollup/plugins] bug(typescript): Breaks on .d.ts declaration inputs (#992) @ctjlewishttps://github.com/ctjlewis Did you find a solution? I'm dealing with the same issue right now. EDIT: I changed the order of my plugin that used the .d.ts file and the problem is now resolved. Is there any example? I adjusted the plugin and the problem still occurs — Reply to this email directly, view it on GitHub<#992 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAMUTFEHCTON47AJYNS7T3T2E6DTVAVCNFSM6AAAAABTMK6VQOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMZTGM3DGOJRGE. You are receiving this because you were mentioned.Message ID: @.***>

    I found a solution to process .d.ts files using transformers in @rollup-plugin-typescript

    import typescript from '@rollup/plugin-typescript'
    
    // Determines whether a node is a reference to a .d or .d.ts file
    function isDtsReference(node) {
      return (
        (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
        node.moduleSpecifier &&
        (node.moduleSpecifier.text.endsWith('.d') || node.moduleSpecifier.text.endsWith('.d.ts'))
      )
    }
    
    // Custom TypeScript transformer to remove references to .d and .d.ts files
    function removeDtsReferences() {
      return (context) => {
        return (sourceFile) => {
          function visitor(node) {
            // If the node is a reference to a .d or .d.ts file, remove the node
            if (isDtsReference(node)) {
              return undefined
            }
            // Recursively visit child nodes
            return ts.visitEachChild(node, visitor, context)
          }
          // Start accessing from the root node
          return ts.visitNode(sourceFile, visitor)
        }
      }
    }
    
    const config = {
      input:'xxx',
      plugins:[
          typescript({
            transformers: {
              before: [removeDtsReferences()],
            },
          })
        ]
    }
    export default config
    
  13. tangerball commented on Dec 13, 2024

    @tangerball

    Still an issue , This is required for example I am building a component library , I need to also export a type with styled-components to override the DefaultTheme type

    You can check the above solution, I don’t know if it can help you

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions