I have in my project folder:
index.html
src
resources
node_modules
index.html:
<!DOCTYPE html>
<html>
<head>
<title>WebGPU App</title>
<meta charset="UTF-8" />
<link rel="shortcut icon" href="#">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" typ="text/css" href="resources/css/style.css">
<script type="importmap"> {
"imports": {
"three": "/node_modules/three/build/three.module.js",
"three/addons/": "/node_modules/three/examples/jsm/",
"three/nodes": "/node_modules/three/examples/jsm/nodes/Nodes.js"
}
}
</script>
</head>
<body>
<div id="container"></div>
<script type="module" src="./src/index.js"></script>
</body>
</html>
I have all my scripts in the src folder. I use one of the scripts as an import library
src/three-defs.js
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { CopyShader } from 'three/addons/shaders/CopyShader.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
import { ShaderNode, MeshBasicNodeMaterial, texture, vec2, vec3, vec4, uv, color, wgslFn, tslFn, attribute, uniform, storage, positionWorld, normalLocal, normalWorld } from 'three/nodes';
export {THREE, WebGPU, WebGPURenderer, OrbitControls, RenderPass, ShaderPass, EffectComposer, CopyShader, ShaderNode, MeshBasicNodeMaterial, texture, color, vec2, vec3, vec4, wgslFn, tslFn, uv, attribute, uniform, storage, positionWorld, normalLocal, normalWorld};
If I import "three.module.js" without importmap it works. Can it be that workers cannot see the importmap in index.html and therefore cannot import THREE? I don't have any error messages, but you rarely get them if workers don't work because workers run independently of the main code.
if I do it like this in the worker it works:
//import {THREE} from 'three-defs.js';
import * as THREE from "../node_modules/three/build/three.module.js';
Does anyone have experience in dealing with import maps and workers?