Skip to content
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

Two named exports creates invalid CommonJS #7665

Closed
snyamathi opened this issue Jul 17, 2023 · 3 comments
Closed

Two named exports creates invalid CommonJS #7665

snyamathi opened this issue Jul 17, 2023 · 3 comments
Assignees
Labels
Milestone

Comments

@snyamathi
Copy link

Describe the bug

Two or more named exports will yield a file that cannot be parsed by the CJS lexer and has issues with being imported.

Input code

export const foo = 'foo';
export const bar = 'bar';

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript"
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Playground link

https://play.swc.rs/?version=1.3.66&code=H4sIAAAAAAAAA0utKMgvKlFIzs8rLlFIy89XsFVQB1Lq1lypyDJJiUUgGSClbg0Aikj%2FLjMAAAA%3D&config=H4sIAAAAAAAAA6vmUlBQyipOVrJSqAYygZyCxKLi1CI4HyhSXJlXklgBFFFKTc5NLE4uyiwoUQJL1gLJWh2QEbn5KaU5qQhTSioLQDyl5Pzc3Py8rGKQ%2BlquWgCZ2NHCbwAAAA%3D%3D

Expected behavior

Typescript compiler with similar settings will output the following:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bar = exports.foo = void 0;
exports.foo = 'foo';
exports.bar = 'bar';

This is analyzable by the node CJS lexer and will work with a destructuring import.

Actual behavior

We get the following which cannot be statically analyzed and gives errors

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    foo: function() {
        return foo;
    },
    bar: function() {
        return bar;
    }
});
var foo = "foo";
var bar = "bar";
$ node --input-type=module -e "import { foo } from './index.cjs';"
file:///xxx/[eval1]:1
import { foo } from './index.cjs';
         ^^^
SyntaxError: Named export 'foo' not found. The requested module './index.cjs' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from './index.cjs';
const { foo } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async ESMLoader.eval (node:internal/modules/esm/loader:390:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Version

1.3.66

Additional context

No response

@kdy1 kdy1 self-assigned this Jul 18, 2023
@kdy1 kdy1 added this to the Planned milestone Jul 18, 2023
@kdy1
Copy link
Member

kdy1 commented Jul 18, 2023

No. This is expected. You should set interop mode to node because cjs-module-lexer, which is used by node.js to import cjs modules, is not smart enough. https://swc.rs/docs/configuration/modules#importinterop

I added it to #7435.

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.70 Jul 18, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Aug 17, 2023

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 Aug 17, 2023
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