diff --git a/README.md b/README.md index 4b219be..664fb79 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Plugin.js b/src/Plugin.js index 70f47d3..f3d0837 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -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; }); });