@tanstack/devtools-vite 0.6.0 → 0.7.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.
Files changed (55) hide show
  1. package/dist/esm/ast-utils.d.ts +11 -0
  2. package/dist/esm/ast-utils.js +44 -0
  3. package/dist/esm/ast-utils.js.map +1 -0
  4. package/dist/esm/devtools-packages.d.ts +2 -0
  5. package/dist/esm/devtools-packages.js +15 -0
  6. package/dist/esm/devtools-packages.js.map +1 -0
  7. package/dist/esm/editor.js +18 -27
  8. package/dist/esm/editor.js.map +1 -1
  9. package/dist/esm/enhance-logs.d.ts +4 -1
  10. package/dist/esm/enhance-logs.js +48 -81
  11. package/dist/esm/enhance-logs.js.map +1 -1
  12. package/dist/esm/index.js +1 -5
  13. package/dist/esm/inject-plugin.d.ts +5 -4
  14. package/dist/esm/inject-plugin.js +171 -220
  15. package/dist/esm/inject-plugin.js.map +1 -1
  16. package/dist/esm/inject-source.d.ts +4 -1
  17. package/dist/esm/inject-source.js +111 -205
  18. package/dist/esm/inject-source.js.map +1 -1
  19. package/dist/esm/matcher.js +11 -16
  20. package/dist/esm/matcher.js.map +1 -1
  21. package/dist/esm/offset-to-loc.d.ts +14 -0
  22. package/dist/esm/offset-to-loc.js +29 -0
  23. package/dist/esm/offset-to-loc.js.map +1 -0
  24. package/dist/esm/package-manager.js +90 -131
  25. package/dist/esm/package-manager.js.map +1 -1
  26. package/dist/esm/plugin.js +329 -448
  27. package/dist/esm/plugin.js.map +1 -1
  28. package/dist/esm/remove-devtools.d.ts +4 -1
  29. package/dist/esm/remove-devtools.js +93 -134
  30. package/dist/esm/remove-devtools.js.map +1 -1
  31. package/dist/esm/utils.js +149 -162
  32. package/dist/esm/utils.js.map +1 -1
  33. package/dist/esm/virtual-console.js +170 -14
  34. package/dist/esm/virtual-console.js.map +1 -1
  35. package/package.json +4 -10
  36. package/skills/devtools-vite-plugin/SKILL.md +7 -7
  37. package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
  38. package/src/ast-utils.ts +54 -0
  39. package/src/devtools-packages.ts +12 -0
  40. package/src/enhance-logs.ts +48 -78
  41. package/src/inject-plugin.test.ts +200 -147
  42. package/src/inject-plugin.ts +195 -238
  43. package/src/inject-source.test.ts +165 -115
  44. package/src/inject-source.ts +183 -231
  45. package/src/offset-to-loc.ts +39 -0
  46. package/src/plugin.ts +35 -29
  47. package/src/remove-devtools.test.ts +76 -57
  48. package/src/remove-devtools.ts +160 -168
  49. package/src/virtual-console.test.ts +172 -1
  50. package/src/virtual-console.ts +146 -7
  51. package/dist/esm/babel.d.ts +0 -18
  52. package/dist/esm/babel.js +0 -19
  53. package/dist/esm/babel.js.map +0 -1
  54. package/dist/esm/index.js.map +0 -1
  55. package/src/babel.ts +0 -18
