profile
viewpoint

Ask questions@swc/helpers throws error in ESM project

Describe the bug

While attempting to run unit tests with @swc/jest, I noticed that an error occurs when running the CommonJS output from swc in an ESM project.

Input code

import foo from './package.json';
console.log(foo);

Config

{
  "jsc": {
	"externalHelpers": true,
    "transform": {
      "hidden": {
        "jest": true
      }
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Expected behavior

The code should run without errors.

Actual behavior

Output:

"use strict";
var swcHelpers = require("@swc/helpers");
var _foo = swcHelpers.interopRequireDefault(require("./foo"));
console.log(_foo.default);

Result:

var _packageJson = swcHelpers.interopRequireDefault(require("./package.json"));
                              ^

TypeError: swcHelpers.interopRequireDefault is not a function
    at Object.<anonymous> (/tmp/swc-bug.cjs:3:31)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

Version The version of @swc/core:

@swc/cli: 0.1.27 @swc/core: 1.2.36 @swc/helpers: 0.2.2

node version: 14.12.0

swc-project/swc

Answer questions avaly

The same behavior is present with 0.2.0

useful!
source:https://uonfu.com/
Github User Rank List