funkscript 0.2.6 → 0.2.7

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 (171) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/build-watch.d.ts.map +1 -1
  3. package/dist/cli/build-watch.js +4 -24
  4. package/dist/cli/build-watch.js.map +1 -1
  5. package/dist/cli/build.d.ts.map +1 -1
  6. package/dist/cli/build.js +3 -58
  7. package/dist/cli/build.js.map +1 -1
  8. package/dist/cli/check-policy.d.ts +9 -0
  9. package/dist/cli/check-policy.d.ts.map +1 -0
  10. package/dist/cli/check-policy.js +39 -0
  11. package/dist/cli/check-policy.js.map +1 -0
  12. package/dist/cli/check.d.ts.map +1 -1
  13. package/dist/cli/check.js +16 -30
  14. package/dist/cli/check.js.map +1 -1
  15. package/dist/cli/commands.d.ts.map +1 -1
  16. package/dist/cli/commands.js +11 -0
  17. package/dist/cli/commands.js.map +1 -1
  18. package/dist/cli/doctor.d.ts.map +1 -1
  19. package/dist/cli/doctor.js +2 -6
  20. package/dist/cli/doctor.js.map +1 -1
  21. package/dist/cli/index.js +3 -0
  22. package/dist/cli/index.js.map +1 -1
  23. package/dist/cli/query.d.ts +3 -0
  24. package/dist/cli/query.d.ts.map +1 -0
  25. package/dist/cli/query.js +152 -0
  26. package/dist/cli/query.js.map +1 -0
  27. package/dist/cli/run.d.ts.map +1 -1
  28. package/dist/cli/run.js +2 -29
  29. package/dist/cli/run.js.map +1 -1
  30. package/dist/cli/tsc.d.ts.map +1 -1
  31. package/dist/cli/tsc.js +2 -6
  32. package/dist/cli/tsc.js.map +1 -1
  33. package/dist/compiler/check.d.ts.map +1 -1
  34. package/dist/compiler/check.js +18 -28
  35. package/dist/compiler/check.js.map +1 -1
  36. package/dist/compiler/diagnostics-json.d.ts +73 -0
  37. package/dist/compiler/diagnostics-json.d.ts.map +1 -0
  38. package/dist/compiler/diagnostics-json.js +957 -0
  39. package/dist/compiler/diagnostics-json.js.map +1 -0
  40. package/dist/compiler/escape-audit.d.ts +46 -0
  41. package/dist/compiler/escape-audit.d.ts.map +1 -0
  42. package/dist/compiler/escape-audit.js +682 -0
  43. package/dist/compiler/escape-audit.js.map +1 -0
  44. package/dist/compiler/modules/output-plan.d.ts.map +1 -1
  45. package/dist/compiler/modules/output-plan.js +10 -5
  46. package/dist/compiler/modules/output-plan.js.map +1 -1
  47. package/dist/compiler/modules/source-files.d.ts +2 -0
  48. package/dist/compiler/modules/source-files.d.ts.map +1 -1
  49. package/dist/compiler/modules/source-files.js +8 -0
  50. package/dist/compiler/modules/source-files.js.map +1 -1
  51. package/dist/compiler/numeric-comparisons.d.ts.map +1 -1
  52. package/dist/compiler/numeric-comparisons.js +13 -7
  53. package/dist/compiler/numeric-comparisons.js.map +1 -1
  54. package/dist/compiler/pipelines/resolution.d.ts.map +1 -1
  55. package/dist/compiler/pipelines/resolution.js +24 -23
  56. package/dist/compiler/pipelines/resolution.js.map +1 -1
  57. package/dist/compiler/query.d.ts +53 -0
  58. package/dist/compiler/query.d.ts.map +1 -0
  59. package/dist/compiler/query.js +781 -0
  60. package/dist/compiler/query.js.map +1 -0
  61. package/dist/compiler/source-map.d.ts.map +1 -1
  62. package/dist/compiler/source-map.js +2 -7
  63. package/dist/compiler/source-map.js.map +1 -1
  64. package/dist/compiler/type-view-paths.d.ts +2 -0
  65. package/dist/compiler/type-view-paths.d.ts.map +1 -1
  66. package/dist/compiler/type-view-paths.js +8 -0
  67. package/dist/compiler/type-view-paths.js.map +1 -1
  68. package/dist/compiler/typescript-context.d.ts.map +1 -1
  69. package/dist/compiler/typescript-context.js +5 -7
  70. package/dist/compiler/typescript-context.js.map +1 -1
  71. package/dist/emitter/assemblyscript.d.ts.map +1 -1
  72. package/dist/emitter/assemblyscript.js +120 -136
  73. package/dist/emitter/assemblyscript.js.map +1 -1
  74. package/dist/formatter/index.d.ts +9 -0
  75. package/dist/formatter/index.d.ts.map +1 -1
  76. package/dist/formatter/index.js +158 -2
  77. package/dist/formatter/index.js.map +1 -1
  78. package/dist/index.d.ts +3 -0
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/index.js +3 -0
  81. package/dist/index.js.map +1 -1
  82. package/dist/lsp/completion.d.ts +25 -0
  83. package/dist/lsp/completion.d.ts.map +1 -1
  84. package/dist/lsp/completion.js +1114 -51
  85. package/dist/lsp/completion.js.map +1 -1
  86. package/dist/lsp/definition.d.ts +2 -0
  87. package/dist/lsp/definition.d.ts.map +1 -1
  88. package/dist/lsp/definition.js +52 -58
  89. package/dist/lsp/definition.js.map +1 -1
  90. package/dist/lsp/hover.d.ts +5 -1
  91. package/dist/lsp/hover.d.ts.map +1 -1
  92. package/dist/lsp/hover.js +1000 -41
  93. package/dist/lsp/hover.js.map +1 -1
  94. package/dist/lsp/imports.d.ts +2 -0
  95. package/dist/lsp/imports.d.ts.map +1 -1
  96. package/dist/lsp/imports.js +35 -20
  97. package/dist/lsp/imports.js.map +1 -1
  98. package/dist/lsp/navigation.d.ts +1 -1
  99. package/dist/lsp/navigation.d.ts.map +1 -1
  100. package/dist/lsp/navigation.js +30 -12
  101. package/dist/lsp/navigation.js.map +1 -1
  102. package/dist/lsp/range-format.d.ts +4 -0
  103. package/dist/lsp/range-format.d.ts.map +1 -0
  104. package/dist/lsp/range-format.js +88 -0
  105. package/dist/lsp/range-format.js.map +1 -0
  106. package/dist/lsp/schema-import-hover.d.ts +8 -0
  107. package/dist/lsp/schema-import-hover.d.ts.map +1 -0
  108. package/dist/lsp/schema-import-hover.js +52 -0
  109. package/dist/lsp/schema-import-hover.js.map +1 -0
  110. package/dist/lsp/server.d.ts +1 -0
  111. package/dist/lsp/server.d.ts.map +1 -1
  112. package/dist/lsp/server.js +53 -48
  113. package/dist/lsp/server.js.map +1 -1
  114. package/dist/lsp/workspace.d.ts +3 -1
  115. package/dist/lsp/workspace.d.ts.map +1 -1
  116. package/dist/lsp/workspace.js +66 -10
  117. package/dist/lsp/workspace.js.map +1 -1
  118. package/dist/plugins/bun.d.ts +2 -18
  119. package/dist/plugins/bun.d.ts.map +1 -1
  120. package/dist/plugins/bun.js +5 -30
  121. package/dist/plugins/bun.js.map +1 -1
  122. package/dist/plugins/esbuild.d.ts +1 -1
  123. package/dist/plugins/esbuild.d.ts.map +1 -1
  124. package/dist/plugins/esbuild.js +10 -36
  125. package/dist/plugins/esbuild.js.map +1 -1
  126. package/dist/plugins/server-boundary.d.ts +14 -0
  127. package/dist/plugins/server-boundary.d.ts.map +1 -0
  128. package/dist/plugins/server-boundary.js +25 -0
  129. package/dist/plugins/server-boundary.js.map +1 -0
  130. package/dist/plugins/vite.d.ts +2 -18
  131. package/dist/plugins/vite.d.ts.map +1 -1
  132. package/dist/plugins/vite.js +4 -29
  133. package/dist/plugins/vite.js.map +1 -1
  134. package/package.json +1 -1
  135. package/src/cli/build-watch.ts +4 -26
  136. package/src/cli/build.ts +9 -60
  137. package/src/cli/check-policy.ts +54 -0
  138. package/src/cli/check.ts +19 -31
  139. package/src/cli/commands.ts +13 -0
  140. package/src/cli/doctor.ts +2 -5
  141. package/src/cli/index.ts +3 -0
  142. package/src/cli/query.ts +191 -0
  143. package/src/cli/run.ts +5 -30
  144. package/src/cli/tsc.ts +2 -5
  145. package/src/compiler/check.ts +28 -48
  146. package/src/compiler/diagnostics-json.ts +1205 -0
  147. package/src/compiler/escape-audit.ts +913 -0
  148. package/src/compiler/modules/output-plan.ts +11 -9
  149. package/src/compiler/modules/source-files.ts +9 -0
  150. package/src/compiler/numeric-comparisons.ts +14 -8
  151. package/src/compiler/pipelines/resolution.ts +30 -26
  152. package/src/compiler/query.ts +1177 -0
  153. package/src/compiler/source-map.ts +3 -7
  154. package/src/compiler/type-view-paths.ts +7 -0
  155. package/src/compiler/typescript-context.ts +6 -7
  156. package/src/emitter/assemblyscript.ts +101 -147
  157. package/src/formatter/index.ts +229 -2
  158. package/src/index.ts +3 -0
  159. package/src/lsp/completion.ts +1452 -66
  160. package/src/lsp/definition.ts +50 -58
  161. package/src/lsp/hover.ts +1442 -40
  162. package/src/lsp/imports.ts +55 -21
  163. package/src/lsp/navigation.ts +41 -22
  164. package/src/lsp/range-format.ts +102 -0
  165. package/src/lsp/schema-import-hover.ts +76 -0
  166. package/src/lsp/server.ts +69 -69
  167. package/src/lsp/workspace.ts +63 -9
  168. package/src/plugins/bun.ts +14 -66
  169. package/src/plugins/esbuild.ts +16 -55
  170. package/src/plugins/server-boundary.ts +41 -0
  171. package/src/plugins/vite.ts +12 -62
