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

feat: esm support #161

Closed
wants to merge 5 commits into from
Closed

feat: esm support #161

wants to merge 5 commits into from

Conversation

n1ru4l
Copy link
Member

@n1ru4l n1ru4l commented Feb 12, 2021

I noticed that the esm version was broken due to @restart/hooks not being esm compatible and the es module export entry in the package.json missing.

I removed the @restart/hooks dependency in favor of a simply inline of the logic and also adjusted the package.json for the es export.

With those changes, relay-hooks can be successfully used with snowpack.

@morrys
Copy link
Member

morrys commented Feb 12, 2021

Hi @n1ru4l,
thanks for your PR, I didn't add the module in the package because I was waiting for help to verify ESM support 💯

In this PR #162 I have added the tests for useMutation so that I can better verify this PR :)

@n1ru4l
Copy link
Member Author

n1ru4l commented Feb 12, 2021

After digging more, for Node.js to be compatible the file extension of the modules must be .mjs and all the imports must specify the .mjs extension. See https://github.com/enisdenjo/graphql-ws/pull/110/files

Since React does not ship ESM modules Node.js support does not matter that much, however, this library will hopefully not require any more changes when react starts shipping esm modules :)

package.json Outdated Show resolved Hide resolved
@morrys
Copy link
Member

morrys commented Feb 15, 2021

hi @n1ru4l,
can you update the PR with the master and fix the warnings?

@n1ru4l n1ru4l force-pushed the feat-esm branch 3 times, most recently from 8085749 to cb1f404 Compare February 15, 2021 11:32
@n1ru4l
Copy link
Member Author

n1ru4l commented Feb 15, 2021

@morrys done :)

@morrys
Copy link
Member

morrys commented Feb 15, 2021

@n1ru4l I tried the bundle in the example projects cra and pagination-nextjs-ssr and I get errors similar to this: facebook/create-react-app#10356

@n1ru4l
Copy link
Member Author

n1ru4l commented Feb 17, 2021

@morrys Seems like create-react-app is not properly configured to support the .mjs extension.

An alternative approach (that could potentially break the common js module) could be the following: https://unpkg.com/browse/@tinyhttp/app@1.1.12/package.json

"type": "module",

This tells Node.js that any .js file should be treated as if it is was a .mjs file.

@n1ru4l
Copy link
Member Author

n1ru4l commented Feb 18, 2021

I found this and will try it: d3/d3#3469 (comment)

@alex-gopuff
Copy link

When will this be merged?

@morrys
Copy link
Member

morrys commented May 18, 2021

For now it is not merged because there are problems #161 (comment)

@n1ru4l
Copy link
Member Author

n1ru4l commented May 19, 2021

i now have a solution that works consistently. I will try to update the PR this week :)

The logic for this is super simple to inline and does not require an external dependency to achieve.
Comment on lines +86 to +107
format === "cjs" && {
name: "writePkgJSON",
writeBundle: async () => {
await fs.writeFile(
"lib/cjs/package.json",
JSON.stringify({
type: "commonjs",
})
);
}
},
format === "es" && {
name: "writePkgJSON",
writeBundle: async () => {
await fs.writeFile(
"lib/package.json",
JSON.stringify({
type: "module",
})
);
}
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lib/package.json with the contents {"type":"module"} and also a lib/cjs/package.json with the contents {"type":"commonjs"} is generated.

This tells Node.js to treat the contents of that folder/subfolder as the given file type without having to specify the .cjs or .mjs extension. I did not specify it in the root package.json as it caused issues with babel and jest (jest is not es ready yet).

Copy link
Member Author

@n1ru4l n1ru4l May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that the lib folder also includes the "raw" js files besides the bundles. Do we really need those raw files? Why would one want to import those directly instead of using the top-level exports, do you have any use-case in mind? Would it be enough to simply have the typescript definition files and the bundled rollup code?

Comment on lines +13 to +19
"exports": {
".": {
"import": "./lib/es-relay-hooks.mjs",
"require": "./lib/cjs/cjs-relay-hooks.js"
},
"./package.json": "./package.json"
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting with Node.js 14 you cannot simply import anything from a package. You can only use the exports declared in the exports map.

E.g. import { fetchResolver } from "relay-hooks/lib/FetchResolver"; would fail. If that should be possible a entry for each file must be declared in the exports map.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @n1ru4l,
i tried this package in react-relay-offline and i get this error.

react-relay-offline\node_modules\relay-hooks\lib\QueryFetcher.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as areEqual from 'fbjs/lib/areEqual';
    SyntaxError: Cannot use import statement outside a module

Did you mean this?

Copy link
Member Author

@n1ru4l n1ru4l May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are referring to this: https://github.com/morrys/react-relay-offline/blob/c12787d3d0c3325389cd4c8d06ea2f3ffdac800a/src/runtime/loadQuery.ts#L3

Then yes, instead this file should be explicitly exposed via the exports map. See https://nodejs.org/api/packages.html#packages_package_entry_points

Is there any specific reason why you don't re-export those files from the entry point (in relay-hooks) and import it from there (in react-relay-offline)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also related to #161 (comment)

@morrys morrys mentioned this pull request May 19, 2021
@n1ru4l n1ru4l closed this May 26, 2021
@n1ru4l n1ru4l deleted the feat-esm branch May 26, 2021 20:31
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

3 participants