monkeytype/frontend/vite-plugins/jquery-inject.ts
Christian Fehmer 8cce5bfc7e
build: combine vite config into a single file (@fehmer) (#7190)
- **build: replace dotenv with vite env variables (@fehmer)**
- **build: combine vite config into a single file (@fehmer)**

---------

Co-authored-by: Miodec <jack@monkeytype.com>
2025-12-05 19:45:12 +01:00

29 lines
846 B
TypeScript

import { Plugin } from "vite";
import MagicString from "magic-string";
export function jqueryInject(): Plugin {
return {
name: "simple-jquery-inject",
async transform(src: string, id: string) {
if (id.endsWith(".ts")) {
//check if file has a jQuery or $() call
if (/(?:jQuery|\$)\([^)]*\)/.test(src)) {
const s = new MagicString(src);
//if file has "use strict"; at the top, add it below that line, if not, add it at the very top
if (src.startsWith(`"use strict";`)) {
s.appendRight(12, `\nimport $ from "jquery";`);
} else {
s.prepend(`import $ from "jquery";`);
}
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
};
}
}
return;
},
};
}