@@ -0,0 +1,11 @@
1
+ import { Node } from 'oxc-parser';
2
+ /**
3
+ * Iterate over the direct child nodes of an AST node.
4
+ * Uses a per-type cache of which keys hold child nodes to avoid
5
+ * allocating Object.entries() arrays on every call.
6
+ */
7
+ export declare function forEachChild(node: Node, callback: (child: Node) => void): void;
8
+ /**
9
+ * Recursively walk AST nodes, calling `visitor` for each node with a `type`.
10
+ */
11
+ export declare function walk(node: Node, visitor: (node: Node) => void): void;
@@ -0,0 +1,44 @@
1
+ //#region src/ast-utils.ts
2
+ /**
3
+ * Cache of keys that hold child nodes (objects/arrays) per AST node type.
4
+ * Since oxc-parser produces AST via JSON.parse, every instance of a given
5
+ * node type has the same set of keys, so we only need to discover them once.
6
+ */
7
+ var childKeysCache = /* @__PURE__ */ new Map();
8
+ function getChildKeys(node) {
9
+ let keys = childKeysCache.get(node.type);
10
+ if (keys) return keys;
11
+ keys = [];
12
+ for (const key in node) {
13
+ if (key === "type" || key === "start" || key === "end") continue;
14
+ if (typeof node[key] === "object") keys.push(key);
15
+ }
16
+ childKeysCache.set(node.type, keys);
17
+ return keys;
18
+ }
19
+ /**
20
+ * Iterate over the direct child nodes of an AST node.
21
+ * Uses a per-type cache of which keys hold child nodes to avoid
22
+ * allocating Object.entries() arrays on every call.
23
+ */
24
+ function forEachChild(node, callback) {
25
+ const keys = getChildKeys(node);
26
+ for (const key of keys) {
27
+ const value = node[key];
28
+ if (value === null) continue;
29
+ if (Array.isArray(value)) {
30
+ for (const item of value) if (typeof item === "object" && item !== null && "type" in item) callback(item);
31
+ } else if ("type" in value) callback(value);
32
+ }
33
+ }
34
+ /**
35
+ * Recursively walk AST nodes, calling `visitor` for each node with a `type`.
36
+ */
37
+ function walk(node, visitor) {
38
+ visitor(node);
39
+ forEachChild(node, (child) => walk(child, visitor));
40
+ }
41
+ //#endregion
42
+ export { forEachChild, walk };
43
+
44
+ //# sourceMappingURL=ast-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ast-utils.js","names":[],"sources":["../../src/ast-utils.ts"],"sourcesContent":["import type { Node } from 'oxc-parser'\n\n/**\n * Cache of keys that hold child nodes (objects/arrays) per AST node type.\n * Since oxc-parser produces AST via JSON.parse, every instance of a given\n * node type has the same set of keys, so we only need to discover them once.\n */\nconst childKeysCache = new Map<string, Array<string>>()\n\nfunction getChildKeys(node: Node): Array<string> {\n let keys = childKeysCache.get(node.type)\n if (keys) return keys\n\n keys = []\n for (const key in node) {\n if (key === 'type' || key === 'start' || key === 'end') continue\n // typeof null === 'object', so nullable node fields get cached too\n if (typeof (node as any)[key] === 'object') {\n keys.push(key)\n }\n }\n childKeysCache.set(node.type, keys)\n return keys\n}\n\n/**\n * Iterate over the direct child nodes of an AST node.\n * Uses a per-type cache of which keys hold child nodes to avoid\n * allocating Object.entries() arrays on every call.\n */\nexport function forEachChild(node: Node, callback: (child: Node) => void) {\n const keys = getChildKeys(node)\n for (const key of keys) {\n const value = (node as any)[key]\n if (value === null) continue\n if (Array.isArray(value)) {\n for (const item of value) {\n if (typeof item === 'object' && item !== null && 'type' in item) {\n callback(item)\n }\n }\n } else if ('type' in value) {\n callback(value as Node)\n }\n }\n}\n\n/**\n * Recursively walk AST nodes, calling `visitor` for each node with a `type`.\n */\nexport function walk(node: Node, visitor: (node: Node) => void) {\n visitor(node)\n forEachChild(node, (child) => walk(child, visitor))\n}\n"],"mappings":";;;;;;AAOA,IAAM,iCAAiB,IAAI,KAA4B;AAEvD,SAAS,aAAa,MAA2B;CAC/C,IAAI,OAAO,eAAe,IAAI,KAAK,KAAK;CACxC,IAAI,MAAM,OAAO;CAEjB,OAAO,EAAE;CACT,KAAK,MAAM,OAAO,MAAM;EACtB,IAAI,QAAQ,UAAU,QAAQ,WAAW,QAAQ,OAAO;EAExD,IAAI,OAAQ,KAAa,SAAS,UAChC,KAAK,KAAK,IAAI;;CAGlB,eAAe,IAAI,KAAK,MAAM,KAAK;CACnC,OAAO;;;;;;;AAQT,SAAgB,aAAa,MAAY,UAAiC;CACxE,MAAM,OAAO,aAAa,KAAK;CAC/B,KAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAS,KAAa;EAC5B,IAAI,UAAU,MAAM;EACpB,IAAI,MAAM,QAAQ,MAAM;QACjB,MAAM,QAAQ,OACjB,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,UAAU,MACzD,SAAS,KAAK;SAGb,IAAI,UAAU,OACnB,SAAS,MAAc;;;;;;AAQ7B,SAAgB,KAAK,MAAY,SAA+B;CAC9D,QAAQ,KAAK;CACb,aAAa,OAAO,UAAU,KAAK,OAAO,QAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const TANSTACK_DEVTOOLS_PACKAGES: readonly ["@tanstack/react-devtools", "@tanstack/preact-devtools", "@tanstack/solid-devtools", "@tanstack/vue-devtools", "@tanstack/svelte-devtools", "@tanstack/angular-devtools", "@tanstack/devtools"];
2
+ export declare const isTanStackDevtoolsImport: (source: string) => boolean;
@@ -0,0 +1,15 @@
1
+ //#region src/devtools-packages.ts
2
+ var TANSTACK_DEVTOOLS_PACKAGES = [
3
+ "@tanstack/react-devtools",
4
+ "@tanstack/preact-devtools",
5
+ "@tanstack/solid-devtools",
6
+ "@tanstack/vue-devtools",
7
+ "@tanstack/svelte-devtools",
8
+ "@tanstack/angular-devtools",
9
+ "@tanstack/devtools"
10
+ ];
11
+ var isTanStackDevtoolsImport = (source) => TANSTACK_DEVTOOLS_PACKAGES.includes(source);
12
+ //#endregion
13
+ export { TANSTACK_DEVTOOLS_PACKAGES, isTanStackDevtoolsImport };
14
+
15
+ //# sourceMappingURL=devtools-packages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devtools-packages.js","names":[],"sources":["../../src/devtools-packages.ts"],"sourcesContent":["export const TANSTACK_DEVTOOLS_PACKAGES = [\n '@tanstack/react-devtools',\n '@tanstack/preact-devtools',\n '@tanstack/solid-devtools',\n '@tanstack/vue-devtools',\n '@tanstack/svelte-devtools',\n '@tanstack/angular-devtools',\n '@tanstack/devtools',\n] as const\n\nexport const isTanStackDevtoolsImport = (source: string): boolean =>\n (TANSTACK_DEVTOOLS_PACKAGES as ReadonlyArray<string>).includes(source)\n"],"mappings":";AAAA,IAAa,6BAA6B;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,IAAa,4BAA4B,WACtC,2BAAqD,SAAS,OAAO"}
@@ -1,29 +1,20 @@
1
- const DEFAULT_EDITOR_CONFIG = {
2
- name: "VSCode",
3
- open: async (path, lineNumber, columnNumber) => {
4
- const launch = (await import("launch-editor")).default;
5
- launch(
6
- `${path.replaceAll("$", "\\$")}${lineNumber ? `:${lineNumber}` : ""}${columnNumber ? `:${columnNumber}` : ""}`,
7
- void 0,
8
- (filename, err) => {
9
- console.warn(`Failed to open ${filename} in editor: ${err}`);
10
- }
11
- );
12
- }
1
+ //#region src/editor.ts
2
+ var DEFAULT_EDITOR_CONFIG = {
3
+ name: "VSCode",
4
+ open: async (path, lineNumber, columnNumber) => {
5
+ const launch = (await import("launch-editor")).default;
6
+ launch(`${path.replaceAll("$", "\\$")}${lineNumber ? `:${lineNumber}` : ""}${columnNumber ? `:${columnNumber}` : ""}`, void 0, (filename, err) => {
7
+ console.warn(`Failed to open ${filename} in editor: ${err}`);
8
+ });
9
+ }
13
10
  };
14
- const handleOpenSource = async ({
15
- data,
16
- openInEditor
17
- }) => {
18
- const { source, line, column } = data.data;
19
- const lineNum = line ? `${line}` : void 0;
20
- const columnNum = column ? `${column}` : void 0;
21
- if (source) {
22
- return openInEditor(source, lineNum, columnNum);
23
- }
11
+ var handleOpenSource = async ({ data, openInEditor }) => {
12
+ const { source, line, column } = data.data;
13
+ const lineNum = line ? `${line}` : void 0;
14
+ const columnNum = column ? `${column}` : void 0;
15
+ if (source) return openInEditor(source, lineNum, columnNum);
24
16
  };
25
- export {
26
- DEFAULT_EDITOR_CONFIG,
27
- handleOpenSource
28
- };
29
- //# sourceMappingURL=editor.js.map
17
+ //#endregion
18
+ export { DEFAULT_EDITOR_CONFIG, handleOpenSource };
19
+
20
+ //# sourceMappingURL=editor.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"editor.js","sources":["../../src/editor.ts"],"sourcesContent":["type OpenSourceData = {\n type: 'open-source'\n data: {\n /** The source file to open */\n source?: string\n /** The react router route ID, usually discovered via the hook useMatches */\n routeID?: string\n /** The line number in the source file */\n line?: number\n /** The column number in the source file */\n column?: number\n }\n}\n\nexport type EditorConfig = {\n /** The name of the editor, used for debugging purposes */\n name: string\n /** Callback to open a file in the editor */\n open: (\n path: string,\n lineNumber: string | undefined,\n columnNumber?: string,\n ) => Promise<void>\n}\n\nexport const DEFAULT_EDITOR_CONFIG: EditorConfig = {\n name: 'VSCode',\n open: async (path, lineNumber, columnNumber) => {\n const launch = (await import('launch-editor')).default\n launch(\n `${path.replaceAll('$', '\\\\$')}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}`,\n undefined,\n (filename, err) => {\n console.warn(`Failed to open ${filename} in editor: ${err}`)\n },\n )\n },\n}\n\nexport const handleOpenSource = async ({\n data,\n openInEditor,\n}: {\n data: OpenSourceData\n openInEditor: EditorConfig['open']\n}) => {\n const { source, line, column } = data.data\n const lineNum = line ? `${line}` : undefined\n const columnNum = column ? `${column}` : undefined\n if (source) {\n return openInEditor(source, lineNum, columnNum)\n }\n}\n"],"names":[],"mappings":"AAyBO,MAAM,wBAAsC;AAAA,EACjD,MAAM;AAAA,EACN,MAAM,OAAO,MAAM,YAAY,iBAAiB;AAC9C,UAAM,UAAU,MAAM,OAAO,eAAe,GAAG;AAC/C;AAAA,MACE,GAAG,KAAK,WAAW,KAAK,KAAK,CAAC,GAAG,aAAa,IAAI,UAAU,KAAK,EAAE,GAAG,eAAe,IAAI,YAAY,KAAK,EAAE;AAAA,MAC5G;AAAA,MACA,CAAC,UAAU,QAAQ;AACjB,gBAAQ,KAAK,kBAAkB,QAAQ,eAAe,GAAG,EAAE;AAAA,MAC7D;AAAA,IAAA;AAAA,EAEJ;AACF;AAEO,MAAM,mBAAmB,OAAO;AAAA,EACrC;AAAA,EACA;AACF,MAGM;AACJ,QAAM,EAAE,QAAQ,MAAM,OAAA,IAAW,KAAK;AACtC,QAAM,UAAU,OAAO,GAAG,IAAI,KAAK;AACnC,QAAM,YAAY,SAAS,GAAG,MAAM,KAAK;AACzC,MAAI,QAAQ;AACV,WAAO,aAAa,QAAQ,SAAS,SAAS;AAAA,EAChD;AACF;"}
1
+ {"version":3,"file":"editor.js","names":[],"sources":["../../src/editor.ts"],"sourcesContent":["type OpenSourceData = {\n type: 'open-source'\n data: {\n /** The source file to open */\n source?: string\n /** The react router route ID, usually discovered via the hook useMatches */\n routeID?: string\n /** The line number in the source file */\n line?: number\n /** The column number in the source file */\n column?: number\n }\n}\n\nexport type EditorConfig = {\n /** The name of the editor, used for debugging purposes */\n name: string\n /** Callback to open a file in the editor */\n open: (\n path: string,\n lineNumber: string | undefined,\n columnNumber?: string,\n ) => Promise<void>\n}\n\nexport const DEFAULT_EDITOR_CONFIG: EditorConfig = {\n name: 'VSCode',\n open: async (path, lineNumber, columnNumber) => {\n const launch = (await import('launch-editor')).default\n launch(\n `${path.replaceAll('$', '\\\\$')}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}`,\n undefined,\n (filename, err) => {\n console.warn(`Failed to open ${filename} in editor: ${err}`)\n },\n )\n },\n}\n\nexport const handleOpenSource = async ({\n data,\n openInEditor,\n}: {\n data: OpenSourceData\n openInEditor: EditorConfig['open']\n}) => {\n const { source, line, column } = data.data\n const lineNum = line ? `${line}` : undefined\n const columnNum = column ? `${column}` : undefined\n if (source) {\n return openInEditor(source, lineNum, columnNum)\n }\n}\n"],"mappings":";AAyBA,IAAa,wBAAsC;CACjD,MAAM;CACN,MAAM,OAAO,MAAM,YAAY,iBAAiB;EAC9C,MAAM,UAAU,MAAM,OAAO,kBAAkB;EAC/C,OACE,GAAG,KAAK,WAAW,KAAK,MAAM,GAAG,aAAa,IAAI,eAAe,KAAK,eAAe,IAAI,iBAAiB,MAC1G,KAAA,IACC,UAAU,QAAQ;GACjB,QAAQ,KAAK,kBAAkB,SAAS,cAAc,MAAM;IAE/D;;CAEJ;AAED,IAAa,mBAAmB,OAAO,EACrC,MACA,mBAII;CACJ,MAAM,EAAE,QAAQ,MAAM,WAAW,KAAK;CACtC,MAAM,UAAU,OAAO,GAAG,SAAS,KAAA;CACnC,MAAM,YAAY,SAAS,GAAG,WAAW,KAAA;CACzC,IAAI,QACF,OAAO,aAAa,QAAQ,SAAS,UAAU"}
@@ -1 +1,4 @@
1
- export declare function enhanceConsoleLog(code: string, id: string, port: number): import('@babel/generator').GeneratorResult | undefined;
1
+ export declare function enhanceConsoleLog(code: string, id: string, port: number): {
2
+ code: string;
3
+ map: import('magic-string').SourceMap;
4
+ } | undefined;
@@ -1,84 +1,51 @@
1
- import chalk from "chalk";
1
+ import { walk } from "./ast-utils.js";
2
+ import { createLocMapper } from "./offset-to-loc.js";
2
3
  import { normalizePath } from "vite";
3
- import { gen, trav } from "./babel.js";
4
- import { parse } from "@babel/parser";
5
- import * as t from "@babel/types";
6
- const transform = (ast, filePath, port) => {
7
- let didTransform = false;
8
- trav(ast, {
9
- CallExpression(path) {
10
- const callee = path.node.callee;
11
- if (callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "console" && callee.property.type === "Identifier" && (callee.property.name === "log" || callee.property.name === "error")) {
12
- const location = path.node.loc;
13
- if (!location) {
14
- return;
15
- }
16
- const [lineNumber, column] = [
17
- location.start.line,
18
- location.start.column
19
- ];
20
- const finalPath = `${filePath}:${lineNumber}:${column + 1}`;
21
- const logMessage = `${chalk.magenta("LOG")} ${chalk.blueBright(`${finalPath}`)}
22
- → `;
23
- const serverLogMessage = t.arrayExpression([
24
- t.stringLiteral(logMessage)
25
- ]);
26
- const browserLogMessage = t.arrayExpression([
27
- // LOG with css formatting specifiers: %c
28
- t.stringLiteral(
29
- `%c${"LOG"}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c
30
- → `
31
- ),
32
- // magenta
33
- t.stringLiteral("color:#A0A"),
34
- t.stringLiteral("color:#FFF"),
35
- // blueBright
36
- t.stringLiteral("color:#55F"),
37
- t.stringLiteral("color:#FFF")
38
- ]);
39
- const checkServerCondition = t.binaryExpression(
40
- "===",
41
- t.unaryExpression("typeof", t.identifier("window")),
42
- t.stringLiteral("undefined")
43
- );
44
- path.node.arguments.unshift(
45
- t.spreadElement(
46
- t.conditionalExpression(
47
- checkServerCondition,
48
- serverLogMessage,
49
- browserLogMessage
50
- )
51
- )
52
- );
53
- didTransform = true;
54
- }
55
- }
56
- });
57
- return didTransform;
58
- };
4
+ import chalk from "chalk";
5
+ import MagicString from "magic-string";
6
+ import { parseSync } from "oxc-parser";
7
+ //#region src/enhance-logs.ts
8
+ function escapeForStringLiteral(str) {
9
+ return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n");
10
+ }
59
11
  function enhanceConsoleLog(code, id, port) {
60
- const [filePath] = id.split("?");
61
- const location = filePath?.replace(normalizePath(process.cwd()), "");
62
- try {
63
- const ast = parse(code, {
64
- sourceType: "module",
65
- plugins: ["jsx", "typescript"]
66
- });
67
- const didTransform = transform(ast, location, port);
68
- if (!didTransform) {
69
- return;
70
- }
71
- return gen(ast, {
72
- sourceMaps: true,
73
- retainLines: true,
74
- filename: id,
75
- sourceFileName: filePath
76
- });
77
- } catch (e) {
78
- return;
79
- }
12
+ const filePath = id.split("?")[0];
13
+ const location = filePath.replace(normalizePath(process.cwd()), "");
14
+ try {
15
+ const result = parseSync(filePath, code, {
16
+ sourceType: "module",
17
+ lang: "tsx"
18
+ });
19
+ if (result.errors.length > 0) return;
20
+ const offsetToLoc = createLocMapper(code);
21
+ const s = new MagicString(code);
22
+ walk(result.program, (node) => {
23
+ if (node.type !== "CallExpression") return;
24
+ const callee = node.callee;
25
+ if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.object.name === "console" && callee.property.type === "Identifier" && (callee.property.name === "log" || callee.property.name === "error")) {
26
+ const loc = offsetToLoc(node.start);
27
+ const [lineNumber, column] = [loc.line, loc.column];
28
+ const finalPath = `${location}:${lineNumber}:${column + 1}`;
29
+ const spreadStr = `...(typeof window === "undefined" ? ${`["${escapeForStringLiteral(`${chalk.magenta("LOG")} ${chalk.blueBright(`${finalPath}`)}\n → `)}"]`} : ${`["%cLOG%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \\n \\u2192 ","color:#A0A","color:#FFF","color:#55F","color:#FFF"]`}), `;
30
+ let parenOffset = callee.end;
31
+ while (parenOffset < code.length && code[parenOffset] !== "(") parenOffset++;
32
+ s.appendRight(parenOffset + 1, spreadStr);
33
+ }
34
+ });
35
+ if (!s.hasChanged()) return;
36
+ return {
37
+ code: s.toString(),
38
+ map: s.generateMap({
39
+ source: filePath,
40
+ file: id,
41
+ includeContent: true
42
+ })
43
+ };
44
+ } catch (e) {
45
+ return;
46
+ }
80
47
  }
81
- export {
82
- enhanceConsoleLog
83
- };
84
- //# sourceMappingURL=enhance-logs.js.map
48
+ //#endregion
49
+ export { enhanceConsoleLog };
50
+
51
+ //# sourceMappingURL=enhance-logs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"enhance-logs.js","sources":["../../src/enhance-logs.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { normalizePath } from 'vite'\nimport { gen, parse, t, trav } from './babel'\nimport type { types as Babel } from '@babel/core'\nimport type { ParseResult } from '@babel/parser'\n\nconst transform = (\n ast: ParseResult<Babel.File>,\n filePath: string,\n port: number,\n) => {\n let didTransform = false\n\n trav(ast, {\n CallExpression(path) {\n const callee = path.node.callee\n // Match console.log(...) or console.error(...)\n if (\n callee.type === 'MemberExpression' &&\n callee.object.type === 'Identifier' &&\n callee.object.name === 'console' &&\n callee.property.type === 'Identifier' &&\n (callee.property.name === 'log' || callee.property.name === 'error')\n ) {\n const location = path.node.loc\n if (!location) {\n return\n }\n const [lineNumber, column] = [\n location.start.line,\n location.start.column,\n ]\n const finalPath = `${filePath}:${lineNumber}:${column + 1}`\n const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\\n → `\n\n const serverLogMessage = t.arrayExpression([\n t.stringLiteral(logMessage),\n ])\n const browserLogMessage = t.arrayExpression([\n // LOG with css formatting specifiers: %c\n t.stringLiteral(\n `%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \\n → `,\n ),\n // magenta\n t.stringLiteral('color:#A0A'),\n t.stringLiteral('color:#FFF'),\n // blueBright\n t.stringLiteral('color:#55F'),\n t.stringLiteral('color:#FFF'),\n ])\n\n // typeof window === \"undefined\"\n const checkServerCondition = t.binaryExpression(\n '===',\n t.unaryExpression('typeof', t.identifier('window')),\n t.stringLiteral('undefined'),\n )\n\n // ...(isServer ? serverLogMessage : browserLogMessage)\n path.node.arguments.unshift(\n t.spreadElement(\n t.conditionalExpression(\n checkServerCondition,\n serverLogMessage,\n browserLogMessage,\n ),\n ),\n )\n\n didTransform = true\n }\n },\n })\n\n return didTransform\n}\n\nexport function enhanceConsoleLog(code: string, id: string, port: number) {\n const [filePath] = id.split('?')\n // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n const location = filePath?.replace(normalizePath(process.cwd()), '')!\n\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location, port)\n if (!didTransform) {\n return\n }\n return gen(ast, {\n sourceMaps: true,\n retainLines: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return\n }\n}\n"],"names":[],"mappings":";;;;;AAMA,MAAM,YAAY,CAChB,KACA,UACA,SACG;AACH,MAAI,eAAe;AAEnB,OAAK,KAAK;AAAA,IACR,eAAe,MAAM;AACnB,YAAM,SAAS,KAAK,KAAK;AAEzB,UACE,OAAO,SAAS,sBAChB,OAAO,OAAO,SAAS,gBACvB,OAAO,OAAO,SAAS,aACvB,OAAO,SAAS,SAAS,iBACxB,OAAO,SAAS,SAAS,SAAS,OAAO,SAAS,SAAS,UAC5D;AACA,cAAM,WAAW,KAAK,KAAK;AAC3B,YAAI,CAAC,UAAU;AACb;AAAA,QACF;AACA,cAAM,CAAC,YAAY,MAAM,IAAI;AAAA,UAC3B,SAAS,MAAM;AAAA,UACf,SAAS,MAAM;AAAA,QAAA;AAEjB,cAAM,YAAY,GAAG,QAAQ,IAAI,UAAU,IAAI,SAAS,CAAC;AACzD,cAAM,aAAa,GAAG,MAAM,QAAQ,KAAK,CAAC,IAAI,MAAM,WAAW,GAAG,SAAS,EAAE,CAAC;AAAA;AAE9E,cAAM,mBAAmB,EAAE,gBAAgB;AAAA,UACzC,EAAE,cAAc,UAAU;AAAA,QAAA,CAC3B;AACD,cAAM,oBAAoB,EAAE,gBAAgB;AAAA;AAAA,UAE1C,EAAE;AAAA,YACA,KAAK,KAAK,QAAQ,kCAAkC,IAAI,6BAA6B,mBAAmB,SAAS,CAAC,EAAE;AAAA;AAAA,UAAA;AAAA;AAAA,UAGtH,EAAE,cAAc,YAAY;AAAA,UAC5B,EAAE,cAAc,YAAY;AAAA;AAAA,UAE5B,EAAE,cAAc,YAAY;AAAA,UAC5B,EAAE,cAAc,YAAY;AAAA,QAAA,CAC7B;AAGD,cAAM,uBAAuB,EAAE;AAAA,UAC7B;AAAA,UACA,EAAE,gBAAgB,UAAU,EAAE,WAAW,QAAQ,CAAC;AAAA,UAClD,EAAE,cAAc,WAAW;AAAA,QAAA;AAI7B,aAAK,KAAK,UAAU;AAAA,UAClB,EAAE;AAAA,YACA,EAAE;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAAA,UACF;AAAA,QACF;AAGF,uBAAe;AAAA,MACjB;AAAA,IACF;AAAA,EAAA,CACD;AAED,SAAO;AACT;AAEO,SAAS,kBAAkB,MAAc,IAAY,MAAc;AACxE,QAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAE/B,QAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,IAAA,CAAK,GAAG,EAAE;AAEnE,MAAI;AACF,UAAM,MAAM,MAAM,MAAM;AAAA,MACtB,YAAY;AAAA,MACZ,SAAS,CAAC,OAAO,YAAY;AAAA,IAAA,CAC9B;AACD,UAAM,eAAe,UAAU,KAAK,UAAU,IAAI;AAClD,QAAI,CAAC,cAAc;AACjB;AAAA,IACF;AACA,WAAO,IAAI,KAAK;AAAA,MACd,YAAY;AAAA,MACZ,aAAa;AAAA,MACb,UAAU;AAAA,MACV,gBAAgB;AAAA,IAAA,CACjB;AAAA,EACH,SAAS,GAAG;AACV;AAAA,EACF;AACF;"}
1
+ {"version":3,"file":"enhance-logs.js","names":[],"sources":["../../src/enhance-logs.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { normalizePath } from 'vite'\nimport MagicString from 'magic-string'\nimport { parseSync } from 'oxc-parser'\nimport { createLocMapper } from './offset-to-loc'\nimport { walk } from './ast-utils'\n\nfunction escapeForStringLiteral(str: string): string {\n return str.replace(/\\\\/g, '\\\\\\\\').replace(/\"/g, '\\\\\"').replace(/\\n/g, '\\\\n')\n}\n\nexport function enhanceConsoleLog(code: string, id: string, port: number) {\n const filePath = id.split('?')[0]!\n const location = filePath.replace(normalizePath(process.cwd()), '')\n\n try {\n const result = parseSync(filePath, code, {\n sourceType: 'module',\n lang: 'tsx',\n })\n if (result.errors.length > 0) return\n\n const offsetToLoc = createLocMapper(code)\n const s = new MagicString(code)\n\n walk(result.program, (node) => {\n if (node.type !== 'CallExpression') return\n\n const callee = node.callee\n if (\n callee.type === 'MemberExpression' &&\n !callee.computed &&\n callee.object.type === 'Identifier' &&\n callee.object.name === 'console' &&\n callee.property.type === 'Identifier' &&\n (callee.property.name === 'log' || callee.property.name === 'error')\n ) {\n const loc = offsetToLoc(node.start)\n const [lineNumber, column] = [loc.line, loc.column]\n const finalPath = `${location}:${lineNumber}:${column + 1}`\n const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\\n → `\n\n const serverLogMessage = `[\"${escapeForStringLiteral(logMessage)}\"]`\n const browserLogMessage = `[\"%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \\\\n \\\\u2192 \",\"color:#A0A\",\"color:#FFF\",\"color:#55F\",\"color:#FFF\"]`\n\n const spreadStr = `...(typeof window === \"undefined\" ? ${serverLogMessage} : ${browserLogMessage}), `\n\n // Find the opening '(' of the call by scanning forward from callee end\n let parenOffset = callee.end\n while (parenOffset < code.length && code[parenOffset] !== '(') {\n parenOffset++\n }\n // Insert right after '('\n s.appendRight(parenOffset + 1, spreadStr)\n }\n })\n\n if (!s.hasChanged()) return\n\n return {\n code: s.toString(),\n map: s.generateMap({\n source: filePath,\n file: id,\n includeContent: true,\n }),\n }\n } catch (e) {\n return\n }\n}\n"],"mappings":";;;;;;;AAOA,SAAS,uBAAuB,KAAqB;CACnD,OAAO,IAAI,QAAQ,OAAO,OAAO,CAAC,QAAQ,MAAM,OAAM,CAAC,QAAQ,OAAO,MAAM;;AAG9E,SAAgB,kBAAkB,MAAc,IAAY,MAAc;CACxE,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;CAC/B,MAAM,WAAW,SAAS,QAAQ,cAAc,QAAQ,KAAK,CAAC,EAAE,GAAG;CAEnE,IAAI;EACF,MAAM,SAAS,UAAU,UAAU,MAAM;GACvC,YAAY;GACZ,MAAM;GACP,CAAC;EACF,IAAI,OAAO,OAAO,SAAS,GAAG;EAE9B,MAAM,cAAc,gBAAgB,KAAK;EACzC,MAAM,IAAI,IAAI,YAAY,KAAK;EAE/B,KAAK,OAAO,UAAU,SAAS;GAC7B,IAAI,KAAK,SAAS,kBAAkB;GAEpC,MAAM,SAAS,KAAK;GACpB,IACE,OAAO,SAAS,sBAChB,CAAC,OAAO,YACR,OAAO,OAAO,SAAS,gBACvB,OAAO,OAAO,SAAS,aACvB,OAAO,SAAS,SAAS,iBACxB,OAAO,SAAS,SAAS,SAAS,OAAO,SAAS,SAAS,UAC5D;IACA,MAAM,MAAM,YAAY,KAAK,MAAM;IACnC,MAAM,CAAC,YAAY,UAAU,CAAC,IAAI,MAAM,IAAI,OAAO;IACnD,MAAM,YAAY,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS;IAMxD,MAAM,YAAY,uCAAuC,KAH3B,uBAAuB,GAF/B,MAAM,QAAQ,MAAM,CAAC,GAAG,MAAM,WAAW,GAAG,YAAY,CAAC,OAEf,CAAC,IAGS,KAAK,eAFjC,kCAAkC,KAAK,4BAA4B,mBAAmB,UAAU,GAAG,uEAEhD;IAGjG,IAAI,cAAc,OAAO;IACzB,OAAO,cAAc,KAAK,UAAU,KAAK,iBAAiB,KACxD;IAGF,EAAE,YAAY,cAAc,GAAG,UAAU;;IAE3C;EAEF,IAAI,CAAC,EAAE,YAAY,EAAE;EAErB,OAAO;GACL,MAAM,EAAE,UAAU;GAClB,KAAK,EAAE,YAAY;IACjB,QAAQ;IACR,MAAM;IACN,gBAAgB;IACjB,CAAC;GACH;UACM,GAAG;EACV"}
package/dist/esm/index.js CHANGED
@@ -1,6 +1,2 @@
1
1
  import { defineDevtoolsConfig, devtools } from "./plugin.js";
2
- export {
3
- defineDevtoolsConfig,
4
- devtools
5
- };
6
- //# sourceMappingURL=index.js.map
2
+ export { defineDevtoolsConfig, devtools };
@@ -1,12 +1,13 @@
1
1
  import { PluginInjection } from '@tanstack/devtools-client';
2
- import { types as Babel } from '@babel/core';
3
- import { ParseResult } from '@babel/parser';
4
2
  /**
5
3
  * Finds the TanStackDevtools component name in the file
6
4
  * Handles renamed imports and namespace imports
7
5
  */
8
- export declare const findDevtoolsComponentName: (ast: ParseResult<Babel.File>) => string | null;
9
- export declare const transformAndInject: (ast: ParseResult<Babel.File>, injection: PluginInjection, devtoolsComponentName: string) => boolean;
6
+ export declare const findDevtoolsComponentName: (code: string) => string | null;
7
+ export declare const transformAndInject: (code: string, injection: PluginInjection, devtoolsComponentName: string) => {
8
+ code: string;
9
+ transformed: boolean;
10
+ } | null;
10
11
  /**
11
12
  * Detects if a file contains TanStack devtools import
12
13
  */