Closed Squareys closed 1 year ago
I believe the problem might be that mindar-image.prod.js
is only for nodejs environments now, and can no longer be used in the browser. That's fairly unfortunate for the wonderland-mind-ar-tracking project, though :/
I don't think mindar-image.prod.js
is for nodejs environments. I have no problem importing it from a create-react-app
. I'm not sure about using esbuild
though. Haven't tried that.
Probably unrelated to the issue, but
import {Controller} from 'mind-ar/dist/mindar-image.prod.js';
instead of
import {MindAR} from 'mind-ar/dist/mindar-image.prod.js';
import {Controller} from 'mind-ar/dist/mindar-image.prod.js';
That's great, though does not change much. It looks like the mindar-image.prod.js
has various flags like "IS_BROWSER"
and "IS_NODE"
, but I'm not sure how to pass them via esbuild. You can pass defines via --define:IS_BROWSER=true
, but the way controller-939e6d85.js
seems to be using them makes them incompatible with defines 🤔
I think esbuild resolves require() calls at bundle time, which means that conditional requires() on flags that in the browser would always be true will actually get resolved eagerly :(
I'm not sure where does the require
calls come from. All the code in mindar repo is using import
. Maybe it has something to do with the vite building process, which I'm not familiar with. Maybe @jmschrack has some ideas?
@Squareys MindAR 1.2.0 uses Vite to create the bundles. Vite uses Esbuild for transformation and minification, but bundling is done via Rollup.js
I think "tensorflow" may be the one causing the problem, although I suspect the actual bug may lie in either Esbuild or Vite (which passes commands to Esbuild).
Have you tried import {Controller} from 'mind-ar/src/image-target/controller.js'
instead? This would have your instance of Esbuild handle the bundling and import instead of relying on a pre-bundled version.
@jmschrack Thank you for helping out here! Highly appreciated 🙏
With your suggestion
import {Controller} from 'mind-ar/src/image-target/controller.js'
I'm running into trouble with the worker that is imported transitively:
[2023-03-07 20:10:34.395][error] X [ERROR] No matching export in "../../node_modules/mind-ar/src/image-target/compiler.worker.js?worker&inline" for import "default"
[2023-03-07 20:10:34.395][error]
[2023-03-07 20:10:34.395][error] ../../node_modules/mind-ar/src/image-target/compiler.js:2:7:
[2023-03-07 20:10:34.395][error] 2 │ import CompilerWorker from "./compiler.worker.js?worker&inline";
[2023-03-07 20:10:34.396][error] ╵ ~~~~~~~~~~~~~~
[2023-03-07 20:10:34.396][error]
[2023-03-07 20:10:34.396][error] X [ERROR] No matching export in "../../node_modules/mind-ar/src/image-target/controller.worker.js?worker&inline" for import "default"
[2023-03-07 20:10:34.396][error]
[2023-03-07 20:10:34.396][error] ../../node_modules/mind-ar/src/image-target/controller.js:3:7:
[2023-03-07 20:10:34.396][error] 3 │ import ControllerWorker from "./controller.worker.js?worker&inline";
[2023-03-07 20:10:34.396][error] ╵ ~~~~~~~~~~~~~~~~
[2023-03-07 20:10:34.396][error]
[2023-03-07 20:10:34.396][error] 2 errors
[2023-03-07 20:10:34.396][error] node:child_process:910
[2023-03-07 20:10:34.396][error] throw err;
[2023-03-07 20:10:34.396][error] ^
[2023-03-07 20:10:34.396][error]
[2023-03-07 20:10:34.397][error] Error: Command failed: C:\Program Files\Wonderland\WonderlandEngine\templates\package\node_modules\esbuild-windows-64\esbuild.exe C:/Repos/wonderland-mind-ar-tracking/examples/image-tracking/cache/js/_editor_index.js --bundle --sourcemap --define:WL_EDITOR=true
[2023-03-07 20:10:34.397][error] at checkExecSyncError (node:child_process:871:11)
[2023-03-07 20:10:34.397][error] at Object.execFileSync (node:child_process:907:15)
[2023-03-07 20:10:34.397][error] at Object.<anonymous> (C:\Program Files\Wonderland\WonderlandEngine\templates\package\node_modules\esbuild\bin\esbuild:211:28)
[2023-03-07 20:10:34.397][error] at Module._compile (node:internal/modules/cjs/loader:1159:14)
[2023-03-07 20:10:34.397][error] at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
[2023-03-07 20:10:34.397][error] at Module.load (node:internal/modules/cjs/loader:1037:32)
[2023-03-07 20:10:34.397][error] at Module._load (node:internal/modules/cjs/loader:878:12)
[2023-03-07 20:10:34.397][error] at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
[2023-03-07 20:10:34.397][error] at node:internal/main/run_main_module:23:47 {
[2023-03-07 20:10:34.397][error] status: 1,
[2023-03-07 20:10:34.397][error] signal: null,
[2023-03-07 20:10:34.397][error] output: [ null, null, null ],
[2023-03-07 20:10:34.397][error] pid: 23948,
[2023-03-07 20:10:34.397][error] stdout: null,
[2023-03-07 20:10:34.397][error] stderr: null
[2023-03-07 20:10:34.398][error] }
[2023-03-07 20:10:34.398][error]
[2023-03-07 20:10:34.398][error] Node.js v18.12.1
Is that possibly a special rollup.js feature to be able to import workers like that?
* Edit: So yes, indeed, I'd need the esbuild-plugin-inline-worker, but we're currently running esbuild
through the executable, which doesn't support plugins. Guess that's on my side now.
Thank you very much!
I figured it out with the suggestion of @jmschrack, here's our solution:
The dist folder will contain a final bundle ES module that can be used from any Wonderland Engine project by simply installing the npm package.
Closing--highly grateful for your help!
Glad I could help!
@hiukim I think it might be useful to note in the documentation about this setup with esbuild?
If you are using esbuild and running into require errors, you can try importing the classes directly Example:
import {MindARThree} from 'mind-ar/src/image-target/three.js'
You'll need to use the esbuild-plugin-inline-worker
Yes, totally! I'll put that in. thx
Hi @hiukim !
Trying again to import 1.2.0 with the following code:
This is the error I get:
I'm using
esbuild
to bundle for the browser. Any help is appreciated! 🙏Best, Jonathan