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

Using same version of preact for pre-rendering #803

Merged
merged 5 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/cli/lib/lib/webpack/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ module.exports = function(env, params) {
);
return '';
}
const { cwd } = env;

let preact = require('preact'),
renderToString = require('preact-render-to-string');
const preact = require(require.resolve(`${cwd}/node_modules/preact`));
Copy link

@tomsiwik tomsiwik Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, using a monorepo manager like lerna this workaround breaks the build process and returns:

✖ ERROR Template execution failed: Error: Cannot find module './node_modules/preact'

The cwd is located inside root/packages/mypack and is looking for a root/packages/mypack/node_modules, which however is located in root/node_modules. Node won't resolve correctly due to lernas or yarns module mgmt and thus this will break.

I reverted the changes and logged destination. A revert worked but I think changing the destination to: ${__dirname}/../../../../preact and ${__dirname}/../../../../preact-render-to-string would work too.

Not sure what's more elegant...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any workaround for this? I just moved a preact app into my monorepo to not have to maintain it in a separate repo, and I'm running into this exact error.

Copy link

@tomsiwik tomsiwik Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexjsdev With Lerna and Yarn workspaces no. If you disable yarn workspaces this should work with Lerna though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1418

const renderToString = require(require.resolve(
`${cwd}/node_modules/preact-render-to-string`
));

return renderToString(preact.h(app, { ...params, url }));
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/lib/lib/webpack/webpack-server-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function serverConfig(env) {
chunkFilename: '[name].chunk.[chunkhash:5].js',
libraryTarget: 'commonjs2',
},
externals: {
preact: 'preact',
},
target: 'node',
resolveLoader: {
alias: {
Expand Down