0

The problem is this: I want to make a npm package that creates procedural objects for threejs. But I don't know how to link three.js in my code. 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). I suspect that this is not the architecture that I should use. I saw in some libraries they load THREE with a function like:

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; therefore require() function is not needed anymore. I would like to understand how to implement this in my code. Thanks beforehand for the help.

SDEscobedo
  • 385
  • 1
  • 2
  • 11

1 Answers1

0

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.
In this way I was able to generate npm package that does not installs Three.js as a dependency as I wanted.

My code can be seen here.

jasie
  • 2,192
  • 10
  • 39
  • 54
SDEscobedo
  • 385
  • 1
  • 2
  • 11