-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
feat: esm support #161
Changes from all commits
3319db8
90d2883
5a9295c
512af97
90e6d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import replace from '@rollup/plugin-replace'; | |
import sourceMaps from 'rollup-plugin-sourcemaps'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import { promises as fs } from "fs"; | ||
import pkg from './package.json'; | ||
|
||
const makeExternalPredicate = (externalArr) => { | ||
|
@@ -24,15 +25,14 @@ function createConfigInternal({ format, production }) { | |
return { | ||
input: 'src/index.ts', | ||
output: { | ||
file: 'lib/' + format + '-relay-hooks' + (production ? '.min' : '') + '.js', | ||
file: 'lib/' + (format === "cjs" ? "cjs/" : "") + format + '-relay-hooks' + (production ? '.min' : '') + '.js', | ||
format, | ||
name: 'relay-hooks', | ||
indent: false, | ||
globals: { | ||
'fbjs/lib/areEqual': 'areEqual', | ||
'fbjs/lib/invariant': 'invariant', | ||
'fbjs/lib/warning': 'warning', | ||
'@restart/hooks/useMounted': 'useMounted', | ||
react: 'React', | ||
'relay-runtime': 'relayRuntime', | ||
}, | ||
|
@@ -83,6 +83,28 @@ function createConfigInternal({ format, production }) { | |
toplevel: format === 'cjs', | ||
warnings: true, | ||
}), | ||
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", | ||
}) | ||
); | ||
} | ||
}, | ||
Comment on lines
+86
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A This tells Node.js to treat the contents of that folder/subfolder as the given file type without having to specify the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
], | ||
}; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "lib", | ||
"rootDir": "src", | ||
"module": "ESNext", | ||
"target": "ES2020", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"declaration": true, | ||
"lib": ["dom", "es6", "esnext.asynciterable", "es2017.object"], | ||
"jsx": "react", | ||
"skipLibCheck": true | ||
}, | ||
"exclude": ["lib", "__tests__", "examples", "__mocks__", "coverage", "scripts"], | ||
"compileOnSave": true | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
Did you mean this?
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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)