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

import ... -> ... is not defined #164

Closed
j2l opened this issue Jul 23, 2021 · 1 comment
Closed

import ... -> ... is not defined #164

j2l opened this issue Jul 23, 2021 · 1 comment

Comments

@j2l
Copy link

j2l commented Jul 23, 2021

Hi Jim,
I successfully run svelte and three (not svelthree here), but for any reason working code in index.svelte like:

import * as THREE from 'three';
const scene = new THREE.Scene();

plenti serve throws:

Can't render htmlComponent: ReferenceError: THREE is not defined /home/runner/work/plenti/plenti/cmd/build/data_source.go on line 307

javascript stack trace: ReferenceError: THREE is not defined
    at create_ssr:36:16
    at Object.$$render (create_ssr:1555:22)
    at create_ssr:35:135
    at $$render (create_ssr:1555:22)
    at Object.render (create_ssr:1563:26)
    at create_ssr:1:58

What does Go need?
Thanks :)

@jimafisk
Copy link
Member

jimafisk commented Dec 1, 2021

Hi @j2l, I'm sorry for letting this issue sit for so long without response. I had actually started to respond but needed to fix some bugs before I could get this working.

Response I started with webpack instructions (don't use)

It looks like three.js doesn't ship with .mjs files by default: mrdoob/three.js#19830. Newer versions of node support this, but our "gopack" (like snowpack, but with less features) tool specifically looks for these file extensions for esm support. Down the road I'd like to move the core files from gopack into our ejectable filesystem (#159) and potentially add esbuild to allow a minimal config setup for pulling in other packages (#28 (comment)).

For now, it probably makes sense to use a bundler like webpack outside of plenti's build process.

  1. Install webpack: npm install webpack webpack-cli --save-dev
  2. Create webpack.config.js in the root of the project with the following contents:
const path = require('path');

module.exports = {
  entry: './node_modules/three/src/Three.js',
  output: {
    path: path.resolve(__dirname, 'assets'),
    filename: 'bundle.js',
  },
};
  1. Run webpack: ./node_modules/webpack/bin/webpack.js
  2. Reference the newly created file inside <head> tags of layouts/global/head.svelte:
<script type="module" src="/assets/bundle.js"></script>

Then in between the <script> tags of your component (for example layouts/content/index.svelte) you could import the module like so:

import * as THREE from '/assets/bundle.js';

However doing this ^ gives me an error along these lines:

errs.go:57: Can't render htmlComponent: ReferenceError: layouts_content__assets_scene_js_createScene is not defined /home/jimafisk/Projects/jantcu/plentico/plenti/cmd/build/data_source.go on line 321


javascript stack trace: ReferenceError: layouts_content__assets_scene_js_createScene is not defined
    at create_ssr:18:20
    at Object.$$render (create_ssr:1555:22)
    at create_ssr:35:135
    at $$render (create_ssr:1555:22)
    at Object.render (create_ssr:1563:26)
    at create_ssr:1:58 (File: /home/jimafisk/Projects/jantcu/plentico/plenti/cmd/build.go, Line: 140)

That's because Plenti is seeing the named import to a locally referenced file and is trying to turn it into a "component signature" that we use internally to identify things in V8. I actually still need to fix this bug, so for now it's probably better to import directly from NPM and with the gopack updates I made in v0.4.28 you should be able to pull threejs in now.

I made some updates to "gopack" which provides esm support for the project, so now you should be able to npm install three then do something like:

import * as THREE from 'three';

Here's a working example site demonstrating this:

Note: Just make sure you use the newest release v0.4.28 to get this working

@jimafisk jimafisk closed this as completed Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants