GithubHelp home page GithubHelp logo

Comments (4)

tmikaeld avatar tmikaeld commented on May 5, 2024 1

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.

from esbuild.

evanw avatar evanw commented on May 5, 2024

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].

from esbuild.

gabrielalmeida avatar gabrielalmeida commented on May 5, 2024

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;

from esbuild.

evanw avatar evanw commented on May 5, 2024

@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.

from esbuild.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.