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

Named namespace and function import doesn't work. #24

Closed
tmikaeld opened this issue Feb 19, 2020 · 4 comments
Closed

Named namespace and function import doesn't work. #24

tmikaeld opened this issue Feb 19, 2020 · 4 comments

Comments

@tmikaeld
Copy link

This works:

import KJUR from 'jsrsasign';
import { b64toBA, b64utob64 } from 'jsrsasign';

This doesn't:

import { KJUR, b64toBA, b64utob64 } from 'jsrsasign';

KJUR is a namespace, while the others are functions or classes.

Webpack handles this as expected.

@tmikaeld
Copy link
Author

I'm guessing this is the right way to use this:

import KJUR, { b64toBA, b64utob64 } from 'jsrsasign';

Feel free to close the issue if you don't want to handle both.

@evanw
Copy link
Owner

evanw commented Apr 7, 2020

It's possible that this was fixed earlier. I'm guessing the fix was adding support for CommonJS default exports via __esModule in #17.

I'm going to close this since it looks like this is working fine now:

import KJUR from 'jsrsasign';
import { b64toBA, b64utob64 } from 'jsrsasign';
console.log(KJUR.crypto.DSA, b64toBA, b64utob64);
import { KJUR, b64toBA, b64utob64 } from 'jsrsasign';
console.log(KJUR.crypto.DSA, b64toBA, b64utob64);
import KJUR, { b64toBA, b64utob64 } from 'jsrsasign';
console.log(KJUR.crypto.DSA, b64toBA, b64utob64);

When bundled with esbuild using --bundle and then executed using node, all three of these output the same thing: [Function (anonymous)] [Function: b64toBA] [Function: b64utob64].

@evanw evanw closed this as completed Apr 7, 2020
@gabrielalmeida
Copy link

I've run into a situation where re-exported namespaces are not bundled properly as named exports when using barrel files and export star syntax.

When doing this:

import {
  namedPropFromB,
  proto
} from "pkg";

I'd get the following on node runtime:
SyntaxError: The requested module 'pkg' does not provide an export named 'proto'

It will build and generate valid DTS about the exported proto NS.


Original pkg exports are as follows:

index.js

import A from './A'

export * from '../proto'
export * from './B'

export default A

Proto is generated by pbjs from protobufjs, not to confuse with pbjs also from @evanw:

yarn pbjs -t static-module -w commonjs -o ./Proto/index.js ./Proto/Proto.proto;
yarn pbts -o ./Proto/index.d.ts ./Proto/index.js;
...
// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});

$root.proto = (function() {

    /**
     * Namespace proto.
     * @exports proto
     * @namespace
     */
    var proto = {};

     proto.Something = (function() {
       ...
    }

    return proto;
})();

module.exports = $root;

esbuild bundle:

...
Object.defineProperty(exports, "__esModule", { value: true });
const A_1 = __importDefault(require("./A"));
__exportStar(require("./B"), exports);
__exportStar(require("../Proto"), exports);
exports.default = A_1.default;

I'd expect proto to be accessible as a named export but it won't. The only way to use its types and runtime methods is:

import {
  namedPropFromB,
  // proto // typescript finds it but node will fail on runtime
} from "pkg";

import * as pkg from "pkg";

const sample = pkg.proto.Something.Method(value) as pkg.proto.Something.ISomething;

@evanw
Copy link
Owner

evanw commented Jun 9, 2023

@gabrielalmeida You should file a new issue if you believe you have found a bug with esbuild. It would also be helpful to have a self-contained reproduction on https://esbuild.github.io/try/.

  // proto // typescript finds it but node will fail on runtime

Keep in mind that you need --platform=node when building for node. In this case esbuild generates annotations for node so node's syntactic CommonJS analyzer can find the re-exported export names: #2486.

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