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

build error in SWR 2.2.0 #96

Closed
himself65 opened this issue Jul 1, 2023 · 9 comments · Fixed by #136
Closed

build error in SWR 2.2.0 #96

himself65 opened this issue Jul 1, 2023 · 9 comments · Fixed by #136
Labels
help wanted Extra attention is needed

Comments

@himself65
Copy link
Sponsor Contributor

"use client";

import { useState } from "react";
import useSWR from 'swr'

export const Counter = () => {
  // @ts-ignore
  const { data } = useSWR('hello', () => 'world', {
    suspense: true
  })
  const [count, setCount] = useState(0);
  return (
    <div style={{ border: "3px blue dashed", margin: "1em", padding: "1em" }}>
      <p>Count: {count}</p>
      <button onClick={() => setCount((c) => c + 1)}>Increment</button>
      <h3>This is a client component.</h3>
      <div>
        {data}
      </div>
    </div>
  );
};
/Users/himself65/.nvm/versions/node/v18.16.1/bin/npm run build

> affine-waku@0.1.0 build
> waku build

vite v4.3.9 building SSR bundle for production...
✓ 6 modules transformed.
✓ built in 42ms
"default" is not exported by "node_modules/swr/core/dist/react-server.mjs", imported by "src/Counter.tsx".
file: /Users/himself65/Code/affine-waku/src/Counter.tsx:4:7
2: 
3: import { useState } from "react";
4: import useSWR from 'swr'
          ^
5: 
6: export const Counter = () => {
file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:2124
        base = Object.assign(new Error(base.message), base);
                             ^

Error [RollupError]: "default" is not exported by "node_modules/swr/core/dist/react-server.mjs", imported by "src/Counter.tsx".
    at error (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:2124:30)
    at Module.error (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:13463:16)
    at Module.traceVariable (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:13884:29)
    at ModuleScope.findVariable (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:12429:39)
    at ReturnValueScope.findVariable (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:6965:38)
    at ChildScope.findVariable (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:6965:38)
    at Identifier.bind (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:8127:40)
    at CallExpression.bind (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:5738:23)
    at CallExpression.bind (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:9680:15)
    at VariableDeclarator.bind (file:///Users/himself65/Code/affine-waku/node_modules/rollup/dist/es/shared/node-entry.js:5738:23) {
  binding: 'default',
  code: 'MISSING_EXPORT',
  exporter: '/Users/himself65/Code/affine-waku/node_modules/swr/core/dist/react-server.mjs',
  id: '/Users/himself65/Code/affine-waku/src/Counter.tsx',
  url: 'https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module',
  pos: 103,
  loc: {
    column: 7,
    file: '/Users/himself65/Code/affine-waku/src/Counter.tsx',
    line: 4
  },
  frame: '2: \n' +
    '3: import { useState } from "react";\n' +
    "4: import useSWR from 'swr'\n" +
    '          ^\n' +
    '5: \n' +
    '6: export const Counter = () => {',
  watchFiles: [
    '/Users/himself65/Code/affine-waku/entries.ts',
    '/Users/himself65/Code/affine-waku/package.json',
    '/Users/himself65/Code/affine-waku/node_modules/waku/dist/server.js',
    '/Users/himself65/Code/affine-waku/src/App.tsx',
    '/Users/himself65/Code/affine-waku/src/Counter.tsx',
    '/Users/himself65/Code/affine-waku/node_modules/swr/core/dist/react-server.mjs',
    '/Users/himself65/Code/affine-waku/node_modules/swr/_internal/dist/react-server.mjs'
  ]
}

Node.js v18.16.1

Process finished with exit code 1

Upstream: vercel/swr#2700


repo: https://github.com/himself65/affine-waku/blob/issue-swr/src/Counter.tsx

yarn install
yarn build
@himself65
Copy link
Sponsor Contributor Author

same kind of code run success in storybook and next.js

@himself65
Copy link
Sponsor Contributor Author

BTW, SWR 2.1.5 works

@himself65
Copy link
Sponsor Contributor Author

@dai-shi
Copy link
Owner

dai-shi commented Jul 3, 2023

https://unpkg.com/browse/swr@2.2.0/package.json

    "react-server": "./core/dist/react-server.mjs",

Yeah, it's because this new condition is added in exports.

Hmm, wait, your file is under "use client". So, we should simply ignore it on server bundling?
Does it work with waku dev?

@himself65
Copy link
Sponsor Contributor Author

waku dev works, but not for build

@dai-shi
Copy link
Owner

dai-shi commented Jul 4, 2023

diff --git a/src/lib/builder.ts b/src/lib/builder.ts
index 4c87114..a9cfe11 100644
--- a/src/lib/builder.ts
+++ b/src/lib/builder.ts
@@ -62,7 +62,7 @@ const analyzeEntries = async (entriesFile: string) => {
     ssr: {
       noExternal: /^(?!node:)/,
       // FIXME this is very adhoc.
-      external: ["react", "minimatch"],
+      external: ["react", "minimatch", "swr"],
     },
     resolve: {
       conditions: ["react-server"],

That ☝️ seems to work around the build error.

This is slightly related with #79, but we don't have a good workaround for this.
I think we might need to revisit how to use Vite for server bundling.

This was referenced Jul 4, 2023
@EasonYou
Copy link
Contributor

It seems can't use in server components.
image

@dai-shi
Copy link
Owner

dai-shi commented Jul 12, 2023

Hi, thanks for jumping in.
Can anyone try #103 to see how it change the error?

@dai-shi
Copy link
Owner

dai-shi commented Jul 31, 2023

The latest version v0.14.0 causes another error:

import { serialize, OBJECT, SWRConfig as SWRConfig$1, defaultConfig, withArgs, SWRGlobalState, createCacheHelper, isUndefined, getTimestamp, UNDEFINED, isFunction, revalidateEvents, internalMutate, useIsomorphicLayoutEffect, subscribeCallback, IS_SERVER, rAF, IS_REACT_LEGACY, mergeObjects } from 'swr/_internal';
                                                                                                                                                                                                                                                                    ^^^^^^^^^^^^^^^
SyntaxError: The requested module 'swr/_internal' does not provide an export named 'IS_REACT_LEGACY'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Can anyone investigate it??

Not sure if it is somehow related with #111.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants