Skip to content

feat: transform commonjs to esm #2

@hyrious

Description

@hyrious

I came up with a super easy way of doing this instead of going "syntheticNamedExports". To be clear I just want to replicate the node17 behavior: use cjs-module-lexer to support partial of the cjs modules. The transformed module will be like:

var exports = {}, module = {exports}
// have to use { block } to prevent names conflict
{
  // original code
}
export default module.exports
// analyzed possible named exports
export var useState = module.exports.useState
...

The downside is obvious: some modules will cause error at runtime when it doesn't follow the pattern. But the advantage is we can reduce some verbosing code about __commonJS. I can make this transformation optional.

Those requires still need the trick in this repo. But I want to add additional info here:

try { require("x") } catch {}

Code like this won't be transformed to import statement because esbuild allowed this pattern and it won't warn about it. Frankly speaking, even "require is undefined" won't cause error at runtime when it was caught like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions