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

@astrojs/lit breaks @astrojs/cloudflare builds #42

Closed
1 task done
chaffinated opened this issue Apr 21, 2023 · 4 comments
Closed
1 task done

@astrojs/lit breaks @astrojs/cloudflare builds #42

chaffinated opened this issue Apr 21, 2023 · 4 comments

Comments

@chaffinated
Copy link

chaffinated commented Apr 21, 2023

What version of astro are you using?

2.3.0

Are you using an SSR adapter? If so, which one?

Cloudflare

What package manager are you using?

yarn

What operating system are you using?

Mac

What browser are you using?

Brave

Describe the Bug

When deploying via GitHub, the astro project builds successfully, but fails at deploying the CF function.

No lit components are used in the project; simply adding lit to the integrations field in the config seems to break deployments.

The error from Cloudflare:
Error: Failed to publish your Function. Got error: Uncaught ReferenceError: window is not defined at functionsWorker-xxxx.js:2140:10

A snippet of what could be the offending code in the build:

var ea = window,
  aa =
    ea.ShadowRoot &&
    (ea.ShadyCSS === void 0 || ea.ShadyCSS.nativeShadow) &&
    "adoptedStyleSheets" in Document.prototype &&
    "replace" in CSSStyleSheet.prototype,
  Fn = Symbol(),
  Sn = new WeakMap(),
  ta = class {
    constructor(t, a, u) {
      if (((this._$cssResult$ = !0), u !== Fn))
        throw Error(
          "CSSResult is not constructable. Use `unsafeCSS` or `css` instead.",
        );
      (this.cssText = t), (this.t = a);
    }
    get styleSheet() {
...

This is apparently from the Lit library itself.

I'll keep digging in to see what I can find out, but I'd greatly appreciated any help!

Link to Minimal Reproducible Example

https://github.com/chaffinated/astro-lit-cloudflare-repro

Participation

  • I am willing to submit a pull request for this issue.
@chaffinated
Copy link
Author

I believe I've narrowed this down to the package.json exports field in the lit package and the way the bundler resolves the package. The server-friendly exports of lit-html and @lit/reactive-element are kept under the conditional node in the exports field:

{
  "name": "lit-html",
  "version": "2.7.2",
  "description": "HTML templates literals in JavaScript",
  "license": "BSD-3-Clause",
  "repository": {
    "type": "git",
    "url": "https://github.com/lit/lit.git",
    "directory": "packages/lit-html"
  },
  "author": "Google LLC",
  "homepage": "https://lit.dev/",
  "main": "lit-html.js",
  "type": "module",
  "exports": {
    ".": {
      "types": "./development/lit-html.d.ts",
      "node": {
        "development": "./node/development/lit-html.js",
        "default": "./node/lit-html.js"
      },
      "development": "./development/lit-html.js",
      "default": "./lit-html.js"
    },
    "./async-directive.js": {
      "types": "./development/async-directive.d.ts",
      "node": {
        "development": "./node/development/async-directive.js",
        "default": "./node/async-directive.js"
      },
      "development": "./development/async-directive.js",
      "default": "./async-directive.js"
    },
...

It appears that resolve.conditions in the Astro config file is not respected when building with config file:

import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';
import tailwind from "@astrojs/tailwind";
import lit from "@astrojs/lit";

// https://astro.build/config
export default defineConfig({
  output: 'server',
  integrations: [
    tailwind(),
    lit(),
  ],
  adapter: cloudflare({ mode: 'directory' }),
  vite: {
    ssr: {
      noExternal: [
        '@lit/reactive-element',
        'lit-html',
        'lit',
      ],
    },
    resolve: {
      conditions: ['node'],
    },
  },
});

@chaffinated
Copy link
Author

This should be resolved with this PR:
withastro/astro#6915

@alexanderniebuhr
Copy link
Member

Can we confirm that this is still an issue.

@alexanderniebuhr
Copy link
Member

Closing because it is stale & superseded by #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants