Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rework three example build, to fix webpack #53

Merged
merged 2 commits into from
Feb 20, 2018
Merged
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
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 || {}));
}