@widgetic/dev-tools 0.1.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,120 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { existsSync } from 'node:fs';
5
+ import { join } from 'node:path';
6
+ import {
7
+ buildSiblingAliases,
8
+ devOptimizeExclude,
9
+ devSsrNoExternal,
10
+ siblingPackageRoots,
11
+ SIBLING_PACKAGES
12
+ } from '../lib/frontend-layout.mjs';
13
+
14
+ const FRONTEND = fileURLToPath(new URL('../..', import.meta.url));
15
+
16
+ test('the design-system table expands to every subpath plus the index', () => {
17
+ const aliases = buildSiblingAliases(FRONTEND, { packages: ['design-system'] });
18
+ const keys = Object.keys(aliases);
19
+ assert.deepEqual(
20
+ keys.sort(),
21
+ [
22
+ '@widgetic/design-system',
23
+ '@widgetic/design-system/components',
24
+ '@widgetic/design-system/fonts',
25
+ '@widgetic/design-system/icons',
26
+ '@widgetic/design-system/previews',
27
+ '@widgetic/design-system/shad-editor',
28
+ '@widgetic/design-system/stores',
29
+ '@widgetic/design-system/styles'
30
+ ].sort()
31
+ );
32
+ assert.equal(aliases['@widgetic/design-system/icons'], `${FRONTEND}/design-system/src/lib/icons`);
33
+ // styles is a file, not a directory — the one entry that ever differed
34
+ assert.equal(
35
+ aliases['@widgetic/design-system/styles'],
36
+ `${FRONTEND}/design-system/src/lib/styles/global.css`
37
+ );
38
+ assert.equal(aliases['@widgetic/design-system'], `${FRONTEND}/design-system/src/lib/index.ts`);
39
+ });
40
+
41
+ test('entry packages alias to their entry file, lib packages to their tree', () => {
42
+ const aliases = buildSiblingAliases(FRONTEND, {
43
+ packages: ['api-sdk', 'cache-layer', 'chat', 'file-browser']
44
+ });
45
+ assert.equal(aliases['@widgetic/api-sdk'], `${FRONTEND}/api-sdk/src/index.ts`);
46
+ assert.equal(aliases['@widgetic/cache-layer'], `${FRONTEND}/cache-layer/src/index.ts`);
47
+ assert.equal(aliases['@widgetic/chat'], `${FRONTEND}/chat/src/lib`);
48
+ assert.equal(aliases['@widgetic/file-browser'], `${FRONTEND}/file-browser/src/lib`);
49
+ });
50
+
51
+ test('a selection without design-system emits no design-system aliases', () => {
52
+ const aliases = buildSiblingAliases(FRONTEND, { packages: ['api-sdk', 'canvas'] });
53
+ assert.deepEqual(Object.keys(aliases), ['@widgetic/api-sdk', '@widgetic/canvas']);
54
+ });
55
+
56
+ test('design-system can be narrowed to specific subpaths or lose its index', () => {
57
+ const narrowed = buildSiblingAliases(FRONTEND, {
58
+ packages: ['design-system'],
59
+ designSystemSubpaths: ['styles', 'icons']
60
+ });
61
+ assert.deepEqual(Object.keys(narrowed).sort(), [
62
+ '@widgetic/design-system',
63
+ '@widgetic/design-system/icons',
64
+ '@widgetic/design-system/styles'
65
+ ]);
66
+ const noIndex = buildSiblingAliases(FRONTEND, {
67
+ packages: ['design-system'],
68
+ designSystemIndex: false
69
+ });
70
+ assert.ok(!('@widgetic/design-system' in noIndex));
71
+ });
72
+
73
+ test('an unknown package or subpath throws with where-to-fix guidance', () => {
74
+ assert.throws(
75
+ () => buildSiblingAliases(FRONTEND, { packages: ['props-editor'] }),
76
+ /SIBLING_PACKAGES.*frontend-layout\.mjs/
77
+ );
78
+ assert.throws(
79
+ () =>
80
+ buildSiblingAliases(FRONTEND, {
81
+ packages: ['design-system'],
82
+ designSystemSubpaths: ['widgets']
83
+ }),
84
+ /not a declared design-system subpath/
85
+ );
86
+ assert.throws(() => siblingPackageRoots(FRONTEND, ['nope']), /SIBLING_PACKAGES/);
87
+ });
88
+
89
+ test('siblingPackageRoots resolves checkout root plus lib/entry', () => {
90
+ const roots = siblingPackageRoots(FRONTEND, ['editor', 'api-sdk']);
91
+ assert.equal(roots.get('editor')?.root, `${FRONTEND}/editor`);
92
+ assert.equal(roots.get('editor')?.lib, `${FRONTEND}/editor/src/lib`);
93
+ assert.equal(roots.get('api-sdk')?.entry, `${FRONTEND}/api-sdk/src/index.ts`);
94
+ });
95
+
96
+ test('the exclude and noExternal lists mirror the selection', () => {
97
+ const packages = ['api-sdk', 'design-system', 'cache-layer'];
98
+ assert.deepEqual(devOptimizeExclude(packages), [
99
+ '@widgetic/api-sdk',
100
+ '@widgetic/design-system',
101
+ '@widgetic/cache-layer'
102
+ ]);
103
+ assert.deepEqual(devSsrNoExternal(packages), devOptimizeExclude(packages));
104
+ });
105
+
106
+ /**
107
+ * The layout truth on disk: every declared directory must exist, and the
108
+ * renamed-away ones must not come back. This is the pin that keeps a future
109
+ * sibling move a one-file change here instead of a silent five-repo drift.
110
+ */
111
+ test('every declared checkout directory exists under Frontend/', () => {
112
+ for (const pkg of Object.values(SIBLING_PACKAGES)) {
113
+ assert.ok(existsSync(join(FRONTEND, pkg.dir)), `missing checkout: ${pkg.dir}`);
114
+ }
115
+ });
116
+
117
+ test('the renamed-away checkouts stay gone', () => {
118
+ assert.ok(!existsSync(join(FRONTEND, 'props-editor')));
119
+ assert.ok(!existsSync(join(FRONTEND, 'prompt-chat')));
120
+ });
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The `$lib` remap decides where a sibling package's imports resolve while its
4
+ * source is compiled inside a host app — and it does so by looking at the
5
+ * filesystem, returning a path only when the file exists. These tests pin the
6
+ * decisions a refactor would quietly invert:
7
+ *
8
+ * - the per-importer scoping (the host's own `$lib` must stay kit's);
9
+ * - the unwrap of a specifier kit has already rewritten under the host's
10
+ * `src/lib` (`ownLibMarker`);
11
+ * - the extension ladder that absorbs shadcn's `.js`-for-`.ts` imports;
12
+ * - and the missing-module contract: `null`, never a guess, so a genuinely
13
+ * absent file still fails loudly against the right tree.
14
+ *
15
+ * Fixtures are real directories in the OS temp dir — the plugin's contract is
16
+ * `existsSync`, so testing it against fakes would prove nothing.
17
+ *
18
+ * Runs on the built-in test runner — no framework, no dependencies:
19
+ * npm run test:vite
20
+ */
21
+
22
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
23
+ import { tmpdir } from "node:os";
24
+ import { join } from "node:path";
25
+ import { after, describe, it } from "node:test";
26
+ import assert from "node:assert/strict";
27
+
28
+ import { createSiblingSrcLibPlugin } from "../lib/vite-src-lib.mjs";
29
+
30
+ /** Every fixture this run created, so a failure cannot leave one behind. */
31
+ const created = [];
32
+
33
+ after(() => {
34
+ for (const root of created) rmSync(root, { recursive: true, force: true });
35
+ });
36
+
37
+ /** A sibling src/lib tree with the given files. */
38
+ function fixture(files) {
39
+ const root = mkdtempSync(join(tmpdir(), "vite-src-lib-"));
40
+ created.push(root);
41
+ const libRoot = join(root, "sibling", "src", "lib");
42
+ for (const [file, contents] of Object.entries(files)) {
43
+ const full = join(libRoot, file);
44
+ mkdirSync(join(full, ".."), { recursive: true });
45
+ writeFileSync(full, contents);
46
+ }
47
+ return { root, libRoot };
48
+ }
49
+
50
+ function plugin(options) {
51
+ return createSiblingSrcLibPlugin({
52
+ name: "test-remap",
53
+ importerFragments: ["/sibling/src/"],
54
+ libRoots: [options.libRoot],
55
+ ...options,
56
+ });
57
+ }
58
+
59
+ describe("a $lib import from a sibling source file", () => {
60
+ it("resolves against that sibling’s src/lib", () => {
61
+ const { libRoot } = fixture({ "utils.ts": "export const one = 1;\n" });
62
+ const remap = plugin({ libRoot });
63
+
64
+ assert.equal(
65
+ remap.resolveId("$lib/utils.js", join(libRoot, "App.svelte")),
66
+ join(libRoot, "utils.ts"),
67
+ );
68
+ });
69
+
70
+ it("resolves a .svelte file directly", () => {
71
+ const { libRoot } = fixture({ "components/Card.svelte": "<span />\n" });
72
+ const remap = plugin({ libRoot });
73
+
74
+ assert.equal(
75
+ remap.resolveId(
76
+ "$lib/components/Card.svelte",
77
+ join(libRoot, "routes/Page.svelte"),
78
+ ),
79
+ join(libRoot, "components", "Card.svelte"),
80
+ );
81
+ });
82
+
83
+ it("resolves a bare $lib to the package’s index", () => {
84
+ const { libRoot } = fixture({ "index.ts": "export {};\n" });
85
+ const remap = plugin({ libRoot });
86
+
87
+ assert.equal(
88
+ remap.resolveId("$lib", join(libRoot, "App.svelte")),
89
+ join(libRoot, "index.ts"),
90
+ );
91
+ });
92
+ });
93
+
94
+ describe("the specifier kit has already rewritten", () => {
95
+ it("is unwrapped from the host’s src/lib back into the sibling’s tree", () => {
96
+ const { libRoot } = fixture({ "utils.ts": "export const one = 1;\n" });
97
+ const remap = plugin({ libRoot, ownLibMarker: "/app/src/lib/" });
98
+
99
+ assert.equal(
100
+ remap.resolveId("/app/src/lib/utils.js", join(libRoot, "App.svelte")),
101
+ join(libRoot, "utils.ts"),
102
+ );
103
+ });
104
+ });
105
+
106
+ describe("what the remap must not touch", () => {
107
+ it("leaves the host app’s own $lib alone", () => {
108
+ const { libRoot } = fixture({ "utils.ts": "" });
109
+ const remap = plugin({ libRoot });
110
+
111
+ assert.equal(
112
+ remap.resolveId("$lib/utils.js", "/app/src/lib/main.ts"),
113
+ null,
114
+ );
115
+ });
116
+
117
+ it("leaves bare package specifiers alone", () => {
118
+ const { libRoot } = fixture({ "utils.ts": "" });
119
+ const remap = plugin({ libRoot });
120
+
121
+ assert.equal(remap.resolveId("svelte", join(libRoot, "App.svelte")), null);
122
+ assert.equal(
123
+ remap.resolveId("@widgetic/design-system", join(libRoot, "App.svelte")),
124
+ null,
125
+ );
126
+ });
127
+
128
+ it("ignores an importer outside every registered tree", () => {
129
+ const { libRoot } = fixture({ "utils.ts": "" });
130
+ const remap = plugin({ libRoot });
131
+
132
+ assert.equal(
133
+ remap.resolveId("$lib/utils.js", "/somewhere/else/App.svelte"),
134
+ null,
135
+ );
136
+ assert.equal(remap.resolveId("$lib/utils.js"), null);
137
+ });
138
+ });
139
+
140
+ describe("the file-exists contract", () => {
141
+ it("returns null for a genuinely missing module", () => {
142
+ const { libRoot } = fixture({ "utils.ts": "" });
143
+ const remap = plugin({ libRoot });
144
+
145
+ assert.equal(
146
+ remap.resolveId("$lib/nope.ts", join(libRoot, "App.svelte")),
147
+ null,
148
+ );
149
+ });
150
+
151
+ it("prefers the literal .js the import names when it exists", () => {
152
+ // The exact name wins; the .ts fallback exists for shadcn imports that
153
+ // name a .js which is not there — it does not override a real file.
154
+ const { libRoot } = fixture({ "y.ts": "", "y.js": "" });
155
+ const remap = plugin({ libRoot });
156
+
157
+ assert.equal(
158
+ remap.resolveId("$lib/y.js", join(libRoot, "App.svelte")),
159
+ join(libRoot, "y.js"),
160
+ );
161
+ });
162
+
163
+ it("falls back to .ts when the named .js does not exist", () => {
164
+ const { libRoot } = fixture({ "x.js": "" });
165
+ const remap = plugin({ libRoot });
166
+
167
+ assert.equal(
168
+ remap.resolveId("$lib/x.js", join(libRoot, "App.svelte")),
169
+ join(libRoot, "x.js"),
170
+ );
171
+ });
172
+
173
+ it("preserves the query string", () => {
174
+ const { libRoot } = fixture({ "a.png": "" });
175
+ const remap = plugin({ libRoot });
176
+
177
+ assert.equal(
178
+ remap.resolveId("$lib/a.png?inline", join(libRoot, "App.svelte")),
179
+ join(libRoot, "a.png?inline"),
180
+ );
181
+ });
182
+ });
183
+
184
+ describe("construction", () => {
185
+ it("refuses fragments and libRoots that do not pair up", () => {
186
+ assert.throws(
187
+ () =>
188
+ createSiblingSrcLibPlugin({
189
+ importerFragments: ["/a/src/", "/b/src/"],
190
+ libRoots: ["/a/lib"],
191
+ }),
192
+ /one-to-one/,
193
+ );
194
+ });
195
+
196
+ it("picks the libRoot whose fragment matches the importer", () => {
197
+ const a = fixture({ "onlyInA.ts": "" });
198
+ const b = fixture({ "onlyInB.ts": "" });
199
+ const remap = createSiblingSrcLibPlugin({
200
+ importerFragments: ["/sibling-a/src/", "/sibling-b/src/"],
201
+ libRoots: [a.libRoot, b.libRoot],
202
+ ownLibMarker: "/app/src/lib/",
203
+ });
204
+ // The fixtures differ only by directory name; name the fragments to match.
205
+ const remapA = createSiblingSrcLibPlugin({
206
+ importerFragments: [join(a.root, "sibling/src/")],
207
+ libRoots: [a.libRoot],
208
+ });
209
+ const remapB = createSiblingSrcLibPlugin({
210
+ importerFragments: [join(b.root, "sibling/src/")],
211
+ libRoots: [b.libRoot],
212
+ });
213
+
214
+ assert.equal(
215
+ remapA.resolveId("$lib/onlyInA.ts", join(a.libRoot, "App.svelte")),
216
+ join(a.libRoot, "onlyInA.ts"),
217
+ );
218
+ assert.equal(
219
+ remapB.resolveId("$lib/onlyInB.ts", join(b.libRoot, "App.svelte")),
220
+ join(b.libRoot, "onlyInB.ts"),
221
+ );
222
+ assert.equal(
223
+ remap.resolveId("$lib/onlyInA.ts", "/b/src/lib/App.svelte"),
224
+ null,
225
+ );
226
+ });
227
+ });