Skip to content

Commit

Permalink
fix: rework three example build, to fix webpack (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Feb 20, 2018
1 parent 44b6d41 commit 382156b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
20 changes: 14 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"rollup-plugin-json": "^2.1.1",
"rollup-plugin-multi-entry": "^2.0.1",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-re": "^1.0.6",
"rollup-watch": "^3.2.2",
"semver": "^5.3.0",
"sinon": "^2.2.0",
Expand Down
37 changes: 26 additions & 11 deletions scripts/rollup-replace.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import replace from 'rollup-plugin-replace';
import replace from 'rollup-plugin-re';

export default function(options) {
// three modules to find-replace in
const modules = [
'VRControls',
'VREffect',
'OrbitControls'
];

export default function(options) {
return replace(Object.assign({
'include': ['node_modules/three/examples/js/**'],
'delimiters': ['', ''],
'THREE.VRControls =': "var THREE = require('three');var VRControls;\nmodule.exports = VRControls =",
'THREE.VRControls': 'VRControls',
include: ['node_modules/three/examples/js/**'],
patterns: [
{transform(code, id) {
modules.forEach((m) => {
if (!(new RegExp(m)).test(id)) {
return;
}

// trun the global modifiction into an import and a local variable definition
code = code.replace(`THREE.${m} =`, `import * as THREE from 'three';\nvar ${m} =`);

'THREE.VREffect =': "var THREE = require('three');var VREffect;\nmodule.exports = VREffect =",
'THREE.VREffect': 'VREffect',
// change references from the global modification to the local variable
code = code.replace(new RegExp(`THREE.${m}`, 'g'), m);

'THREE.OrbitControls =': "var THREE = require('three');var OrbitControls;\nmodule.exports = OrbitControls =",
'THREE.OrbitControls': 'OrbitControls'
}, options || {}));
// export that local variable as default from this module
code += `\nexport default ${m};`;
});
return code;
}}
]}, options || {}));
}

0 comments on commit 382156b

Please sign in to comment.