@hoversource/babel-plugin-react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ import { PluginObj } from "@babel/core";
2
+ export default function babelPluginReactHoverSource(): PluginObj;
3
+ export declare function vitePluginReactHoverSource(): {
4
+ name: string;
5
+ enforce: "pre";
6
+ transform(code: string, id: string): {
7
+ code: string | undefined;
8
+ map: {
9
+ version: number;
10
+ sources: string[];
11
+ names: string[];
12
+ sourceRoot?: string | undefined;
13
+ sourcesContent?: string[] | undefined;
14
+ mappings: string;
15
+ file: string;
16
+ } | undefined;
17
+ } | null;
18
+ };
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ import { types as t, transformSync } from "@babel/core";
2
+ export default function babelPluginReactHoverSource() {
3
+ return {
4
+ name: "babel-plugin-react-hoversource",
5
+ visitor: {
6
+ JSXOpeningElement(path, state) {
7
+ const location = path.node.loc;
8
+ if (!location)
9
+ return;
10
+ const filename = state.file.opts.filename;
11
+ if (!filename)
12
+ return;
13
+ // Skip node_modules and common library files
14
+ if (filename.includes("node_modules"))
15
+ return;
16
+ // Get project root relative path
17
+ const projectRoot = state.file.opts.root || process.cwd();
18
+ let relativePath = filename;
19
+ if (filename.startsWith(projectRoot)) {
20
+ relativePath = filename.slice(projectRoot.length).replace(/^[/\\]/, "");
21
+ }
22
+ // Normalize slashes to forward slashes
23
+ relativePath = relativePath.replaceAll("\\", "/");
24
+ const line = location.start.line;
25
+ const column = location.start.column + 1; // 1-based column
26
+ const locStr = `${relativePath}:${line}:${column}`;
27
+ // Check if data-hoversource-loc attribute already exists
28
+ const hasAttr = path.node.attributes.some((attr) => t.isJSXAttribute(attr) && attr.name.name === "data-hoversource-loc");
29
+ if (!hasAttr) {
30
+ path.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("data-hoversource-loc"), t.stringLiteral(locStr)));
31
+ }
32
+ }
33
+ }
34
+ };
35
+ }
36
+ export function vitePluginReactHoverSource() {
37
+ const pluginInstance = babelPluginReactHoverSource();
38
+ return {
39
+ name: "vite-plugin-react-hoversource",
40
+ enforce: "pre",
41
+ transform(code, id) {
42
+ if (id.includes("node_modules") || !/\.[jt]sx$/.test(id)) {
43
+ return null;
44
+ }
45
+ const result = transformSync(code, {
46
+ filename: id,
47
+ plugins: [pluginInstance],
48
+ ast: false,
49
+ code: true,
50
+ sourceMaps: true,
51
+ });
52
+ return result ? { code: result.code || undefined, map: result.map || undefined } : null;
53
+ }
54
+ };
55
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@hoversource/babel-plugin-react",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "build": "tsc -b",
15
+ "clean": "tsc -b --clean"
16
+ },
17
+ "dependencies": {
18
+ "@babel/core": "^7.23.0"
19
+ },
20
+ "devDependencies": {
21
+ "@types/babel__core": "^7.20.5"
22
+ }
23
+ }