preactjs / preset-vite

Preset for using Preact with the vite bundler
https://npm.im/@preact/preset-vite
MIT License
262 stars 26 forks source link

Freshly generated project doesn't build #117

Closed stonetwig closed 7 months ago

stonetwig commented 7 months ago

image

nnpm init preact

┌  Preact - Fast 3kB alternative to React with the same modern API
│
◇  Project directory:
│  pefe
│
◇  Project language:
│  TypeScript
│
◇  Use router?
│  Yes
│
◇  Prerender app (SSG)?
│  No
│
◇  Use ESLint?
│  Yes
│
◇  Set up project directory
│
◇  Installed project dependencies
│
◇  Getting Started ─╮
│                   │
│  $ cd pefe        │
│  $ npm run dev    │
│                   │
├───────────────────╯
│
└  You're all set!

Then I just run build and get a failure:


npm run build

> build
> vite build

vite v4.5.3 building for production...
✓ 19 modules transformed.
✓ built in 119ms
"renderToStringAsync" is not exported by "__vite-optional-peer-dep:preact-render-to-string:preact-iso", imported by "node_modules/preact-iso/src/prerender.js".
file: /Users/myuser/code/pefe/node_modules/preact-iso/src/prerender.js:2:9
1: import { h, options, cloneElement } from 'preact';
2: import { renderToStringAsync } from 'preact-render-to-string';
            ^
3:
4: let vnodeHook;
error during build:
RollupError: "renderToStringAsync" is not exported by "__vite-optional-peer-dep:preact-render-to-string:preact-iso", imported by "node_modules/preact-iso/src/prerender.js".
    at error (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
    at Module.error (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:13745:16)
    at Module.traceVariable (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:14175:29)
    at ModuleScope.findVariable (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:12615:39)
    at FunctionScope.findVariable (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at ChildScope.findVariable (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at BlockScope.findVariable (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:7124:38)
    at Identifier.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:8319:40)
    at CallExpression.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at CallExpression.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:9890:15)
    at AwaitExpression.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at VariableDeclarator.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at VariableDeclaration.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5888:28)
    at BlockStatement.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5888:28)
    at TryStatement.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at BlockStatement.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5888:28)
    at FunctionDeclaration.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at ExportDefaultDeclaration.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5892:23)
    at Program.bind (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:5888:28)
    at Module.bindReferences (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:13741:18)
    at Graph.sortModules (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:25853:20)
    at Graph.build (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:25737:14)
    at async file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:26706:13
    at async catchUnfinishedHookActions (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:25910:16)
    at async rollupInternal (file:///Users/myuser/code/pefe/node_modules/rollup/dist/es/shared/node-entry.js:26701:5)
    at async build (file:///Users/myuser/code/pefe/node_modules/vite/dist/node/chunks/dep-41cf5ffd.js:48079:18)
    at async CAC.<anonymous> (file:///Users/myuser/code/pefe/node_modules/vite/dist/node/cli.js:842:9)

Not sure on how to fix this since it seems like there is a method that is not being exported properly?

Node version: v18.17.1

Tried to updated to v20.12.1 but with the same results.

Thanks

rschristian commented 7 months ago

Someone mentioned this on our Slack, looks like it's a bug in Vite.

Can you add preact-render-to-string to your project and test that? It's an optional peer dep of preact-iso, but it's only used if you've set up prerendering. It seems Vite is incorrectly trying to access it.

stonetwig commented 7 months ago

The problem seems to occur when I generate a project with router configuration files. Sure I can test that!

stonetwig commented 7 months ago

Can you add preact-render-to-string to your project and test that?

This solved the issue, now I can build just fine. Thank you for the insanely quick answer.

rschristian commented 7 months ago

Glad it helped! Sorry for the inconvenience.

Might need to make an issue upstream in Vite, this seems to be a regression in the latest version.