Giter Club home page Giter Club logo

Comments (5)

nathancarter avatar nathancarter commented on August 12, 2024 1

The documentation, under the heading JavaScript API, literally says you can use a require-style or import-style way to load the module. If one of the two is out of scope, the docs should make that clear rather than recommending the reader to do what's not supported.

More importantly, doing what's in the docs fails with an error. So regardless of the official position on ES6, the docs currently recommend writing code that will fail in its first 2 lines. (To be specific, the import line doesn't fail, but the next line of code that builds a parser fails because the import didn't bring in the kind of thing that was intended.)

Finally, if you make the change to the docs that I suggested in my original issue report, then everything actually works correctly thereafter. So I don't think there's any reason to shy away from ES6 support, since...well, since you have it already! :)

from peggy.

reverofevil avatar reverofevil commented on August 12, 2024

The correct ES6-style use is actually

No, it's not. ES6 uses that syntax with default export while there is no default export in peggy. Not to mention it's not ES6 module library at all.

from peggy.

hildjj avatar hildjj commented on August 12, 2024

Hm. I just started a new project with npm init -y, then installed peggy with npm i -D peggy, then created a file called t.mjs containing:

import peggy from "peggy"

const g = peggy.generate("foo = '1'")
console.log(g.parse('1'))

which when executed with node t.mjs outputs "1".

import * as peggy from "peggy" fails, since peggy contains something like: { default: { generate: [function] } }

So I think this is a valid complaint.

from peggy.

reverofevil avatar reverofevil commented on August 12, 2024

There is no mentions of default in its source code. The environment (node, webpack) can use arbitrary compatibility layers between CommonJS and ES modules (different even under different settings!), and there is no way to know on the CommonJS library side which one is applied by an environment.

Even though documentation starts talking about Node, where *-import is incorrect, further it also mentions a browser, where we can't possibly know it. I think even under ts-node it can work either way with different tsconfigs.

It doesn't seem to me to be a problem of documentation. Probably it would be better to set up a separate ESM entry point in package.json that works for both types of import. As it is right now, this is a CommonJS library, and import issues are completely out of scope.

from peggy.

hildjj avatar hildjj commented on August 12, 2024

In the browser, most people will use the pre-bundled version in browser/peggy.min.js. We could be more careful about recommending that, and we should probably put a browser entry into the package.json pointing to it.

Node's docs say: "When importing CommonJS modules, the module.exports object is provided as the default export"

In Typescript, you do need to do import * as peggy from "peggy" unless you've got allowSyntheticDefaultImports enabled.

All of this could be made clearer in the docs.

By the way, I disagree that es6 users of Peggy are out of scope, but I don't think that matters that much here.

from peggy.

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.