mentaljam / rollup-plugin-swc

Rollup plugin to compile bundles with the SWC.
https://www.npmjs.com/package/rollup-plugin-swc
MIT License
74 stars 8 forks source link

Error: Could not resolve typescript, tsx and jsx files #4

Open aelbore opened 4 years ago

aelbore commented 4 years ago

Please see PR #3

(async function() {

  const mockfs = require('mock-fs')
  const rollup = require('rollup')
  const swc = require('rollup-plugin-swc')

  mockfs({
    './src/foo.ts': `
      export const foo = () => 'Hello World' 
    `,
    './src/index.ts': `
      import { foo } from './foo'

      console.log(foo())
    `
  })

  const bundle = await rollup.rollup({
    input: './src/index.ts',
    plugins: [ 
      swc({
        jsc: {
          parser: {
            syntax: 'typescript',
          },
          target: 'es2018',
        }
      })
    ]
  })

  const { output } = await bundle.generate({ format: 'es' })
  const { code } = output.shift()

  console.log(code)

  /// actual 
  /// (node:31839) UnhandledPromiseRejectionWarning: 
  ///   Error: Could not resolve './foo' from src/index.ts

  /// expected
  /// should have no error
  /// should resolve the path

})()
mentaljam commented 3 years ago

Hi,

Sorry for not responding for so long. What about using @rollup/plugin-node-resolve like in the example?

abenz1267 commented 3 years ago

didn't wanna open up a new issue... so i'm hijacking this as it's SOMEWHAT related.

I can't build, because imports are missing the *.ts extension, which Typescript omits by default. I'd like to keep it that way. How can i make SWC resolve those imports correctly? Or do i have to add the extension to every import now?

If i add extensions: ['.ts'], to the nodeResolve plugin like in the example, it can't resolve node_modules imports. Tries to resolve "repeat.ts" instead of "repeat.js" as in the actual import.

mentaljam commented 3 years ago

@abenz1267, have you tried extensions: ['.js', '.ts']?

abenz1267 commented 3 years ago

Oh yeah, i did.

Error: Could not load /home/andrej/Documents/litdo/node_modules/lit/directives/repeat.ts (imported by src/index.ts): ENOENT: no such file or directory, open '/home/andrej/Documents/litdo/node_modules/lit/directives/repeat.ts'

Is the whole error. Keep in mind the actual import calls repeat.js not repeat.ts.

On 14 Jul 2021, 13:28 +0200, Petr Tsymbarovich @.***>, wrote:

@abenz1267, have you tried extensions: ['.js', '.ts']? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

matthew-dean commented 3 years ago

Any update on this? Ideally, it should not require @rollup/plugin-node-resolve for relative files.

matthew-dean commented 3 years ago

Related: even using @rollup/plugin-node-resolve, I couldn't get this plugin / SWC to resolve index files, like: ./src/ui/Button/index.ts when using

import { Button } from '@ui/Button'

Is this a SWC issue, and regardless, is there a way to get SWC to resolve paths using other rollup plugins? It doesn't seem like module resolution in SWC uses the Rollup flow to resolve paths.