swc-project / plugins

Plugins for swc, written in rust
Apache License 2.0
334 stars 55 forks source link

`@swc/plugin-transform-imports` is missing support for namespace imports/exports and default imports #323

Closed dp-152 closed 4 months ago

dp-152 commented 4 months ago

Currently, it appears the transform-imports plugin only supports named imports and exports. Namespace and default imports/exports are completely ignored. By extension, mixed imports (import foo, { bar } from "module-name" or import foo, * as bar from "module-name") are also left untouched.

This is a problem when using the plugin:

Currently, the following examples:

// Works - outputs `import { someFunction } from "./somefile.mjs"`
import { someFunction } from "./somefile.ts";

// Works - outputs `import { someOtherFunction as someOtherFn } from "./somefile.mjs"`
import { someOtherFunction as someOtherFn } from "./somefile.ts";

// Works - outputs `export * from ./somefile.mjs`
export * from "./somefile.ts";

// Works - outputs `export {someFunction} from "./somefile.mjs"`
export { someFunction } from "./somefile.ts";

are transformed as expected, while

// Does not work - outputs `import defaultFunction from "./somefile.ts"`
import defaultFunction from "./somefile.ts";

// Does not work - outputs `import defaultFunction, { someFunction, someOtherFunction as someOtherFn } from "./somefile.ts"`
import defaultFunction, { someFunction, someOtherFunction as someOtherFn } from "./somefile.ts";

// Does not work - outputs `import defaultFunction, * as someModule from "./somefile.ts"`
import defaultFunction, * as someModule from "./somefile.ts";

// Does not work - outputs `export * as someModule from "./somefile.ts"`
export * as someModule from "./somefile.ts";

are not touched by the plugin.

This can be verified using @swc/plugin-transform-imports@2.0.7 (@swc/core@1.6.6), with the following .swcrc config:

{
  "module": {
    "type": "es6"
  },
  "jsc": {
    "target": "esnext",
    "parser": {
      "syntax": "typescript"
    },
    "experimental": {
      "plugins": [
        [
          "@swc/plugin-transform-imports",
          {
            "^(..?\\/.*?)(\\.[jt]sx?)?$": {
              "skipDefaultConversion": true,
              "transform": "{{matches.[1]}}.mjs"
            }
          }
        ]
      ]
    }
  }
}

Minimum reproduction example: https://github.com/dp-152/swc-plugin-transform-imports-repro