Skip to content

Creating a build with vite causes method to be undefined #15319

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

Closed
7 tasks done
hybridherbst opened this issue Dec 11, 2023 · 11 comments
Closed
7 tasks done

Creating a build with vite causes method to be undefined #15319

hybridherbst opened this issue Dec 11, 2023 · 11 comments

Comments

@hybridherbst
Copy link

Describe the bug

The code in the reproduction case only breaks when making a build. Local development works fine.

Related issue on the three.js side to keep track of it:

I have tracked down the difference between local run and a build run being that functionNode.call is undefined in builds, but defined and working in local development:

I am not 100% sure if this is a vite bug during bundling but I don't know where the discrepancy comes from.

Reproduction

https://stackblitz.com/edit/vitejs-vite-9b37bu

Steps to reproduce

  1. Go to https://stackblitz.com/edit/vitejs-vite-9b37bu
  2. Open the browser console
  3. Run npm run start
  4. Note no errors (code runs fine)
  5. Cancel the run by pressing Ctrl + C in the terminal
  6. Run npm run preview which builds the code and hosts the resulting bundled code
  7. In the console, note error: Uncaught TypeError: functionNode.call is not a function

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.10.5 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.0.4 => 5.0.7

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Dec 11, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@hybridherbst hybridherbst changed the title Creating a build breaks three.js code Creating a build with vite causes method to be undefined Dec 11, 2023
@XiSenao
Copy link
Collaborator

XiSenao commented Dec 13, 2023

It seems that rollup removes modules with side effects during the build phase, which causes errors.

// vite.config.ts
import { defineConfig } from 'vite';
export default defineConfig(async ({ command }) => {
  return {
    build: {
      rollupOptions: {
        treeshake: false,
      },
    },
  };
});

@hybridherbst
Copy link
Author

Thanks for the comment. It's good to know that it is treeshaking-related, but I certainly want to keep that turned on. I assume that means the source code needs to change and not have side effects? Are there good docs for where to start with understanding what's being treeshaked any why?

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 13, 2023

You can refer to the document, which may be helpful for you.

The three/examples/jsm/nodes/Nodes.js module has side effects, a relatively simple way can be referred to as follows.

import * as jsmNodes from 'three/examples/jsm/nodes/Nodes.js';
const {
  mx_noise_vec3,
  timerLocal,
  MeshPhysicalNodeMaterial,
  normalWorld,
} = jsmNodes;

This issue seems to be unrelated to Vite itself. If this is a problem, you can relate the current issue to the behavior of Rollup handling tree-shaking.

@hybridherbst
Copy link
Author

Turns out the three.js package already declares these particular files as having side effects in its package.json.

"sideEffects": ["./examples/jsm/nodes/Nodes.js"],

Is that ignored in vite?

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 13, 2023

The vite build process depends on rollup. I understand that the sideEffects field in package.json should be of concern to rollup.

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 13, 2023

I tried to use rollup to directly build three/examples/jsm/nodes/Nodes.js module, and found that some modules in the built artifact were tree-shaken, it seems that the sideEffects field did not limit rollup's behavior 🤔

https://stackblitz.com/edit/vitejs-vite-pxc7td?file=rollup.config.js

@ycw
Copy link

ycw commented Dec 14, 2023

Turns out the three.js package already declares these particular files as having side effects in its package.json.
"sideEffects": ["./examples/jsm/nodes/Nodes.js"],

Only that exact single module is side effectful, all the modules re-exported from there aren't; If some of them are indeed side effectful, you should mark them in sideEffects, in your case:

- "sideEffects": ["./examples/jsm/nodes/Nodes.js"],
+ "sideEffects": ["./examples/jsm/nodes/Nodes.js", "./examples/jsm/nodes/core/FunctionCallNode.js"],

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 14, 2023

Yeah. According to my understanding, all modules in ./examples/jsm/nodes have side effects, so the value of the sideEffects field in https://github.com/mrdoob/three.js/blob/dev/package.json should be ./examples/jsm/nodes/**/* instead of ./examples/jsm/nodes/Nodes.js.

@XiSenao
Copy link
Collaborator

XiSenao commented Dec 14, 2023

If this issue has been resolved, you can close it. @hybridherbst

@sapphi-red
Copy link
Member

I checked the reproduction with the sideEffects field change applied (mrdoob/three.js#27360 (comment)) and the error didn't happen.
Closing as this is not a bug in Vite.

@XiSenao Thanks for investigating!

@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants