Provided below is a solution that serves as a partial answer and workaround instead of an "official" response. It is hoped that this will be beneficial to others.
To summarize:
In the webpack.config.js
file, define THREE
as an external module:
externals: {
three: 'THREE'
},
This prevents THREE
from being included in the bundle.js
, which has its advantages and disadvantages. Overall, it is a positive outcome.
Concluding HTML script tags:
...
<script src="http://threejs.org/build/three.min.js"></script>
<script src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="bundle.js"></script>
</body>
</html>
It should be noted that "external" does not imply "must originate from a third-party site". Rather, it signifies "not included in bundle.js".
More Detailed Explanation
I had been attempting to address numerous issues simultaneously. I had come to a realization similar to this:
"WebPack can gather all dependencies, such as THREE and trackball modules along with other THREE utility modules, incorporate them with typings information, package, compress, and bundle them into a single bundle.js file - so might as well do everything."
While this statement holds true, it is not the most efficient method to proceed.
Instead, I adopted the viewpoint that WebPack can neatly package the application code, and while it can package the vendor code too, doing so leads to various peculiar issues for limited benefit - thus, keeping those dependencies external is preferred.
In my attempts to include both THREE
and trackball controls within the bundle.js
, I encountered significant challenges. Integrating THREE
itself was relatively straightforward and successful on the initial attempt, but incorporating the trackball or orbit controls presented perplexing obstacles.
There were multiple hurdles to overcome.
One issue stemmed from inaccurately named typing information for dt
:
$ ls -C1 typings/globals/
three
three-orbitcontrols
three-trackballcontrols
These names are intuitive and sensible - however, the NPM modules differ:
$ grep -i three package.json
"three": "^0.77.1",
"three-orbit-controls": "^72.0.0",
"three.trackball": "0.0.1"
While uncertain, it appears that these naming disparities cause webpack difficulties in identifying which modules to include in the bundle based on imports.
Even after successfully importing, resolving the name THREE.OrbitControls
proved elusive.
The NPM module for the OrbitControls
exports a function for invocation, requiring the existing THREE
module as an argument before incorporating the OrbitControls
. Despite this, the webpack initialization code lacks knowledge on how to execute this function, leaving me at a loss for resolution.
The following code snippet would result in compilation errors yet generate functional JS code:
import OrbitControls = require('three-orbitcontrols');
THREE.OrbitControls = OrbitControls(THREE); // compile error but still emits JS that works
This approach demanded excessive effort for a simple concept: renaming directories, tweaking startup code, overlooking compile-time errors...
It gradually became apparent that there is no pressing need for THREE
to be contained within the bundle, nor does it matter if the OrbitControls
possess types or not. In fact, having a smaller bundle equates to improved speed and efficiency.
How can THREE
be removed from the bundle? Simply declare it as external
. Subsequently, the default NPM and typings modules operate seamlessly. Furthermore, the necessity of typings modules for the orbit or trackball controls remains uncertain.