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

Can not export the name-imported with default in transpiled cjs #3029

Closed
snowyu opened this issue Mar 31, 2023 · 6 comments
Closed

Can not export the name-imported with default in transpiled cjs #3029

snowyu opened this issue Mar 31, 2023 · 6 comments

Comments

@snowyu
Copy link

snowyu commented Mar 31, 2023

The ESM js files to transpile:

// fn.js
export function fn() {}
export default fn
// fn1.js
export function fn1() {}
export default fn1
// index.js
export * from './fn'
export * from './fn1'
// package.json
{
  "name": "util-ex",
  "main": "./lib/index.js",
  "module": "./src/index.js",
  "type": "commonjs",
}

Used it in another esm package after transpiled, found no cloneObject exported. exported only after commenting the export default cloneObject

import { fn } from 'util-ex'
console.log(fn) // is undefined
@hyrious
Copy link

hyrious commented Apr 1, 2023

You mentioned "transpiled", what are the actual files in runtime? What runtime is it? It sounds like #2486 where esbuild currently does not emit any code for node.js to infer re-exports from cjs code. But you can provide a self-contained reproduction to make it clear.

In fact, the transform mode does not produce the "unbundled" result (or let's call it preserve modules as in rollup, which is not supported yet: #708). Any try to use it like that will likely to fail.

@evanw evanw closed this as completed in b86e581 Apr 1, 2023
@snowyu
Copy link
Author

snowyu commented Apr 2, 2023

@evanw 0.17.15 Still Can not export the name-imported, report error now: SyntaxError: Named export 'fn' not found. The requested module 'util-ex' is a CommonJS module, which may not support all module.exports as named exports.

But can workaround:

import util from 'util-ex';
const fn = util.fn;

If wanna named exported on node, do not overrite the module.exports.

@evanw
Copy link
Owner

evanw commented Apr 2, 2023

I added a test case based on your incomplete reproduction: b86e581#diff-6058dc2cc79db56a46212b92b928329c09ac9f8a3f41c6ce8edbafb3d6050613R1990-R2023. That test case failed before 0.17.15 and passed with 0.17.15, which is why I considered this issue to be fixed.

If you still believe that there is a bug with esbuild, then please provide a complete reproduction. That would include at minimum all relevant files and all instructions necessary to reproduce the bug.

@snowyu
Copy link
Author

snowyu commented Apr 2, 2023

@hyrious
Copy link

hyrious commented Apr 2, 2023

Your repl has a mistake in package.json "type": "module", which indicates that all .js files are in ESM. Node.js will try to load ./dist/lib.js with ESM loader and it cannot find any named exports apparently.

@snowyu
Copy link
Author

snowyu commented Apr 5, 2023

@hyrious which means other package(esm) call the cjs pacakge! The SWC has the same issue, but It fixed now since I've reported.

You can chagne the js to cjs in the lib dir is the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants