@mhjuma/traverse 0.3.0 → 0.4.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,118 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ import { traverseVitePlugin, TRAVERSE_OPTIMIZE_DEPS_INCLUDE } from './vite-plugin';
4
+
5
+ describe('traverseVitePlugin', () => {
6
+ it('returns a plugin with the expected metadata', () => {
7
+ const p = traverseVitePlugin();
8
+ expect(p.name).toBe('@mhjuma/traverse');
9
+ expect(p.enforce).toBe('pre');
10
+ });
11
+
12
+ it('config() returns optimizeDeps.include matching the exported constant', () => {
13
+ const p = traverseVitePlugin();
14
+ const cfg = p.config!();
15
+ expect(cfg?.optimizeDeps?.include).toEqual([...TRAVERSE_OPTIMIZE_DEPS_INCLUDE]);
16
+ });
17
+
18
+ it('config() returns a fresh array each call (not the frozen constant)', () => {
19
+ // Vite mutates the `include` array internally; returning the frozen
20
+ // singleton would throw later. Verify we hand back a writable copy.
21
+ const cfg = traverseVitePlugin().config!()!;
22
+ expect(() => { (cfg.optimizeDeps!.include as string[]).push('test'); }).not.toThrow();
23
+ });
24
+
25
+ describe('transform() — Bridge.NET CJS shim', () => {
26
+ const SHIM_PREFIX = 'var global=globalThis;var module={exports:{}};var exports=module.exports;\n';
27
+
28
+ it('prepends the shim to bridge.min.js inside @kusto/language-service', () => {
29
+ const p = traverseVitePlugin();
30
+ const id = '/somewhere/node_modules/@kusto/language-service/bridge.min.js';
31
+ const got = p.transform!('(function(n){n.Bridge=1})(this);', id);
32
+ expect(got).not.toBeNull();
33
+ expect(got!.code).toBe(SHIM_PREFIX + '(function(n){n.Bridge=1})(this);');
34
+ expect(got!.map).toBeNull();
35
+ });
36
+
37
+ it('prepends the shim to Kusto.JavaScript.Client.min.js', () => {
38
+ const p = traverseVitePlugin();
39
+ const id = '/n_m/@kusto/language-service/Kusto.JavaScript.Client.min.js';
40
+ const got = p.transform!('orig;', id);
41
+ expect(got!.code).toBe(SHIM_PREFIX + 'orig;');
42
+ });
43
+
44
+ it('prepends the shim to newtonsoft.json.min.js', () => {
45
+ const p = traverseVitePlugin();
46
+ const id = '/n_m/@kusto/language-service/newtonsoft.json.min.js';
47
+ const got = p.transform!('orig;', id);
48
+ expect(got!.code).toBe(SHIM_PREFIX + 'orig;');
49
+ });
50
+
51
+ it('prepends the shim to Kusto.Language.Bridge.min.js inside @kusto/language-service-next', () => {
52
+ const p = traverseVitePlugin();
53
+ const id = '/n_m/@kusto/language-service-next/Kusto.Language.Bridge.min.js';
54
+ const got = p.transform!('orig;', id);
55
+ expect(got!.code).toBe(SHIM_PREFIX + 'orig;');
56
+ });
57
+
58
+ it('matches paths with query-string suffixes (Vite ?import, ?worker_file)', () => {
59
+ const p = traverseVitePlugin();
60
+ const id = '/n_m/@kusto/language-service/bridge.min.js?worker_file&type=module';
61
+ const got = p.transform!('orig;', id);
62
+ expect(got).not.toBeNull();
63
+ expect(got!.code).toBe(SHIM_PREFIX + 'orig;');
64
+ });
65
+
66
+ it('matches paths with Windows-style backslashes', () => {
67
+ const p = traverseVitePlugin();
68
+ const id = 'C:\\app\\node_modules\\@kusto\\language-service\\bridge.min.js';
69
+ const got = p.transform!('orig;', id);
70
+ expect(got).not.toBeNull();
71
+ });
72
+
73
+ it('does NOT match unrelated files in @kusto', () => {
74
+ const p = traverseVitePlugin();
75
+ const id = '/n_m/@kusto/language-service/index.js';
76
+ expect(p.transform!('orig;', id)).toBeNull();
77
+ });
78
+
79
+ it('does NOT match arbitrary .min.js files outside @kusto', () => {
80
+ const p = traverseVitePlugin();
81
+ const id = '/n_m/other/bridge.min.js';
82
+ expect(p.transform!('orig;', id)).toBeNull();
83
+ });
84
+
85
+ it('does NOT match a file whose path superficially contains "bridge.min.js" but in wrong scope', () => {
86
+ const p = traverseVitePlugin();
87
+ const id = '/n_m/@kusto/other-package/bridge.min.js';
88
+ expect(p.transform!('orig;', id)).toBeNull();
89
+ });
90
+
91
+ it('does NOT touch normal JS files', () => {
92
+ const p = traverseVitePlugin();
93
+ expect(p.transform!('const x = 1;', '/src/foo.js')).toBeNull();
94
+ });
95
+ });
96
+
97
+ describe('TRAVERSE_OPTIMIZE_DEPS_INCLUDE', () => {
98
+ it('is frozen so consumers cannot accidentally mutate the canonical list', () => {
99
+ expect(Object.isFrozen(TRAVERSE_OPTIMIZE_DEPS_INCLUDE)).toBe(true);
100
+ });
101
+
102
+ it('includes both worker entries', () => {
103
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('monaco-editor/esm/vs/editor/editor.worker');
104
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto/release/esm/kusto.worker');
105
+ });
106
+
107
+ it('includes all four Bridge.NET subdeps in the parent > child form', () => {
108
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto > @kusto/language-service/bridge.min');
109
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto > @kusto/language-service/Kusto.JavaScript.Client.min');
110
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto > @kusto/language-service/newtonsoft.json.min');
111
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto > @kusto/language-service-next/Kusto.Language.Bridge.min');
112
+ });
113
+
114
+ it('includes xregexp (kusto plugin sub-dep with strict pnpm resolution)', () => {
115
+ expect(TRAVERSE_OPTIMIZE_DEPS_INCLUDE).toContain('@kusto/monaco-kusto > xregexp');
116
+ });
117
+ });
118
+ });
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Vite plugin that wires up `@mhjuma/traverse`'s build-time requirements:
3
+ *
4
+ * 1. **Bridge.NET CJS shim** — `@kusto/monaco-kusto`'s worker entry side-effect
5
+ * imports four Bridge.NET IIFE scripts (`bridge.min.js`,
6
+ * `Kusto.JavaScript.Client.min.js`, `newtonsoft.json.min.js`,
7
+ * `Kusto.Language.Bridge.min.js`). These reference Node-style globals
8
+ * (`module`, `exports`, `global`) via a CJS escape hatch
9
+ * (`typeof module !== "undefined" && module.exports && (n = global)`).
10
+ * Without those globals defined, the IIFE's other branch fires
11
+ * (`(function(n) { n.Bridge = Bridge; })(this)`) where `this` is
12
+ * `undefined` in strict ESM context, throwing `TypeError: Cannot set
13
+ * properties of undefined`. The shim prepends a 1-line CJS context so
14
+ * the escape hatch fires and routes globals to `globalThis`.
15
+ *
16
+ * 2. **`optimizeDeps.include` for worker entries** — Vite's dep scanner
17
+ * only walks the main-thread import graph in dev. Worker `import`
18
+ * chains are NOT followed, so the kusto worker's CJS sub-dependencies
19
+ * (Bridge.NET scripts, xregexp) never get pre-bundled into proper ESM
20
+ * unless we list them explicitly. The `parent > child` form is required
21
+ * here because pnpm's strict resolution keeps these transitive deps out
22
+ * of the project root.
23
+ *
24
+ * **Not handled here**: `Buffer` / `process` Node globals. The kusto
25
+ * worker's deeper code paths reference these and there's no good way to
26
+ * polyfill them from a plugin without taking on
27
+ * `vite-plugin-node-polyfills` as a peer dependency. Consumers must add
28
+ * `nodePolyfills({ overrides: { fs: null } })` themselves alongside this
29
+ * plugin. See `docs/guide/quick-start.md` for the full Vite setup.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * // vite.config.ts
34
+ * import { defineConfig } from 'vite';
35
+ * import { nodePolyfills } from 'vite-plugin-node-polyfills';
36
+ * import { traverseVitePlugin } from '@mhjuma/traverse/vite-plugin';
37
+ *
38
+ * export default defineConfig({
39
+ * plugins: [
40
+ * // ... your own plugins ...
41
+ * nodePolyfills({ overrides: { fs: null } }),
42
+ * traverseVitePlugin(),
43
+ * ],
44
+ * });
45
+ * ```
46
+ */
47
+
48
+ // Use a structural `Plugin` shape rather than `import type { Plugin } from 'vite'`
49
+ // so this module typechecks for consumers that haven't installed `vite` as a
50
+ // devDependency. The cost is a tiny duplicated type; the win is we don't
51
+ // require `vite` as a peerDep that would force webpack/Rollup-only consumers
52
+ // to install it for no reason.
53
+ interface ViteLikePlugin {
54
+ name: string;
55
+ enforce?: 'pre' | 'post';
56
+ config?: () => { optimizeDeps?: { include?: readonly string[] } } | undefined;
57
+ transform?: (code: string, id: string) => { code: string; map: null } | null;
58
+ }
59
+
60
+ /**
61
+ * Pattern matching the four Bridge.NET IIFE scripts inside
62
+ * `@kusto/language-service{,-next}` that need the CJS context shim.
63
+ *
64
+ * Matches both posix and Windows path separators. The trailing `(\?|$)` lets
65
+ * us match either the file path itself or one with a query-string suffix
66
+ * (Vite uses `?import`, `?worker_file`, etc).
67
+ */
68
+ const BRIDGE_NET_FILE_PATTERN =
69
+ /[\\/]@kusto[\\/](language-service|language-service-next)[\\/](bridge\.min|Kusto\.JavaScript\.Client\.min|newtonsoft\.json\.min|Kusto\.Language\.Bridge\.min)\.js(\?|$)/;
70
+
71
+ /** The shim prepended to each Bridge.NET IIFE. Kept as a constant so consumers
72
+ * reading the source can see exactly what we inject. */
73
+ const BRIDGE_NET_SHIM =
74
+ 'var global=globalThis;var module={exports:{}};var exports=module.exports;\n';
75
+
76
+ /**
77
+ * Worker entry points and their CJS sub-dependencies that Vite's dep
78
+ * scanner won't discover on its own. Kept as a separate exported constant so
79
+ * tests can assert on it without re-implementing the wiring, and so callers
80
+ * who construct their own plugin can reuse it.
81
+ */
82
+ export const TRAVERSE_OPTIMIZE_DEPS_INCLUDE: readonly string[] = Object.freeze([
83
+ 'monaco-editor/esm/vs/editor/editor.worker',
84
+ '@kusto/monaco-kusto/release/esm/kusto.worker',
85
+ '@kusto/monaco-kusto > xregexp',
86
+ '@kusto/monaco-kusto > @kusto/language-service/bridge.min',
87
+ '@kusto/monaco-kusto > @kusto/language-service/Kusto.JavaScript.Client.min',
88
+ '@kusto/monaco-kusto > @kusto/language-service/newtonsoft.json.min',
89
+ '@kusto/monaco-kusto > @kusto/language-service-next/Kusto.Language.Bridge.min',
90
+ ]);
91
+
92
+ /**
93
+ * Construct the Vite plugin. Idempotent across multiple calls (each call
94
+ * returns a fresh plugin object — Vite deduplicates by `name`).
95
+ */
96
+ export function traverseVitePlugin(): ViteLikePlugin {
97
+ return {
98
+ name: '@mhjuma/traverse',
99
+ enforce: 'pre',
100
+ config() {
101
+ return {
102
+ optimizeDeps: {
103
+ include: [...TRAVERSE_OPTIMIZE_DEPS_INCLUDE],
104
+ },
105
+ };
106
+ },
107
+ transform(code, id) {
108
+ if (!BRIDGE_NET_FILE_PATTERN.test(id)) {
109
+ return null;
110
+ }
111
+ return { code: BRIDGE_NET_SHIM + code, map: null };
112
+ },
113
+ };
114
+ }