Minify THREE.js
This plugin helps projects who use THREE.js shrink their size by:
- Resolve
three/three/build/three.module.jstothree/src/Three.js.- This makes it possible for bundlers to perform better tree-shaking.
- Mark
threeas side-effect free. - Replace WebGL constants with literals.
- Minify GLSL files.
- node >= v12.0
- three.js >= r120
- Webpack plugin requires Webpack 5
npm install --save-dev @yushijinhun/three-minifier-rollup
rollup.config.js:
import { threeMinifier } from "@yushijinhun/three-minifier-rollup";
...
export default {
...
plugins: [
threeMinifier(), // <=== Add plugin on the FIRST line
...
]
};npm install --save-dev @yushijinhun/three-minifier-webpack
webpack.config.js:
const ThreeMinifierPlugin = require("@yushijinhun/three-minifier-webpack");
const threeMinifier = new ThreeMinifierPlugin();
...
module.exports = {
...
plugins: [
threeMinifier, // <=== (1) Add plugin on the FIRST line
...
],
resolve: {
plugins: [
threeMinifier.resolver, // <=== (2) Add resolver on the FIRST line
...
]
}
};npm install --save-dev @yushijinhun/three-minifier-webpack
next.config.js:
const ThreeMinifierPlugin = require("@yushijinhun/three-minifier-webpack");
module.exports = {
webpack: (config, { isServer, dev }) => {
if (!isServer && !dev) {
config.cache = false;
const threeMinifier = new ThreeMinifierPlugin();
config.plugins.unshift(threeMinifier);
config.resolve.plugins.unshift(threeMinifier.resolver);
}
return config;
},
};Most Rollup plugins are compatible with Vite, including three-minifier.
npm install --save-dev @yushijinhun/three-minifier-rollup
vite.config.js:
import { defineConfig } from "vite";
import { threeMinifier } from "@yushijinhun/three-minifier-rollup";
export default defineConfig({
plugins: [
{ ...threeMinifier(), enforce: "pre" } // <=== Add plugin here
]
});SvelteKit uses Vite as its build tool, so the setup is similar to Vite.
npm install --save-dev @yushijinhun/three-minifier-rollup
svelte.config.js:
import adapter from "@sveltejs/adapter-auto";
import { threeMinifier } from "@yushijinhun/three-minifier-rollup";
export default {
kit: {
adapter: adapter(),
target: "#svelte",
vite: {
plugins: [
{ ...threeMinifier(), enforce: "pre" } // <=== Add plugin here
]
}
}
};Yes!
Consider the following example:
import { WebGLRenderer } from "three";
console.log(WebGLRenderer);- Rollup: 576K => 354K
- Webpack: 582K => 354K
No. These are the acceptable approaches to importing THREE.js:
import { ... } from "three";
import { ... } from "three/build/three.module.js";
import { ... } from "three/src/Three";
import { ... } from "three/src/math/vector3";
// or something like theseYes. This plugin solves mrdoob/three.js#17482.
You do not need to do any extra work to use examples jsm modules.
import { WebGLRenderer } from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// it works wellIn order to make THREE.js tree-shakable, efforts have been made by many people on the upstream project. However, THREE.js hasn't come up with a feasible solution so far. See related issues to learn more.
- Importing examples jsm modules causes bundlers to bundle three.js source code twice mrdoob/three.js#17482
- ReactAreaLights do not seem to work in a module bundler mrdoob/three.js#17220
- Add sideEffects: false flag to package.json to allow tree shaking mrdoob/three.js#16059
- Allow tree-shaking by adding "sideEffects": false flag mrdoob/three.js#16317
- Enable tree-shaking both for the main and examples files mrdoob/three.js#16301
- Support esm on node with conditional exports mrdoob/three.js#18498
- vxna/optimize-three-webpack-plugin
- mattdesl/threejs-tree-shake