-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Create a minified browser build compatible with next.js #1108
Conversation
buffer-layout has a couple of browser-specific improvements in pabigot/buffer-layout#29 and pabigot/buffer-layout#27 that this update pulls in
* Separates NodeWallet into its own file * Ensures that NodeWallet and workspace are only exported on Node
Circular dependencies can make TypeScript compiling slower. In this commit we fix imports so things can be a bit faster.
A base config pattern will allow us to keep module emitting from typescript compiler options separate
This commit uses rollup, same as solana's web3 project uses to bundle a browser-specific build
Updated to create ESM as per conversation on Discord. I also created a gist with the unminified build artifact in case y'all want to check it out. The error making tests error out should be fixed now as well. |
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.
Thank you for the thorough explanation! LGTM. Just a couple of nits.
Hi guys, when we can expect updated npm package that includes this fix? |
This change seems to have tripped up |
I see there is a new version of npm package 0.19.0. Unfortunately looks like fix above produced a new bug. NextJS project compiles without any errors but on page load i'm getting this error: SyntaxError: Cannot use import statement outside a module |
@igdeman can you share your next js version and wether your project is using webpack 4 or 5? (Here’s how you’d know: https://nextjs.org/docs/messages/webpack5) |
Temporary workaround for this that worked for me was to create a import * as anchor from "@project-serum/anchor";
declare module "@project-serum/anchor" {
export const workspace: any;
export const Wallet: import("@project-serum/anchor/dist/cjs/nodewallet").default;
} |
Hi, The I also have a couple of questions @jeduan:
If I understand the bundler helps with removing annoying imports of Node.js APIs that the consumer trivially nullify themselves (or that anchor can deal with differently, ie, by exposing a Node.js & a browser entrypoint) but I believe it's worth considering the benefits of not bundling a library, intended for bundling-capable consumers. A bundler is totally fine for a CDN export like in #3 though it couldn't be the same build output configuration. |
We need a browser bundle because
I'm curious on the downsides of minimizing library side.
the
This project still publishes an ESM transpiled library in Having said that, I'm just a random contributor. You should feel empowered to send PRs 😄 |
Sounds like more than a bundler, we just need to exclude that module from the browser build output one way or another, or remove the side-effect, or tell the bundlers to treat the package as side-effects free.
from the top of my head:
I see no upsides..
yes and it was used in web projects nevertheless, now it's gone without a warning or a mention in the changelog :(
Yes, this is true, but bundlers such as Webpack will pick the
No problem, I'm more interested in finding a better solution to the initial problem. |
Hi there, project is using nextjs version: 12.0.7. According to the link you provide, project should use webpack 5 as there is no webpack5: false flag in the config. |
@dominictwlee nice, could this be upstreamed? Or is it just a workaround? |
Possibly, maybe not via a .d.ts file but adding those declarations directly in the index.ts file. I'll give it a try, will put up a PR shortly |
Hi, I have viewed all the comments here and I am still confused on how can I use unpkg to export anchor to be used in browser. In @solana/web3.js we can choose to use the script below to be used in browser. Anyone can enlighten me which file should I locate so I can use Anchor as browser bundle?
|
Browser Build
This PR creates a browser-specific build that includes none of the server-specific code.
It uses Rollup to create a single output that's tree-shakeable and that will not include node-specific APIs like
NodeWallet
andProvider.local()
. The build is declared on package.json'sbrowser
field.The code is split by individually reviewable commits
In addition a couple of goodies:
How was this tested
I created a minimal next.js project which complained about
fs
andpath
being undefined on the client. After doingyarn link '@project-serum/anchor'
and reloading, the project ran.Fixes #244
See also #728 and #983
Related to #1073
Resulting bundle: