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

[Bug]: Not working in Astro #1817

Closed
1 task done
PuruVJ opened this issue Aug 23, 2023 · 5 comments · Fixed by #1818
Closed
1 task done

[Bug]: Not working in Astro #1817

PuruVJ opened this issue Aug 23, 2023 · 5 comments · Fixed by #1818
Labels
bug Something isn't working

Comments

@PuruVJ
Copy link

PuruVJ commented Aug 23, 2023

What happened?

First of all, freaking AWESOME library man!!! 🔥🔥🔥

I tried to use @tldraw/editor in Astro based environment, but I keep getting bundler errors. Now, I think it's not Astro itself, but some vite config at some level.

I keep getting the error:

[vite] Error when evaluating SSR module /src/Editor.jsx: failed to import "@tldraw/editor"|- file:///home/projects/github-6vmn42/node_modules/@tldraw/editor/dist/esm/lib/app/App.mjs:35import { EventEmitter } from "eventemitter3";         ^^^^^^^^^^^^SyntaxError: The requested module 'eventemitter3' does not provide an export named 'EventEmitter'

How can we reproduce the bug?

https://stackblitz.com/edit/github-6vmn42?file=src/Editor.jsx,src/pages/index.astro,package.json&on=stackblitz

What browsers are you seeing the problem on?

Chrome

Contact Details

twitter @puruvjdev

Code of Conduct

  • I agree to follow this project's Code of Conduct
@PuruVJ PuruVJ added the bug Something isn't working label Aug 23, 2023
@steveruizok
Copy link
Collaborator

Thanks so much for the reproduction! I think this is something to do with the default export in the eventemitter3 library, see bug reported here. There doesn't seem to be a clear resolution for that, so I'll just switch to a named export and that should solve the issue.

github-merge-queue bot pushed a commit that referenced this issue Aug 24, 2023
This PR switches from the default export to a named export in event
emitter 3. Should close #1817.

### Change Type

- [x] `patch` — Bug fix

### Test Plan

1. Hard to test ahead of time, but try [this
reproduction](https://stackblitz.com/edit/github-6vmn42?file=src%2FEditor.jsx,src%2Fpages%2Findex.astro,package.json&on=stackblitz)
with the new version.

### Release Notes

- [@tldraw/editor] updates eventemitter3 import to fix issue with Astro
builds.
@PuruVJ
Copy link
Author

PuruVJ commented Aug 24, 2023

Thanks a lot!!! Any ETA on release? Wanna test out tldraw as soon as possible 😁

@Mjtlittle
Copy link

I dont think this resolved the issue; the error message im getting now is...

21:49:21 [ERROR] Named export 'EventEmitter' not found. The requested module 'eventemitter3' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'eventemitter3';
const { EventEmitter } = pkg;

It looks like you have to consume the package this way, or you can bump the version of the eventemitter3 package to the latest version. We are on v4, and v5 adds support for esm (https://github.com/primus/eventemitter3/releases/tag/5.0.0).

After fixing this inplace within node_modules, I run into the same issue with the package "lz-string". Upon fixing this packages usage also, I finally started loading assets for the editor, but now its stuck "Loading assets...".

I may be able to take a closer look later and maybe open a pr

@jezzzm
Copy link

jezzzm commented Feb 29, 2024

Also seeing this error in context of Remix as a Vite plugin. Can't circumvent (e.g. noExternal config or cjsInterop plugin) without something like patch-package at the moment.

@phgn0
Copy link

phgn0 commented Apr 15, 2024

So the following patches fix the tldraw build in a Remix app for me:

@tldraw+editor+2.0.2.patch

diff --git a/node_modules/@tldraw/editor/dist-esm/lib/editor/Editor.mjs b/node_modules/@tldraw/editor/dist-esm/lib/editor/Editor.mjs
index 0016452..9c010f3 100644
--- a/node_modules/@tldraw/editor/dist-esm/lib/editor/Editor.mjs
+++ b/node_modules/@tldraw/editor/dist-esm/lib/editor/Editor.mjs
@@ -42,7 +42,7 @@ import {
   sortByIndex,
   structuredClone
 } from "@tldraw/utils";
-import { EventEmitter } from "eventemitter3";
+import EventEmitter from "eventemitter3";
 import { createTLUser } from "../config/createTLUser.mjs";
 import { checkShapesAndAddCore } from "../config/defaultShapes.mjs";
 import {

tldraw+2.0.2.patch

diff --git a/node_modules/tldraw/dist-esm/lib/ui/hooks/useClipboardEvents.mjs b/node_modules/tldraw/dist-esm/lib/ui/hooks/useClipboardEvents.mjs
index cc58edc..2ec1a51 100644
--- a/node_modules/tldraw/dist-esm/lib/ui/hooks/useClipboardEvents.mjs
+++ b/node_modules/tldraw/dist-esm/lib/ui/hooks/useClipboardEvents.mjs
@@ -4,7 +4,7 @@ import {
   useEditor,
   useValue
 } from "@tldraw/editor";
-import { compressToBase64, decompressFromBase64 } from "lz-string";
+import lz from "lz-string";
 import { useCallback, useEffect } from "react";
 import { useUiEvents } from "../context/events.mjs";
 import { pasteExcalidrawContent } from "./clipboard/pasteExcalidrawContent.mjs";
@@ -184,7 +184,7 @@ async function handleClipboardThings(editor, things, point) {
           const tldrawHtmlComment = text.match(/<tldraw[^>]*>(.*)<\/tldraw>/)?.[1];
           if (tldrawHtmlComment) {
             try {
-              const jsonComment = decompressFromBase64(tldrawHtmlComment);
+              const jsonComment = lz.decompressFromBase64(tldrawHtmlComment);
               if (jsonComment === null) {
                 r({
                   type: "error",
@@ -297,7 +297,7 @@ const handleNativeOrMenuCopy = (editor) => {
     }
     return;
   }
-  const stringifiedClipboard = compressToBase64(
+  const stringifiedClipboard = lz.compressToBase64(
     JSON.stringify({
       type: "application/tldraw",
       kind: "content",

@steveruizok steveruizok mentioned this issue May 12, 2024
2 tasks
github-merge-queue bot pushed a commit that referenced this issue May 12, 2024
This PR changes our imports so that they work in a few rare cases.
#1817

### Change Type
- [x] `sdk` — Changes the tldraw SDK
- [x] `bugfix` — Bug fix

### Release Notes

- Fix bug effecting imports in Astro.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants