压缩 THREE.js
Minify THREE.js
This plugin helps projects who use THREE.js shrink their size by:
three / three/build/three.module.js to three/src/Three.js.three as side-effect free.This plugin also resolves three-nodes and three-addons modules.
npm install --save-dev @yushijinhun/three-minifier-rolluprollup.config.js:
import { threeMinifier } from "@yushijinhun/three-minifier-rollup";
...
export default {
...
plugins: [
threeMinifier(), // {
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. SvelteKit uses Vite as its build tool, so the setup is the same as for Vite.
npm install --save-dev @yushijinhun/three-minifier-rollupvite.config.(js|ts):
import { defineConfig } from "vite";
import { threeMinifier } from "@yushijinhun/three-minifier-rollup";
// import { sveltekit } from '@sveltejs/kit/experimental/vite';
export default defineConfig({
plugins: [
{ ...threeMinifier(), enforce: "pre" }, // 354K
* Webpack: 582K => 354K
### Do I need to modify any existing code?
No. These are the acceptable approaches to importing THREE.js:
```javascript
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 { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// import { OrbitControls } from "three-addons/controls/OrbitControls"; // equivalent to the previous one
import { NodeMaterial } from "three/examples/jsm/nodes/materials/Materials";
// import { NodeMaterial } from "three-nodes/materials/Materials"; // equivalent to the previous oneIn 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.
暂无开放 Issues,或尚未同步最近议题。