preact-cli icon indicating copy to clipboard operation
preact-cli copied to clipboard

--dest doesn't work properly

Open Debdut opened this issue 3 years ago • 5 comments

Do you want to request a feature or report a bug? Bug

What is the current behaviour? When I try to build for other destination using --dest, it throws an unrelated error which is only resolved I use --no-prerender along with it.

If the current behaviour is a bug, please provide the steps to reproduce.

preact build --dest ../public/build

What is the expected behaviour? The expected behaviour is that it should build, as it build with just preact build

If this is a feature request, what is motivation or use case for changing the behaviour?

Please mention other relevant information.

A sneakpeak of the Error

Error: Cannot find module 'preact'
Require stack:
- /Users/debdut/Code/Projects/blogletter/public/build/ssr-build/ssr-bundle.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/lib/webpack/prerender.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/lib/webpack/render-html-plugin.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/lib/webpack/webpack-client-config.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/lib/webpack/run-webpack.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/commands/build.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/commands/index.js
- /Users/debdut/Code/Projects/blogletter/blogletter.news/node_modules/preact-cli/lib/index.js
method: HteQ
at: /Users/debdut/Code/Projects/blogletter/blogletter.news/src/assets/images/browser-dark.png:1:41

This could be caused by using DOM or Web APIs.
Pre-render runs in node and has no access to globals available in browsers.

Consider wrapping code producing error in: 'if (typeof window !== "undefined") { ... }'

Alternatively use 'preact build --no-prerender' to disable prerendering.

This error seems irrelevant since I'm not using any Browser API such as window or document that will come in the way of prerender. Plus it builds perfectly if I don't set the --dest

Please paste the results of preact info here.

Environment Info:
  System:
    OS: macOS 11.0
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 12.14.1 - ~/.nvm/versions/node/v12.14.1/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.14.1/bin/npm
  Browsers:
    Chrome: 83.0.4103.97
    Safari: 14.0.1
  npmPackages:
    preact: ^10.3.2 => 10.5.5 
    preact-cli: ^3.0.0 => 3.0.3 
    preact-render-to-string: ^5.1.4 => 5.1.10 
    preact-router: ^3.2.1 => 3.2.1 
  npmGlobalPackages:
    preact-cli: 3.0.3

Debdut avatar Oct 21 '20 02:10 Debdut

This looks to be an issue with module resolution rather than anything to do with the dest flag. Prerending is done in Node so when you set the target to be outside of the root directory of the application, that dependency doesn't exist in that context. It's like running a command without installing the dependency.

Do you mean to do this, so that the directory structure looks like the following:

some_folder/
--preact_app/
--public/
----build/
------<output>

or is this not intended? I can look into it either way to see if it can be improved, but the simple solution would be to not choose a directory out of the root application directory as of right now.

rschristian avatar Oct 21 '20 05:10 rschristian

Thanks, ya that's what I meant. Currently doing something like this

preact build && rm -rf ../public && mkdir ../public && mv build ../public

Would love a direct solution!

Debdut avatar Oct 21 '20 05:10 Debdut

Alright, I'll see if I can get a quick PR written up here. Should be possible.

Might be some time before you see such a thing merged in though (if it does fit the bill).

rschristian avatar Oct 21 '20 05:10 rschristian

Ya sure! Thank you very much 🤗

Debdut avatar Oct 21 '20 05:10 Debdut

Hm, might not be as simple as I assumed.

Seems to be that the issue is here, we ultimately can not require the ssr-bundle.js when that module's own require statements cannot be resolved (as there is no preact package that can be resolved from ../public/build). I'll play with this some more, but leaving a comment in case I need to leave this for a while.

rschristian avatar Oct 21 '20 06:10 rschristian

Going to close this out as a "won't fix" as this is standard behavior in Node.

rschristian avatar Dec 23 '22 03:12 rschristian