feat(core): slim createTresApp alternative for tree-shakeable Three.js usage#1345
feat(core): slim createTresApp alternative for tree-shakeable Three.js usage#1345alvarosabu wants to merge 7 commits intomainfrom
Conversation
…ss registration - Introduced the `extends` prop in the `Context` component to allow users to specify which Three.js classes to register, enabling tree-shaking. - Updated the `TresCanvas` component to utilize the `extends` prop. - Added a new `index.vue` page for manual extension demonstration. - Updated routing to include the new manual extend page. - Added tests to verify the behavior of the `extends` prop.
✅ Deploy Preview for tresjs-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for tresjs-lab ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for cientos-tresjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@tresjs/cientos
@tresjs/core
@tresjs/eslint-config
@tresjs/leches
@tresjs/nuxt
@tresjs/post-processing
commit: |
- Created a new bundle-test application with multiple HTML entry points for different build modes (full, slim, createTres). - Added Vue components for rendering scenes using Tres.js, including AppFull.vue and SceneCreateTres.vue. - Configured Vite for building the application with rollup visualizer integration. - Updated pnpm-lock.yaml to include new dependencies and devDependencies for the bundle-test app. - Introduced various HTML files for visualizing build statistics and rendering different app configurations.
- Added `tsdown` as a dependency and created build scripts for slim and full configurations. - Introduced new HTML stats files for both slim and full builds. - Updated `vite.config.ts` to target ESNext and disable module preload polyfill. - Enhanced `pnpm-lock.yaml` with new dependencies and updated versions for existing packages.
| expect(catalogue.value).toHaveProperty('Mesh') | ||
| expect(catalogue.value).toHaveProperty('BoxGeometry') | ||
| sceneWrapper.unmount() | ||
| }) |
There was a problem hiding this comment.
suggestion(non-blocking): It would be nice to have a test for the manual extending here.
| import { extend as catalogueExtend } from '../../core/catalogue' | ||
| import type { TresContext } from '../useTresContextProvider' | ||
|
|
||
| type ContextExposed = { context: ShallowRef<TresContext>, dispose: () => void } |
There was a problem hiding this comment.
suggestion: The lint check fails. This should be an interface.
| type ContextExposed = { context: ShallowRef<TresContext>, dispose: () => void } | |
| interface ContextExposed { context: ShallowRef<TresContext>, dispose: () => void } |
| Object.assign(canvas.style, { | ||
| display: 'block', | ||
| width: '100%', | ||
| height: '100%', | ||
| position: windowSize ? 'fixed' : 'relative', | ||
| top: '0', | ||
| left: '0', | ||
| pointerEvents: 'auto', | ||
| touchAction: 'none', | ||
| }) |
There was a problem hiding this comment.
suggestion: For better maintainability, I suggest putting the styles in a central position and using them in both this composable and TresCanvas.
| let contextInstance: ContextExposed | null = null | ||
| const eventHandlers: Partial<Record<keyof ContextEmits, ((...args: unknown[]) => void)[]>> = {} | ||
|
|
||
| function buildEmitBindings(): Record<string, (...args: unknown[]) => void> { |
There was a problem hiding this comment.
nitpick: I think arrow functions are more common in our codebase.
| function buildEmitBindings(): Record<string, (...args: unknown[]) => void> { | |
| const buildEmitBindings = (): Record<string, (...args: unknown[]) => void> => { |
| on(event, handler) { | ||
| if (!eventHandlers[event]) { | ||
| eventHandlers[event] = [] | ||
| } | ||
| eventHandlers[event]!.push(handler as (...args: unknown[]) => void) | ||
| return tresApp | ||
| }, |
There was a problem hiding this comment.
question: How is this supposed to work? eventHandlers is already used above in it's original state. I tried using event hooks for context emits here, but it dod not work. What was the intention here?
| /* import { | ||
| AmbientLight, | ||
| BoxGeometry, | ||
| DirectionalLight, | ||
| Mesh, | ||
| MeshStandardMaterial, | ||
| PerspectiveCamera, | ||
| } from 'three' | ||
| import { createTresApp } from '@tresjs/core/slim' | ||
| import SceneCreateTres from './SceneCreateTres.vue' | ||
|
|
||
| const canvas = document.getElementById('canvas') as HTMLCanvasElement | ||
|
|
||
| const root = createTresApp(canvas, { windowSize: true }) | ||
|
|
||
| root | ||
| .extend({ AmbientLight, BoxGeometry, DirectionalLight, Mesh, MeshStandardMaterial, PerspectiveCamera }) | ||
| .render(SceneCreateTres) | ||
| */ |
There was a problem hiding this comment.
| /* import { | |
| AmbientLight, | |
| BoxGeometry, | |
| DirectionalLight, | |
| Mesh, | |
| MeshStandardMaterial, | |
| PerspectiveCamera, | |
| } from 'three' | |
| import { createTresApp } from '@tresjs/core/slim' | |
| import SceneCreateTres from './SceneCreateTres.vue' | |
| const canvas = document.getElementById('canvas') as HTMLCanvasElement | |
| const root = createTresApp(canvas, { windowSize: true }) | |
| root | |
| .extend({ AmbientLight, BoxGeometry, DirectionalLight, Mesh, MeshStandardMaterial, PerspectiveCamera }) | |
| .render(SceneCreateTres) | |
| */ |
There was a problem hiding this comment.
issue/suggestion/question: The bundle test pages throw an error and don't show anything. Shall we maybe not have the bundle-test folder in the repo? This might reduce maintenance work in the future.
Summary
createTresAppprogrammatic API as a slim alternative to<TresCanvas>that does not auto-extend the full THREE namespace, enabling proper tree-shakingslim.tsentry point (@tresjs/core/slim) exporting onlycreateTresAppextend(THREE)intoTresCanvasdirectly (no change for existing users)Context(dev-only, dynamic import)apps/bundle-testapp with Rollup Visualizer to compare full vs slim build sizesUsage
Test plan
<TresCanvas>still works as before (extends all of THREE automatically)createTresApprenders scene correctly with only registered classescreateTresAppwith noextend()call does NOT include unused Three.js classes in bundlepnpm t)Closes #1292
Closes TRES-214