@@ -1,6 +1,6 @@
1
- import { readdirSync, readFileSync } from "node:fs";
2
- import { join } from "node:path";
3
- import { pathToFileURL } from "node:url";
1
+ import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
2
+ import { isAbsolute, join, relative, sep } from "node:path";
3
+ import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import type { TextDocumentItem } from "vscode-languageserver/node";
5
5
  import { isFileNotFoundError } from "../errors.js";
6
6
 
@@ -9,7 +9,12 @@ export function isExpectedFsAccessError(error: unknown): boolean {
9
9
  if (typeof error !== "object" || error === null || !("code" in error))
10
10
  return false;
11
11
  const code = (error as { code?: unknown }).code;
12
- return code === "EACCES" || code === "EPERM" || code === "ENOSYS";
12
+ return (
13
+ code === "EACCES" ||
14
+ code === "EPERM" ||
15
+ code === "ENOSYS" ||
16
+ code === "ELOOP"
17
+ );
13
18
  }
14
19
 
15
20
  /** Read a fresh workspace view; unsaved open buffers always win over disk. */
@@ -17,6 +22,7 @@ export function workspaceSnapshot(
17
22
  roots: string[],
18
23
  documents: Iterable<TextDocumentItem>,
19
24
  interopDocuments: Iterable<TextDocumentItem>,
25
+ options: { allowIncomplete?: boolean } = {},
20
26
  ): {
21
27
  fun: TextDocumentItem[];
22
28
  interop: TextDocumentItem[];
@@ -24,6 +30,7 @@ export function workspaceSnapshot(
24
30
  if (!roots.length) return null;
25
31
  const files = new Map<string, TextDocumentItem>();
26
32
  const visited = new Set<string>();
33
+ let incomplete = false;
27
34
  const excluded = new Set([
28
35
  "node_modules",
29
36
  ".git",
@@ -32,14 +39,44 @@ export function workspaceSnapshot(
32
39
  "build",
33
40
  "coverage",
34
41
  ]);
42
+ const canonicalRoots: string[] = [];
43
+ try {
44
+ for (const root of roots) canonicalRoots.push(realpathSync(root));
45
+ } catch (error) {
46
+ if (!isExpectedFsAccessError(error)) throw error;
47
+ return null;
48
+ }
49
+ const indexedTarget = (path: string): boolean =>
50
+ canonicalRoots.some((root) => {
51
+ const child = relative(root, path);
52
+ return (
53
+ child !== ".." &&
54
+ !child.startsWith(`..${sep}`) &&
55
+ !isAbsolute(child) &&
56
+ !child.split(sep).some((part) => excluded.has(part))
57
+ );
58
+ });
35
59
  const walk = (directory: string): void => {
36
60
  if (visited.has(directory)) return;
37
61
  visited.add(directory);
38
62
  for (const entry of readdirSync(directory, { withFileTypes: true })) {
39
63
  if (excluded.has(entry.name)) continue;
40
64
  const path = join(directory, entry.name);
41
- if (entry.isSymbolicLink())
42
- throw new Error("Workspace contains an unindexed symbolic link");
65
+ if (entry.isSymbolicLink()) {
66
+ try {
67
+ const target = realpathSync(path);
68
+ if (
69
+ !indexedTarget(target) ||
70
+ (!statSync(target).isDirectory() &&
71
+ !/\.(?:fun|funs|funx|[cm]?[jt]sx?)$/.test(target))
72
+ )
73
+ incomplete = true;
74
+ } catch (error) {
75
+ if (!isExpectedFsAccessError(error)) throw error;
76
+ incomplete = true;
77
+ }
78
+ continue;
79
+ }
43
80
  if (entry.isDirectory()) {
44
81
  walk(path);
45
82
  continue;
@@ -59,13 +96,30 @@ export function workspaceSnapshot(
59
96
  }
60
97
  };
61
98
  try {
62
- for (const root of roots) walk(root);
99
+ for (const root of canonicalRoots) walk(root);
63
100
  } catch (error) {
64
101
  if (!isExpectedFsAccessError(error)) throw error;
65
102
  return null;
66
103
  }
67
- for (const doc of [...documents, ...interopDocuments])
68
- files.set(doc.uri, doc);
104
+ if (incomplete && !options.allowIncomplete) return null;
105
+ const openFiles = new Map<string, TextDocumentItem>();
106
+ for (const doc of [...documents, ...interopDocuments]) {
107
+ let key = doc.uri;
108
+ if (doc.uri.startsWith("file:")) {
109
+ try {
110
+ const canonical = realpathSync(fileURLToPath(doc.uri));
111
+ if (!indexedTarget(canonical) && !options.allowIncomplete) return null;
112
+ key = pathToFileURL(canonical).href;
113
+ } catch (error) {
114
+ if (!isExpectedFsAccessError(error)) throw error;
115
+ }
116
+ }
117
+ const previous = openFiles.get(key);
118
+ if (previous && previous.text !== doc.text && !options.allowIncomplete)
119
+ return null;
120
+ openFiles.set(key, doc);
121
+ }
122
+ for (const [uri, doc] of openFiles) files.set(uri, doc);
69
123
  const all = [...files.values()];
70
124
  return {
71
125
  fun: all.filter((doc) => /\.(?:fun|funs|funx)$/.test(doc.uri)),
@@ -12,27 +12,16 @@ import { TypeScriptCompilationContext } from "../compiler/typescript-context.js"
12
12
  import { isFileNotFoundError } from "../errors.js";
13
13
  import { FUNSCRIPT_VERSION } from "../version.js";
14
14
  import { TypeView, type TypeViewOptions } from "./type-view.js";
15
+ import {
16
+ matchesAnyPattern,
17
+ isServerBoundaryEnabled,
18
+ createServerBoundary,
19
+ type ServerBoundaryOptions,
20
+ } from "./server-boundary.js";
15
21
 
16
22
  export { TypeView, type TypeViewOptions };
17
23
 
18
- export interface ServerBoundaryOptions {
19
- /**
20
- * Whether to enforce compile-time separation of server modules in client builds.
21
- * Defaults to false (opt-in).
22
- */
23
- enabled?: boolean;
24
- /**
25
- * Matchers for server-only modules that must not be bundled into client builds.
26
- * Defaults to [/\.server(\.|$)/].
27
- */
28
- serverPatterns?: (string | RegExp)[];
29
- /**
30
- * Matchers for permitted boundary/bridge modules (e.g. route modules in full-stack
31
- * frameworks) where a framework compiler plugin tree-shakes server code from client outputs.
32
- * Defaults to [].
33
- */
34
- allowedImporters?: (string | RegExp)[];
35
- }
24
+ export type { ServerBoundaryOptions } from "./server-boundary.js";
36
25
 
37
26
  export interface BunPluginOptions {
38
27
  /** Reject raw TypeScript escapes in compiled FunkScript modules. */
@@ -139,19 +128,6 @@ export interface BunPlugin {
139
128
  setup(build: BunPluginBuilder): void | Promise<void>;
140
129
  }
141
130
 
142
- function matchesPattern(value: string, pattern: string | RegExp): boolean {
143
- return typeof pattern === "string"
144
- ? value.includes(pattern)
145
- : pattern.test(value);
146
- }
147
-
148
- function matchesAnyPattern(
149
- value: string,
150
- patterns: (string | RegExp)[],
151
- ): boolean {
152
- return patterns.some((p) => matchesPattern(value, p));
153
- }
154
-
155
131
  /**
156
132
  * Bun runtime loader plugin for FunkScript and FunX (.fun, .funx) compilation.
157
133
  * Compiles FunkScript in-memory directly to TypeScript for Bun's JavaScriptCore runtime,
@@ -190,43 +166,15 @@ export function funscriptBunPlugin(options: BunPluginOptions = {}): BunPlugin {
190
166
  await ensureTypeView();
191
167
  }
192
168
 
193
- const boundaryOption = options.serverBoundary;
194
- const boundaryEnabled =
195
- typeof boundaryOption === "boolean"
196
- ? boundaryOption
197
- : boundaryOption
198
- ? boundaryOption.enabled !== false
199
- : false;
200
-
201
- if (boundaryEnabled || resolveJsToFun) {
202
- const serverPatterns = (typeof boundaryOption === "object" &&
203
- boundaryOption?.serverPatterns) || [/\.server(\.|$)/];
204
- const allowedImporters =
205
- (typeof boundaryOption === "object" &&
206
- boundaryOption?.allowedImporters) ||
207
- [];
208
-
169
+ const violatesServerBoundary = createServerBoundary(
170
+ options.serverBoundary,
171
+ );
172
+ if (isServerBoundaryEnabled(options.serverBoundary) || resolveJsToFun) {
209
173
  build.onResolve({ filter: /.*/ }, (args) => {
210
- if (
211
- boundaryEnabled &&
212
- args.importer &&
213
- matchesAnyPattern(args.path, serverPatterns)
214
- ) {
215
- const isServerImporter = matchesAnyPattern(
216
- args.importer,
217
- serverPatterns,
174
+ if (violatesServerBoundary(args.path, args.importer)) {
175
+ throw new Error(
176
+ `[bun-plugin-funscript] Server module '${args.path}' cannot be imported into client module '${args.importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptBunPlugin().`,
218
177
  );
219
- if (!isServerImporter) {
220
- const isAllowedImporter = matchesAnyPattern(
221
- args.importer,
222
- allowedImporters,
223
- );
224
- if (!isAllowedImporter) {
225
- throw new Error(
226
- `[bun-plugin-funscript] Server module '${args.path}' cannot be imported into client module '${args.importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptBunPlugin().`,
227
- );
228
- }
229
- }
230
178
  }
231
179
 
232
180
  if (resolveJsToFun) {
@@ -8,8 +8,12 @@ import {
8
8
  isPathPolicyEnabled,
9
9
  loadProjectConfig,
10
10
  } from "../compiler/project-config.js";
11
- import type { ServerBoundaryOptions } from "./bun.js";
12
11
  import { TypeView, type TypeViewOptions } from "./type-view.js";
12
+ import {
13
+ matchesAnyPattern,
14
+ createServerBoundary,
15
+ type ServerBoundaryOptions,
16
+ } from "./server-boundary.js";
13
17
 
14
18
  export { TypeView, type ServerBoundaryOptions, type TypeViewOptions };
15
19
 
@@ -47,19 +51,6 @@ export interface EsbuildPluginOptions {
47
51
  entryFile?: string;
48
52
  }
49
53
 
50
- function matchesPattern(value: string, pattern: string | RegExp): boolean {
51
- return typeof pattern === "string"
52
- ? value.includes(pattern)
53
- : pattern.test(value);
54
- }
55
-
56
- function matchesAnyPattern(
57
- value: string,
58
- patterns: (string | RegExp)[],
59
- ): boolean {
60
- return patterns.some((p) => matchesPattern(value, p));
61
- }
62
-
63
54
  function isEsbuildEntry(
64
55
  filename: string,
65
56
  cwd: string,
@@ -116,49 +107,19 @@ export function funscriptEsbuildPlugin(
116
107
  await ensureTypeView();
117
108
  }
118
109
 
119
- const boundaryOption = options.serverBoundary;
120
- const boundaryEnabled =
121
- typeof boundaryOption === "boolean"
122
- ? boundaryOption
123
- : boundaryOption
124
- ? boundaryOption.enabled !== false
125
- : false;
126
-
127
- const serverPatterns = (typeof boundaryOption === "object" &&
128
- boundaryOption?.serverPatterns) || [/\.server(\.|$)/];
129
- const allowedImporters =
130
- (typeof boundaryOption === "object" &&
131
- boundaryOption?.allowedImporters) ||
132
- [];
133
-
110
+ const violatesServerBoundary = createServerBoundary(
111
+ options.serverBoundary,
112
+ );
134
113
  build.onResolve({ filter: /.*/ }, async (args) => {
135
114
  const specifier = args.path;
136
-
137
- // Check server boundary enforcement
138
- if (
139
- boundaryEnabled &&
140
- args.importer &&
141
- matchesAnyPattern(specifier, serverPatterns)
142
- ) {
143
- const isServerImporter = matchesAnyPattern(
144
- args.importer,
145
- serverPatterns,
146
- );
147
- if (!isServerImporter) {
148
- const isAllowedImporter = matchesAnyPattern(
149
- args.importer,
150
- allowedImporters,
151
- );
152
- if (!isAllowedImporter) {
153
- return {
154
- errors: [
155
- {
156
- text: `[funscript-esbuild] Server module '${specifier}' cannot be imported into client module '${args.importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptEsbuildPlugin().`,
157
- },
158
- ],
159
- };
160
- }
161
- }
115
+ if (violatesServerBoundary(specifier, args.importer)) {
116
+ return {
117
+ errors: [
118
+ {
119
+ text: `[funscript-esbuild] Server module '${specifier}' cannot be imported into client module '${args.importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptEsbuildPlugin().`,
120
+ },
121
+ ],
122
+ };
162
123
  }
163
124
 
164
125
  if (!specifier.startsWith(".") && !path.isAbsolute(specifier)) {
@@ -0,0 +1,41 @@
1
+ /** Compile-time separation of server modules in client builds. */
2
+ export interface ServerBoundaryOptions {
3
+ /** Enable boundary checks. Defaults to true when options are provided. */
4
+ enabled?: boolean;
5
+ /** Server-only module matchers. Defaults to [/\.server(\.|$)/]. */
6
+ serverPatterns?: (string | RegExp)[];
7
+ /** Importers permitted to bridge the server/client boundary. Defaults to []. */
8
+ allowedImporters?: (string | RegExp)[];
9
+ }
10
+
11
+ export function matchesAnyPattern(
12
+ value: string,
13
+ patterns: readonly (string | RegExp)[],
14
+ ): boolean {
15
+ return patterns.some((pattern) =>
16
+ typeof pattern === "string" ? value.includes(pattern) : pattern.test(value),
17
+ );
18
+ }
19
+
20
+ export function isServerBoundaryEnabled(
21
+ option: boolean | ServerBoundaryOptions | undefined,
22
+ ): boolean {
23
+ if (typeof option === "boolean") return option;
24
+ return option !== undefined && option !== null && option.enabled !== false;
25
+ }
26
+
27
+ /** Capture a boundary policy for the lifetime of a plugin setup. */
28
+ export function createServerBoundary(
29
+ option: boolean | ServerBoundaryOptions | undefined,
30
+ ): (source: string, importer: string | undefined) => boolean {
31
+ const enabled = isServerBoundaryEnabled(option);
32
+ const settings = typeof option === "object" && option !== null ? option : {};
33
+ const serverPatterns = settings.serverPatterns || [/\.server(\.|$)/];
34
+ const allowedImporters = settings.allowedImporters || [];
35
+ return (source, importer) => {
36
+ if (!enabled || !importer) return false;
37
+ if (!matchesAnyPattern(source, serverPatterns)) return false;
38
+ if (matchesAnyPattern(importer, serverPatterns)) return false;
39
+ return !matchesAnyPattern(importer, allowedImporters);
40
+ };
41
+ }
@@ -10,25 +10,12 @@ import {
10
10
  } from "../compiler/project-config.js";
11
11
  import { TypeScriptCompilationContext } from "../compiler/typescript-context.js";
12
12
  import { TypeView, type TypeViewOptions } from "./type-view.js";
13
+ import {
14
+ createServerBoundary,
15
+ type ServerBoundaryOptions,
16
+ } from "./server-boundary.js";
13
17
 
14
- export interface ServerBoundaryOptions {
15
- /**
16
- * Whether to enforce compile-time separation of server modules in client builds.
17
- * Defaults to false (opt-in).
18
- */
19
- enabled?: boolean;
20
- /**
21
- * Matchers for server-only modules that must not be bundled into client builds.
22
- * Defaults to [/\.server(\.|$)/].
23
- */
24
- serverPatterns?: (string | RegExp)[];
25
- /**
26
- * Matchers for permitted boundary/bridge modules (e.g. route modules in full-stack
27
- * frameworks) where a framework compiler plugin tree-shakes server code from client outputs.
28
- * Defaults to [].
29
- */
30
- allowedImporters?: (string | RegExp)[];
31
- }
18
+ export type { ServerBoundaryOptions } from "./server-boundary.js";
32
19
 
33
20
  export interface FunscriptPluginOptions {
34
21
  /** Reject raw TypeScript escapes in compiled FunkScript modules. */
@@ -60,19 +47,6 @@ export interface FunscriptPluginOptions {
60
47
  serverBoundary?: boolean | ServerBoundaryOptions;
61
48
  }
62
49
 
63
- function matchesPattern(value: string, pattern: string | RegExp): boolean {
64
- return typeof pattern === "string"
65
- ? value.includes(pattern)
66
- : pattern.test(value);
67
- }
68
-
69
- function matchesAnyPattern(
70
- value: string,
71
- patterns: (string | RegExp)[],
72
- ): boolean {
73
- return patterns.some((p) => matchesPattern(value, p));
74
- }
75
-
76
50
  /**
77
51
  * Vite & Vitest plugin for FunkScript and FunX (.fun, .funx) compilation.
78
52
  */
@@ -126,37 +100,13 @@ export function funscriptPlugin(options: FunscriptPluginOptions = {}): Plugin {
126
100
  },
127
101
 
128
102
  async resolveId(source, importer, resolveOptions) {
129
- const boundaryOption = options.serverBoundary;
130
- const boundaryEnabled =
131
- typeof boundaryOption === "boolean"
132
- ? boundaryOption
133
- : boundaryOption
134
- ? boundaryOption.enabled !== false
135
- : false;
136
-
137
- if (boundaryEnabled && importer && !resolveOptions?.ssr) {
138
- const serverPatterns = (typeof boundaryOption === "object" &&
139
- boundaryOption?.serverPatterns) || [/\.server(\.|$)/];
140
-
141
- if (matchesAnyPattern(source, serverPatterns)) {
142
- const isServerImporter = matchesAnyPattern(importer, serverPatterns);
143
- if (!isServerImporter) {
144
- const allowedImporters =
145
- (typeof boundaryOption === "object" &&
146
- boundaryOption?.allowedImporters) ||
147
- [];
148
-
149
- const isAllowedImporter = matchesAnyPattern(
150
- importer,
151
- allowedImporters,
152
- );
153
- if (!isAllowedImporter) {
154
- throw new Error(
155
- `[vite-plugin-funscript] Server module '${source}' cannot be imported into client module '${importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptPlugin().`,
156
- );
157
- }
158
- }
159
- }
103
+ if (
104
+ !resolveOptions?.ssr &&
105
+ createServerBoundary(options.serverBoundary)(source, importer)
106
+ ) {
107
+ throw new Error(
108
+ `[vite-plugin-funscript] Server module '${source}' cannot be imported into client module '${importer}'. FunkScript enforces strict server/client boundary separation. To permit framework bridge modules (e.g. route loaders), configure 'serverBoundary.allowedImporters' in funscriptPlugin().`,
109
+ );
160
110
  }
161
111
 
162
112
  if (