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

Fix esm build to make it work with node #2922

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wfalcon0x
Copy link

@wfalcon0x wfalcon0x commented Nov 1, 2023

Closes #???

Description

Previous PR #2815 has been provided a solution to build cadence-parser in a way that supports running it in nodejs.

An alternative solution has been merged #2851, which unfortunately was still not working with nodejs and failed with the following error:

const result = global[_CadenceParser.functionName("parse")](code);      
TypeError: global[_CadenceParser.functionName(...)] is not a function

This PR provides with a similar approach to #2851, but making sure the solution works both in browser and nodejs.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@jribbink
Copy link
Contributor

jribbink commented Nov 1, 2023

Hey @wfalcon0x! Thanks for the PR!

From your error it looks like the package is actually importing correctly, and instead, this is a runtime error. Specifically, it looks like you may not have loaded the WASM module into the parser before trying to use the CadenceParser.parse function.

Can you try this code? (Make sure to convert it to ESM if you prefer to use that instead)

const {CadenceParser} = require("@onflow/cadence-parser")
const fs = require("fs")

const file = fs.readFileSync("./node_modules/@onflow/cadence-parser/dist/cadence-parser.wasm", "binary")
CadenceParser.create(Buffer.from(file, "binary")).then((parser) => {
  console.log(parser.parse("pub fun main() {}"))
})

Let me know if this solves your issue :)

@wfalcon0x
Copy link
Author

wfalcon0x commented Nov 1, 2023

Hey @jribbink ,

Thank you for your reply. After converting your CommonJS example to ESM I'm getting exactly the same error.

I'm using the cadence-parser from Node.js ES module (ESM), and my code looks the following:

import {CadenceParser} from "@onflow/cadence-parser"
import * as fs from "fs"

const wasm = fs.readFileSync('./node_modules/@onflow/cadence-parser/dist/cadence-parser.wasm')
const parser = await CadenceParser.create(wasm)
...

There are additional details about the original issue in my previous PR: #2815.
Unfortunately the solution that has been picked to be merged instead, doesn't work with Node.js ES module.

I have tested this and my original PR with both in browser and with Node.js.

I have also published the solution created in this PR here:
https://www.npmjs.com/package/@wfalcon/cadence-parser/v/0.0.17

It works perfectly with the following code from a Node.js ES module:

import {CadenceParser} from "@wfalcon/cadence-parser"
import * as fs from "fs"

const wasm = fs.readFileSync('./node_modules/@wfalcon/cadence-parser/dist/cadence-parser.wasm')
const parser = await CadenceParser.create(wasm)
...

Please note, package.json "type" value need to be set to "module", which tells Node.js to interpret .js files within that package as using ES module syntax.

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

Successfully merging this pull request may close these issues.

None yet

2 participants