SukkaW / rollup-plugin-swc

Use SWC with Rollup to transform / minify ESNext and TypeScript code.
MIT License
154 stars 15 forks source link

SWC error: `jsc.baseUrl` must be absolute #41

Closed maslennikov closed 1 year ago

maslennikov commented 1 year ago

Problem With the recent update(issues here and here), swc doesn't accept relative baseUrl anymore. When rollup plugin reads tsconfig.json, it passes baseUrl in whatever format is there, and normally, it is something like . or ./src. It leads to errors:

[!] (plugin swc) Error: failed to handle: base_dir(./) must be absolute. Please ensure that jsc.baseUrl is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if .swcrc is used, but if not, there are many candidates. e.g. the directory containing package.json, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.

Suggested solution If tsconfig file found, resolve baseUrl as absolute path (value from its compilerOptions.baseUrl field resolved against absolute path of the given tsconfig file)

maslennikov commented 1 year ago

Right now I solved this with manual baseUrl resolution in base config file shared across monorepo packages:

import fs from 'node:fs'
import path from 'node:path'
import json5 from 'json5'

// HACK https://github.com/SukkaW/rollup-plugin-swc/issues/41
const tsconfig = json5.parse(fs.readFileSync(TSCONFIG_PATH))
const baseUrl = path.resolve(tsconfig.compilerOptions.baseUrl)

export default {
  // rest skipped
  plugins: [
    swc({
      tsconfig: TSCONFIG_PATH,
      jsc: {
        baseUrl,
      },
    }),
  ],
}
SukkaW commented 1 year ago

Thanks for reporting this!

However, rollup-plugin-swc3 also supports extends in the tsconfig.json as well. So keeping the previous behavior might not be that easy.

theIYD commented 11 months ago

@SukkaW I am using rollup-plugin-dts to generate the declaration files. With this plugin, the above error is thrown.

plugin version: 0.10.2