Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Added ability to set base path(s) through plugin constructor #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ module.exports = {
//...
plugins: [
//...
new ThreeWebpackPlugin()
new ThreeWebpackPlugin(/** library base Path or array of library base Paths, post processing folder path**/)
//e.g
// new ThreeWebpackPlugin('three/');
//or
// new ThreeWebpackPlugin('xtras/lib/');
//or
// new ThreeWebpackPlugin(['three/', 'xtras/lib/']);
//or
// new ThreeWebpackPlugin(['three/', 'xtras/lib/'], 'postprocessing/');
]
};
````
(**Note:** default values for library base path and post processing folder path are `'three/examples/js/'` and `'postprocessing/'` respectively)

You can now import the classes in your application:
````js
Expand Down
80 changes: 49 additions & 31 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,65 @@
'use strict';
const PLUGIN_ID = 'wildpeaks-three';
const Loader = require.resolve('./Loader');
const path = require('path');

class Plugin {
constructor(libPath, postProcessingPath){
this.libPaths = [];
this.postProcessingPath = path.normalize(postProcessingPath || 'postprocessing/');

if(Array.isArray(libPath)){
this.libPaths = this.libPaths.concat(libPath.map(p=>path.normalize(p)));
}
else {
this.libPaths.push(typeof libPath === 'string' ? path.normalize(libPath) : path.normalize('three/examples/js/'));
}


}
apply(compiler){ // eslint-disable-line class-methods-use-this
compiler.hooks.normalModuleFactory.tap(PLUGIN_ID, normalModuleFactory => {
normalModuleFactory.hooks.afterResolve.tap(PLUGIN_ID, data => {
const {loaders, rawRequest} = data;
if (rawRequest.startsWith('three/examples/js/')){
const exportId = rawRequest.split('/').pop();
if (rawRequest === 'three/examples/js/postprocessing/EffectComposer'){
loaders.push({
loader: Loader,
options: {
exports: {
EffectComposer: 'THREE.EffectComposer',
Pass: 'THREE.Pass'
const rawRequestNormalized = path.normalize(rawRequest);
this.libPaths.forEach( libPath =>{
if (rawRequestNormalized.startsWith(libPath)){
const effectComposerPath = path.join(libPath, this.postProcessingPath, "EffectComposer");
const exportId = rawRequestNormalized.split(path.sep).pop().replace('.js','');
if (rawRequestNormalized === effectComposerPath){
loaders.push({
loader: Loader,
options: {
exports: {
EffectComposer: 'THREE.EffectComposer',
Pass: 'THREE.Pass'
}
}
}
});
} else if (rawRequest.startsWith('three/examples/js/postprocessing/')){
loaders.push({
loader: Loader,
options: {
requires: [
'three/examples/js/postprocessing/EffectComposer'
],
exports: {
[exportId]: `THREE.${exportId}`
});
} else if (rawRequestNormalized.startsWith(path.join(libPath, this.postProcessingPath))){
loaders.push({
loader: Loader,
options: {
requires: [
effectComposerPath
],
exports: {
[exportId]: `THREE.${exportId}`
}
}
}
});
} else {
loaders.push({
loader: Loader,
options: {
exports: {
[exportId]: `THREE.${exportId}`
});
} else {
loaders.push({
loader: Loader,
options: {
exports: {
[exportId]: `THREE.${exportId}`
}
}
}
});
});
}
}
}
})
return data;
});
});
Expand Down