@mhjuma/traverse 0.3.0 → 0.4.1

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,126 @@
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
+ * **Production builds**: Vite only applies the main `plugins` array to workers
32
+ * in dev. For a production build you MUST re-apply both this plugin and
33
+ * `nodePolyfills` via `worker.plugins`, or the kusto worker ships without the
34
+ * shim/polyfills and crashes at runtime with `ReferenceError: global is not
35
+ * defined` (breaking KQL highlighting + autocomplete in prod only).
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * // vite.config.ts
40
+ * import { defineConfig } from 'vite';
41
+ * import { nodePolyfills } from 'vite-plugin-node-polyfills';
42
+ * import { traverseVitePlugin } from '@mhjuma/traverse/vite-plugin';
43
+ *
44
+ * const traversePlugins = () => [
45
+ * nodePolyfills({ overrides: { fs: null } }),
46
+ * traverseVitePlugin(),
47
+ * ];
48
+ *
49
+ * export default defineConfig({
50
+ * plugins: [
51
+ * // ... your own plugins ...
52
+ * ...traversePlugins(),
53
+ * ],
54
+ * // Required for production builds — see note above.
55
+ * worker: { plugins: traversePlugins },
56
+ * });
57
+ * ```
58
+ */
59
+
60
+ // Use a structural `Plugin` shape rather than `import type { Plugin } from 'vite'`
61
+ // so this module typechecks for consumers that haven't installed `vite` as a
62
+ // devDependency. The cost is a tiny duplicated type; the win is we don't
63
+ // require `vite` as a peerDep that would force webpack/Rollup-only consumers
64
+ // to install it for no reason.
65
+ interface ViteLikePlugin {
66
+ name: string;
67
+ enforce?: 'pre' | 'post';
68
+ config?: () => { optimizeDeps?: { include?: readonly string[] } } | undefined;
69
+ transform?: (code: string, id: string) => { code: string; map: null } | null;
70
+ }
71
+
72
+ /**
73
+ * Pattern matching the four Bridge.NET IIFE scripts inside
74
+ * `@kusto/language-service{,-next}` that need the CJS context shim.
75
+ *
76
+ * Matches both posix and Windows path separators. The trailing `(\?|$)` lets
77
+ * us match either the file path itself or one with a query-string suffix
78
+ * (Vite uses `?import`, `?worker_file`, etc).
79
+ */
80
+ const BRIDGE_NET_FILE_PATTERN =
81
+ /[\\/]@kusto[\\/](language-service|language-service-next)[\\/](bridge\.min|Kusto\.JavaScript\.Client\.min|newtonsoft\.json\.min|Kusto\.Language\.Bridge\.min)\.js(\?|$)/;
82
+
83
+ /** The shim prepended to each Bridge.NET IIFE. Kept as a constant so consumers
84
+ * reading the source can see exactly what we inject. */
85
+ const BRIDGE_NET_SHIM =
86
+ 'var global=globalThis;var module={exports:{}};var exports=module.exports;\n';
87
+
88
+ /**
89
+ * Worker entry points and their CJS sub-dependencies that Vite's dep
90
+ * scanner won't discover on its own. Kept as a separate exported constant so
91
+ * tests can assert on it without re-implementing the wiring, and so callers
92
+ * who construct their own plugin can reuse it.
93
+ */
94
+ export const TRAVERSE_OPTIMIZE_DEPS_INCLUDE: readonly string[] = Object.freeze([
95
+ 'monaco-editor/esm/vs/editor/editor.worker',
96
+ '@kusto/monaco-kusto/release/esm/kusto.worker',
97
+ '@kusto/monaco-kusto > xregexp',
98
+ '@kusto/monaco-kusto > @kusto/language-service/bridge.min',
99
+ '@kusto/monaco-kusto > @kusto/language-service/Kusto.JavaScript.Client.min',
100
+ '@kusto/monaco-kusto > @kusto/language-service/newtonsoft.json.min',
101
+ '@kusto/monaco-kusto > @kusto/language-service-next/Kusto.Language.Bridge.min',
102
+ ]);
103
+
104
+ /**
105
+ * Construct the Vite plugin. Idempotent across multiple calls (each call
106
+ * returns a fresh plugin object — Vite deduplicates by `name`).
107
+ */
108
+ export function traverseVitePlugin(): ViteLikePlugin {
109
+ return {
110
+ name: '@mhjuma/traverse',
111
+ enforce: 'pre',
112
+ config() {
113
+ return {
114
+ optimizeDeps: {
115
+ include: [...TRAVERSE_OPTIMIZE_DEPS_INCLUDE],
116
+ },
117
+ };
118
+ },
119
+ transform(code, id) {
120
+ if (!BRIDGE_NET_FILE_PATTERN.test(id)) {
121
+ return null;
122
+ }
123
+ return { code: BRIDGE_NET_SHIM + code, map: null };
124
+ },
125
+ };
126
+ }