Comments (5)
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.
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.
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.
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 tsconfig
s.
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.
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)
- Because version 3.0 already released, it's time to remove this option
- Release 3.02 HOT 1
- [docs] Parens create a pluck context HOT 7
- Run tests on es modules HOT 3
- Conversion of a pegjs file to BNF or vice versa ? HOT 2
- Type of `ParserBuildOptions` is wrong HOT 3
- [feature request] watch HOT 3
- doc example doesn't correspond to code example HOT 3
- Fix all uses of String.prototype.substr HOT 5
- Performance javascript grammar HOT 7
- test output format HOT 4
- use `expression1 / expression2 / ... / expressionn` throw `peg$SyntaxError: Expected end of input but "d" found.` HOT 5
- Playground syntax highlight grammar breaks when using template literals HOT 4
- tsconfig targets es5, but allows es2020 functions
- Documentation example for 'Negative assertion' gives an error in Playground
- `-o dist/my-parser.js` errors if `dist/` doesn't exist
- can i use peggy js with other languages like (zig-lang,c,c++ etc) HOT 1
- Invalid character range: \x80-\x10fffd HOT 3
- error location at diffrent place between online-editor and parsed-error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peggy.