diff --git a/package-lock.json b/package-lock.json index 77fd83a8..d242d805 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6547,14 +6547,13 @@ "resolve": "1.5.0" } }, - "rollup-plugin-replace": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/rollup-plugin-replace/-/rollup-plugin-replace-2.0.0.tgz", - "integrity": "sha512-pK9mTd/FNrhtBxcTBXoh0YOwRIShV0gGhv9qvUtNcXHxIMRZMXqfiZKVBmCRGp8/2DJRy62z2JUE7/5tP6WxOQ==", + "rollup-plugin-re": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/rollup-plugin-re/-/rollup-plugin-re-1.0.6.tgz", + "integrity": "sha1-odWCWCSzSK60JwpFb7ukSrRf3U8=", "dev": true, "requires": { - "magic-string": "0.22.4", - "minimatch": "3.0.4", + "magic-string": "0.16.0", "rollup-pluginutils": "2.0.1" }, "dependencies": { @@ -6564,6 +6563,15 @@ "integrity": "sha1-5rGlHPcpJSTnI3wxLl/mZgwc4ao=", "dev": true }, + "magic-string": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.16.0.tgz", + "integrity": "sha1-lw67DacZMwEoX7GqZQ85vdgetFo=", + "dev": true, + "requires": { + "vlq": "0.2.3" + } + }, "rollup-pluginutils": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.0.1.tgz", diff --git a/package.json b/package.json index 496f42d3..ba1660de 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/rollup-replace.js b/scripts/rollup-replace.js index 1b730742..13952447 100644 --- a/scripts/rollup-replace.js +++ b/scripts/rollup-replace.js @@ -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 || {})); }