Skip to content

ESM(TypeScript) => CJS Doesn't Preserve Import Order #1736

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

Closed
XiNiHa opened this issue May 24, 2021 · 2 comments
Closed

ESM(TypeScript) => CJS Doesn't Preserve Import Order #1736

XiNiHa opened this issue May 24, 2021 · 2 comments
Labels

Comments

@XiNiHa
Copy link
Contributor

XiNiHa commented May 24, 2021

Describe the bug
While transpiling TypeScript code, TSC preserves the order while SWC doesn't and just puts all the imports to the top.
It can cause errors in some situations. The code below is the minimum reproduction of the problem I've faced.
I'm not sure if it's just an implementation detail of TSC or a spec but the resulting code is different and it can causes errors.

Input code
parent.ts

class ParentClass {}

import { ChildClass } from './child'

const child = new ChildClass()

child.ts

import { ParentClass } from './parent'

class ChildClass extends ParentClass {}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2018"
  },
  "module": {
    "type": "commonjs"
  }
}

Expected behavior
require() statement should be at the same place where import statement was at
(at least that's what TSC does)

Version
The version of @swc/core: 1.2.58

Additional context
Since the problem can be solved by using require() instead of import, fixing this issue isn't that needed.

@XiNiHa XiNiHa added the C-bug label May 24, 2021
@kdy1
Copy link
Member

kdy1 commented May 24, 2021

Duplicate of #1686

To follow the specification, import statements should be hoisted and tsc is simply doing wrong thing.
Official typescript compiler has a PR to fix the wrong behavior. (See microsoft/TypeScript#39764)

@XiNiHa XiNiHa closed this as completed May 24, 2021
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 24, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants