Skip to content

Instantly share code, notes, and snippets.

@guersam
Last active July 26, 2022 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guersam/7d9aed0b32a6f7910894f54420fe5358 to your computer and use it in GitHub Desktop.
Save guersam/7d9aed0b32a6f7910894f54420fe5358 to your computer and use it in GitHub Desktop.
Vite plugin for dependency monkey patching
import { createLogger } from 'vite';
export function monkeyPatchVegaScenegraph() {
const name = 'monkey-patch-vega-scenegraph';
const logger = createLogger('info', { prefix: `[${name}]` });
const logOptions = {timestamp: true};
const replaceCriteria = /touchmove\(evt\) \{\s*this\.fire/m;
return {
name: name,
transform(code, id) {
if (id.includes('react-vega.js')) {
if (replaceCriteria.test(code)) {
logger.info(`Temporarily monkey patching vega-scenegraph. Refer to: https://github.com/vega/vega/pull/3547`, logOptions);
const transformedCode = code.replace(
replaceCriteria,
`touchmove(evt) {
this._touch = this.pickEvent(evt.changedTouches[0]);
this.fire`);
return {
code: transformedCode,
map: null,
};
} else {
logger.warn(`vega-scenegraph is not monkey patched as intended. Check out if https://github.com/vega/vega/pull/3547 is resolved.`, logOptions);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment