popeindustries / lit

Fast server-rendering and client-hydration of lit-html templates and web components
MIT License
33 stars 4 forks source link

Question: Bundled Lit HTML #34

Closed cristobal closed 1 year ago

cristobal commented 1 year ago

Question

Should the lit-html and lit-html-server include the batteries in other words bundle lit core themselves.

Background

The problem that we see now is that we get the following message on the server.

Screenshot 2023-06-20 at 14 45 13

With some debug we saw that it's the same version that we get the warning on:

Screenshot 2023-06-20 at 14 46 13

Problem

It adds a bit of complexity in the code with ESM to handle server vs. client since it would require to have a custom file that handles dynamic import of the lit implementations depending whether we are on the client vs. server.

Solution

Some solution to this could be e.g.:

  1. Remove lit core as part of the client and server bundle, and rather have it as an explicit import.
  2. Remove lit core from the server bundle only.
  3. ???
popeindustries commented 1 year ago

This looks to me like a dvlp issue. The first message is coming from lit and is standard when NODE_ENV=development. It's a warning, but flagged here as an error by dvlp. There is only one version of lit-html.js, and it works on both server/client.

The real problem here is that the lit core packages need to be vendored due to Google's aggressive minification and missing API for modifying internal properties during hydration. I will continue to look for a better solution here.

cristobal commented 1 year ago

From what i can see is that we require the following files:

  1. @popeindustries+lit-html-server@6.0.1_@types+trusted-types@2.0.3/node_modules/@popeindustries/lit-html-server/lit-html-server.js to make the server happy
  2. @popeindustries+lit-html@5.2.1_@types+trusted-types@2.0.3/node_modules/@popeindustries/lit-html/src/vendor/lit-html.js the client code when run on the server too

Both of them inline a bit of the same code it seems, however the server code is minified on the left, as depicted in the picture on the left below. Screenshot 2023-06-21 at 08 56 59

It may be that they don't inline the all the same code, however something is overlapping at least it seems.

cristobal commented 1 year ago

I think i found out why this happens now, it seems that the bundled lit-html-server, includes the same lit-html since it's imports and exports from @popeindustries/lit-html package from the lit-html-server.js file. https://github.com/popeindustries/lit/blob/8b26dbce848997642c1d3ca45665e4c0eb43d630/packages/lit-html-server/src/lit-html-server.js#L3-L9

However the built file includes all the code that it imports and exports from @popeindustries/lit-html, instead of using import and exports.

This is what is imported 👇🏽 Screenshot 2023-06-21 at 10 00 29

So the package @popeindustries/lit-html is not imported at all here ☝🏽, but rather bundled as part of the build instead. I guess we need to fix the build file here.

Will create an PR 👋🏽

popeindustries commented 1 year ago

Ok, I guess you just need to update L8 in packages/lit-html-server/scripts/build.js to externalize @popeindustries/lit-html instead of lit-html