简体   繁体   English

如何使 npm package 使用 three.js 没有依赖?

[英]How to make npm package that uses three.js with no dependece?

The problem is this: I want to make a npm package that creates procedural objects for threejs.问题是这样的:我想做一个 npm package 为threejs创建程序对象。 But I don't know how to link three.js in my code.但我不知道如何在我的代码中链接 three.js。 I tried to stablish a dependece and using something like我试图建立一个依赖并使用类似的东西

const THREE = require('three');

Function Example(){
return new THREE.TextureLoader()load('./textures/texture.png');
}

module.exports.Example = Example;

This works nice with other constructors, like scenes, and vectors, however for TextureLoader() it generates an error of an undefined document (because there is not document defined for this THREE).这适用于其他构造函数,例如场景和向量,但是对于TextureLoader() ,它会生成未定义文档的错误(因为没有为这三个定义文档)。 I suspect that this is not the architecture that I should use.我怀疑这不是我应该使用的架构。 I saw in some libraries they load THREE with a function like:我在一些库中看到他们使用 function加载三个,例如:

var _loader;
myLib.install = function (libs) {
        THREE = libs.THREE;
        _loader = new THREE.TextureLoader();

    };

So in this case there is not actual dependency on three.js, calling installation function is necessary thou;所以在这种情况下,对 three.js 没有实际依赖,调用安装 function 是必要的; therefore require() function is not needed anymore.因此不再require() function 。 I would like to understand how to implement this in my code.我想了解如何在我的代码中实现这一点。 Thanks beforehand for the help.预先感谢您的帮助。

I've figured it out.我已经想通了。

I found the solution in this answer to another question.我在另一个问题的答案中找到了解决方案。 Just changing el for THREE and using this variable to store Three.js library.只需将el更改为 3 并使用此变量来存储THREE库。
In this way I was able to generate npm package that does not installs Three.js as a dependency as I wanted.通过这种方式,我能够生成 npm package 不安装 Three.js 作为我想要的依赖项。

My code can be seen here .我的代码可以在 这里看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM