brumate 0.1.0 → 0.1.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.
- package/dist/cli.js +2 -2
- package/dist/{files-BBLPU9JR.js → files-DdT84mPy.js} +2 -2
- package/dist/{files-BBLPU9JR.js.map → files-DdT84mPy.js.map} +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/{inspect-CRZlO11T.js → inspect-BPchdWbU.js} +148 -13
- package/dist/inspect-BPchdWbU.js.map +1 -0
- package/dist/vite.js +2 -2
- package/package.json +1 -1
- package/dist/inspect-CRZlO11T.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { D as analyzeProjectProps, a as CompileError, n as inspect } from "./inspect-
|
|
3
|
-
import { n as findFiles, t as defaultExclude } from "./files-
|
|
2
|
+
import { D as analyzeProjectProps, a as CompileError, n as inspect } from "./inspect-BPchdWbU.js";
|
|
3
|
+
import { n as findFiles, t as defaultExclude } from "./files-DdT84mPy.js";
|
|
4
4
|
import { readFile, stat } from "node:fs/promises";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
//#region src/cli.ts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { g as functionId } from "./inspect-
|
|
1
|
+
import { g as functionId } from "./inspect-BPchdWbU.js";
|
|
2
2
|
import { parseSync } from "oxc-parser";
|
|
3
3
|
import { readFile, readdir } from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
@@ -174,4 +174,4 @@ async function findScripts(root, exclude) {
|
|
|
174
174
|
//#endregion
|
|
175
175
|
export { hasUseServer as a, serverModule as c, findServerFiles as i, serverStubs as l, findFiles as n, isScriptFile as o, findScripts as r, serverExports as s, defaultExclude as t };
|
|
176
176
|
|
|
177
|
-
//# sourceMappingURL=files-
|
|
177
|
+
//# sourceMappingURL=files-DdT84mPy.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"files-BBLPU9JR.js","names":[],"sources":["../src/server-files.ts","../src/files.ts"],"sourcesContent":["// Files of server functions. A `.ts` or `.js` file whose first statement is\n// `'use server'` holds functions that only run on the server. Its exports are\n// server functions: the browser gets a stub for each one, which calls the\n// server, and the code of the file never ships.\n\nimport { parseSync } from 'oxc-parser';\nimport { functionId } from './plan.ts';\n\n/** The import of the stubs, which call server functions with `rpc` of the core. */\nconst CLIENT = 'brumate/client';\n\n/** True for a file whose first statement is `'use server'`, after comments. */\nexport function hasUseServer(source: string): boolean {\n return /^(?:\\s|\\/\\/[^\\n]*\\n|\\/\\*[\\s\\S]*?\\*\\/)*(['\"])use server\\1/.test(source);\n}\n\n/** True for a path that can be a file of server functions. */\nexport function isScriptFile(path: string): boolean {\n return (\n /\\.[cm]?[jt]sx?$/.test(path) && !path.endsWith('.d.ts') && !/\\.brm\\.[cm]?[jt]s$/.test(path)\n );\n}\n\n/** True for a function expression or an arrow. */\nfunction isFunction(node: { type: string } | null | undefined): boolean {\n return node?.type === 'ArrowFunctionExpression' || node?.type === 'FunctionExpression';\n}\n\n/**\n * The names of the functions that a file of server functions exports.\n * Throws for an export that is not a function, because the browser can only\n * call a server function through its stub.\n */\nexport function serverExports(source: string, filename: string): string[] {\n const { program, errors } = parseSync(filename, source, { lang: 'ts' });\n if (errors[0]) {\n throw new Error(`${errors[0].message} (${filename})`);\n }\n const functions = new Set<string>();\n const values = new Set<string>();\n const local = new Map<string, 'function' | 'value'>();\n for (const statement of program.body) {\n const declaration =\n statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n local.set(declaration.id.name, 'function');\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier') {\n local.set(declarator.id.name, isFunction(declarator.init) ? 'function' : 'value');\n }\n }\n }\n }\n for (const statement of program.body) {\n if (statement.type === 'ExportDefaultDeclaration') {\n const value = statement.declaration;\n if (value.type === 'FunctionDeclaration' || isFunction(value)) {\n functions.add('default');\n } else {\n values.add('default');\n }\n } else if (statement.type === 'ExportNamedDeclaration') {\n if (statement.exportKind === 'type') {\n continue;\n }\n const declaration = statement.declaration;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n functions.add(declaration.id.name);\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier') {\n (isFunction(declarator.init) ? functions : values).add(declarator.id.name);\n }\n }\n } else if (declaration && !declaration.type.startsWith('TS')) {\n values.add('a class');\n }\n for (const specifier of statement.specifiers) {\n if (specifier.exportKind === 'type') {\n continue;\n }\n const name =\n specifier.exported.type === 'Identifier'\n ? specifier.exported.name\n : specifier.exported.value;\n const from = specifier.local.type === 'Identifier' ? specifier.local.name : '';\n (local.get(from) === 'function' && !statement.source ? functions : values).add(name);\n }\n } else if (statement.type === 'ExportAllDeclaration') {\n values.add('*');\n }\n }\n if (values.size > 0) {\n throw new Error(\n `A 'use server' file exports only functions, because the browser calls them on the server. ${[...values].join(', ')} of ${filename} is not one`,\n );\n }\n return [...functions];\n}\n\n/** The module that the browser gets in place of a file of server functions. */\nexport function serverStubs(\n source: string,\n filename: string,\n id: string,\n hideNames = false,\n): string {\n const lines = [`import { rpc } from ${JSON.stringify(CLIENT)};`];\n for (const name of serverExports(source, filename)) {\n const call = `(...args) => rpc(${JSON.stringify(functionId(id, name, hideNames))}, args)`;\n lines.push(name === 'default' ? `export default ${call};` : `export const ${name} = ${call};`);\n }\n return `${lines.join('\\n')}\\n`;\n}\n\n/**\n * The module that the server runs for a file of server functions: the file,\n * with each exported function wrapped, so its calls while a page renders\n * share a result when their arguments are the same. Offsets stay the same.\n */\nexport function serverModule(source: string, filename: string, id: string): string {\n const { program } = parseSync(filename, source, { lang: 'ts' });\n // Local names of exported functions, with the name that each is exported as.\n const wrapped = new Map<string, string>();\n const declarations = new Map<string, 'function' | 'const'>();\n let code = source;\n for (const statement of program.body) {\n const declaration =\n statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n declarations.set(declaration.id.name, 'function');\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier' && isFunction(declarator.init)) {\n declarations.set(declarator.id.name, 'const');\n }\n }\n }\n }\n const letOf = (declaration: { start: number; kind: string }): void => {\n // `const` becomes `let `, so the function can be wrapped and offsets stay the same.\n if (declaration.kind === 'const') {\n code = `${code.slice(0, declaration.start)}let ${code.slice(declaration.start + 5)}`;\n }\n };\n for (const statement of program.body) {\n if (\n statement.type !== 'ExportNamedDeclaration' ||\n statement.source ||\n statement.exportKind === 'type'\n ) {\n continue;\n }\n const declaration = statement.declaration;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n wrapped.set(declaration.id.name, declaration.id.name);\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier' && isFunction(declarator.init)) {\n wrapped.set(declarator.id.name, declarator.id.name);\n letOf(declaration);\n }\n }\n }\n for (const specifier of statement.specifiers) {\n const local = specifier.local.type === 'Identifier' ? specifier.local.name : '';\n const exported =\n specifier.exported.type === 'Identifier'\n ? specifier.exported.name\n : specifier.exported.value;\n if (declarations.has(local)) {\n wrapped.set(local, exported);\n }\n }\n }\n // A `const` arrow that `export { name }` exports is declared with `let` too.\n for (const statement of program.body) {\n if (statement.type === 'VariableDeclaration') {\n for (const declarator of statement.declarations) {\n if (declarator.id.type === 'Identifier' && wrapped.has(declarator.id.name)) {\n letOf(statement);\n }\n }\n }\n }\n if (wrapped.size === 0) {\n return source;\n }\n const lines = [\"import { dedupe as __brumateDedupe } from 'brumate/server';\"];\n for (const [local, exported] of wrapped) {\n lines.push(`${local} = __brumateDedupe(${local}, ${JSON.stringify(`${id}#${exported}`)});`);\n }\n return `${code}\\n${lines.join('\\n')}\\n`;\n}\n","import { readFile, readdir } from 'node:fs/promises';\nimport path from 'node:path';\nimport { hasUseServer, isScriptFile } from './server-files.ts';\n\n/** Skips `node_modules`, `dist` and hidden directories when looking for `.brm` files. */\nexport function defaultExclude(directory: string): boolean {\n return directory === 'node_modules' || directory === 'dist' || directory.startsWith('.');\n}\n\n/** Finds every `.brm` file under a directory, as absolute paths with forward slashes. */\nexport async function findFiles(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const directories: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n directories.push(visit(file));\n }\n } else if (entry.name.endsWith('.brm')) {\n result.push(file.split(path.sep).join('/'));\n }\n }\n await Promise.all(directories);\n };\n await visit(root);\n return result.toSorted();\n}\n\n/**\n * Finds every file of server functions under a directory: a script whose\n * first statement is `'use server'`.\n */\nexport async function findServerFiles(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const pending: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n pending.push(visit(file));\n }\n } else if (isScriptFile(entry.name)) {\n pending.push(\n readFile(file, 'utf8').then((source) => {\n if (hasUseServer(source)) {\n result.push(file.split(path.sep).join('/'));\n }\n }),\n );\n }\n }\n await Promise.all(pending);\n };\n await visit(root);\n return result.toSorted();\n}\n\n/**\n * Finds every script under a directory, such as `.ts` and `.brm.ts` files, as\n * absolute paths with forward slashes, without reading them.\n */\nexport async function findScripts(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const directories: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n directories.push(visit(file));\n }\n } else if (/\\.[cm]?[jt]sx?$/.test(entry.name) && !entry.name.endsWith('.d.ts')) {\n result.push(file.split(path.sep).join('/'));\n }\n }\n await Promise.all(directories);\n };\n await visit(root);\n return result.toSorted();\n}\n"],"mappings":";;;;;;AASA,MAAM,SAAS;;AAGf,SAAgB,aAAa,QAAyB;CACpD,OAAO,2DAA2D,KAAK,MAAM;AAC/E;;AAGA,SAAgB,aAAa,MAAuB;CAClD,OACE,kBAAkB,KAAK,IAAI,KAAK,CAAC,KAAK,SAAS,OAAO,KAAK,CAAC,qBAAqB,KAAK,IAAI;AAE9F;;AAGA,SAAS,WAAW,MAAoD;CACtE,OAAO,MAAM,SAAS,6BAA6B,MAAM,SAAS;AACpE;;;;;;AAOA,SAAgB,cAAc,QAAgB,UAA4B;CACxE,MAAM,EAAE,SAAS,WAAW,UAAU,UAAU,QAAQ,EAAE,MAAM,KAAK,CAAC;CACtE,IAAI,OAAO,IACT,MAAM,IAAI,MAAM,GAAG,OAAO,EAAE,CAAC,QAAQ,IAAI,SAAS,EAAE;CAEtD,MAAM,4BAAY,IAAI,IAAY;CAClC,MAAM,yBAAS,IAAI,IAAY;CAC/B,MAAM,wBAAQ,IAAI,IAAkC;CACpD,KAAK,MAAM,aAAa,QAAQ,MAAM;EACpC,MAAM,cACJ,UAAU,SAAS,2BAA2B,UAAU,cAAc;EACxE,IAAI,aAAa,SAAS,yBAAyB,YAAY,IAC7D,MAAM,IAAI,YAAY,GAAG,MAAM,UAAU;OACpC,IAAI,aAAa,SAAS,uBAC1B;QAAA,MAAM,cAAc,YAAY,cACnC,IAAI,WAAW,GAAG,SAAS,cACzB,MAAM,IAAI,WAAW,GAAG,MAAM,WAAW,WAAW,IAAI,IAAI,aAAa,OAAO;EAAA;CAIxF;CACA,KAAK,MAAM,aAAa,QAAQ,MAC9B,IAAI,UAAU,SAAS,4BAA4B;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AK,SAAS,OAAO,GAC3E,OAAO,KAAK,KAAK,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC;EAE9C;EACA,MAAM,QAAQ,IAAI,WAAW;CAC/B;CACA,MAAM,MAAM,IAAI;CAChB,OAAO,OAAO,SAAS;AACzB"}
|
|
1
|
+
{"version":3,"file":"files-DdT84mPy.js","names":[],"sources":["../src/server-files.ts","../src/files.ts"],"sourcesContent":["// Files of server functions. A `.ts` or `.js` file whose first statement is\n// `'use server'` holds functions that only run on the server. Its exports are\n// server functions: the browser gets a stub for each one, which calls the\n// server, and the code of the file never ships.\n\nimport { parseSync } from 'oxc-parser';\nimport { functionId } from './plan.ts';\n\n/** The import of the stubs, which call server functions with `rpc` of the core. */\nconst CLIENT = 'brumate/client';\n\n/** True for a file whose first statement is `'use server'`, after comments. */\nexport function hasUseServer(source: string): boolean {\n return /^(?:\\s|\\/\\/[^\\n]*\\n|\\/\\*[\\s\\S]*?\\*\\/)*(['\"])use server\\1/.test(source);\n}\n\n/** True for a path that can be a file of server functions. */\nexport function isScriptFile(path: string): boolean {\n return (\n /\\.[cm]?[jt]sx?$/.test(path) && !path.endsWith('.d.ts') && !/\\.brm\\.[cm]?[jt]s$/.test(path)\n );\n}\n\n/** True for a function expression or an arrow. */\nfunction isFunction(node: { type: string } | null | undefined): boolean {\n return node?.type === 'ArrowFunctionExpression' || node?.type === 'FunctionExpression';\n}\n\n/**\n * The names of the functions that a file of server functions exports.\n * Throws for an export that is not a function, because the browser can only\n * call a server function through its stub.\n */\nexport function serverExports(source: string, filename: string): string[] {\n const { program, errors } = parseSync(filename, source, { lang: 'ts' });\n if (errors[0]) {\n throw new Error(`${errors[0].message} (${filename})`);\n }\n const functions = new Set<string>();\n const values = new Set<string>();\n const local = new Map<string, 'function' | 'value'>();\n for (const statement of program.body) {\n const declaration =\n statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n local.set(declaration.id.name, 'function');\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier') {\n local.set(declarator.id.name, isFunction(declarator.init) ? 'function' : 'value');\n }\n }\n }\n }\n for (const statement of program.body) {\n if (statement.type === 'ExportDefaultDeclaration') {\n const value = statement.declaration;\n if (value.type === 'FunctionDeclaration' || isFunction(value)) {\n functions.add('default');\n } else {\n values.add('default');\n }\n } else if (statement.type === 'ExportNamedDeclaration') {\n if (statement.exportKind === 'type') {\n continue;\n }\n const declaration = statement.declaration;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n functions.add(declaration.id.name);\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier') {\n (isFunction(declarator.init) ? functions : values).add(declarator.id.name);\n }\n }\n } else if (declaration && !declaration.type.startsWith('TS')) {\n values.add('a class');\n }\n for (const specifier of statement.specifiers) {\n if (specifier.exportKind === 'type') {\n continue;\n }\n const name =\n specifier.exported.type === 'Identifier'\n ? specifier.exported.name\n : specifier.exported.value;\n const from = specifier.local.type === 'Identifier' ? specifier.local.name : '';\n (local.get(from) === 'function' && !statement.source ? functions : values).add(name);\n }\n } else if (statement.type === 'ExportAllDeclaration') {\n values.add('*');\n }\n }\n if (values.size > 0) {\n throw new Error(\n `A 'use server' file exports only functions, because the browser calls them on the server. ${[...values].join(', ')} of ${filename} is not one`,\n );\n }\n return [...functions];\n}\n\n/** The module that the browser gets in place of a file of server functions. */\nexport function serverStubs(\n source: string,\n filename: string,\n id: string,\n hideNames = false,\n): string {\n const lines = [`import { rpc } from ${JSON.stringify(CLIENT)};`];\n for (const name of serverExports(source, filename)) {\n const call = `(...args) => rpc(${JSON.stringify(functionId(id, name, hideNames))}, args)`;\n lines.push(name === 'default' ? `export default ${call};` : `export const ${name} = ${call};`);\n }\n return `${lines.join('\\n')}\\n`;\n}\n\n/**\n * The module that the server runs for a file of server functions: the file,\n * with each exported function wrapped, so its calls while a page renders\n * share a result when their arguments are the same. Offsets stay the same.\n */\nexport function serverModule(source: string, filename: string, id: string): string {\n const { program } = parseSync(filename, source, { lang: 'ts' });\n // Local names of exported functions, with the name that each is exported as.\n const wrapped = new Map<string, string>();\n const declarations = new Map<string, 'function' | 'const'>();\n let code = source;\n for (const statement of program.body) {\n const declaration =\n statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n declarations.set(declaration.id.name, 'function');\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier' && isFunction(declarator.init)) {\n declarations.set(declarator.id.name, 'const');\n }\n }\n }\n }\n const letOf = (declaration: { start: number; kind: string }): void => {\n // `const` becomes `let `, so the function can be wrapped and offsets stay the same.\n if (declaration.kind === 'const') {\n code = `${code.slice(0, declaration.start)}let ${code.slice(declaration.start + 5)}`;\n }\n };\n for (const statement of program.body) {\n if (\n statement.type !== 'ExportNamedDeclaration' ||\n statement.source ||\n statement.exportKind === 'type'\n ) {\n continue;\n }\n const declaration = statement.declaration;\n if (declaration?.type === 'FunctionDeclaration' && declaration.id) {\n wrapped.set(declaration.id.name, declaration.id.name);\n } else if (declaration?.type === 'VariableDeclaration') {\n for (const declarator of declaration.declarations) {\n if (declarator.id.type === 'Identifier' && isFunction(declarator.init)) {\n wrapped.set(declarator.id.name, declarator.id.name);\n letOf(declaration);\n }\n }\n }\n for (const specifier of statement.specifiers) {\n const local = specifier.local.type === 'Identifier' ? specifier.local.name : '';\n const exported =\n specifier.exported.type === 'Identifier'\n ? specifier.exported.name\n : specifier.exported.value;\n if (declarations.has(local)) {\n wrapped.set(local, exported);\n }\n }\n }\n // A `const` arrow that `export { name }` exports is declared with `let` too.\n for (const statement of program.body) {\n if (statement.type === 'VariableDeclaration') {\n for (const declarator of statement.declarations) {\n if (declarator.id.type === 'Identifier' && wrapped.has(declarator.id.name)) {\n letOf(statement);\n }\n }\n }\n }\n if (wrapped.size === 0) {\n return source;\n }\n const lines = [\"import { dedupe as __brumateDedupe } from 'brumate/server';\"];\n for (const [local, exported] of wrapped) {\n lines.push(`${local} = __brumateDedupe(${local}, ${JSON.stringify(`${id}#${exported}`)});`);\n }\n return `${code}\\n${lines.join('\\n')}\\n`;\n}\n","import { readFile, readdir } from 'node:fs/promises';\nimport path from 'node:path';\nimport { hasUseServer, isScriptFile } from './server-files.ts';\n\n/** Skips `node_modules`, `dist` and hidden directories when looking for `.brm` files. */\nexport function defaultExclude(directory: string): boolean {\n return directory === 'node_modules' || directory === 'dist' || directory.startsWith('.');\n}\n\n/** Finds every `.brm` file under a directory, as absolute paths with forward slashes. */\nexport async function findFiles(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const directories: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n directories.push(visit(file));\n }\n } else if (entry.name.endsWith('.brm')) {\n result.push(file.split(path.sep).join('/'));\n }\n }\n await Promise.all(directories);\n };\n await visit(root);\n return result.toSorted();\n}\n\n/**\n * Finds every file of server functions under a directory: a script whose\n * first statement is `'use server'`.\n */\nexport async function findServerFiles(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const pending: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n pending.push(visit(file));\n }\n } else if (isScriptFile(entry.name)) {\n pending.push(\n readFile(file, 'utf8').then((source) => {\n if (hasUseServer(source)) {\n result.push(file.split(path.sep).join('/'));\n }\n }),\n );\n }\n }\n await Promise.all(pending);\n };\n await visit(root);\n return result.toSorted();\n}\n\n/**\n * Finds every script under a directory, such as `.ts` and `.brm.ts` files, as\n * absolute paths with forward slashes, without reading them.\n */\nexport async function findScripts(\n root: string,\n exclude: (directory: string) => boolean,\n): Promise<string[]> {\n const result: string[] = [];\n const visit = async (directory: string): Promise<void> => {\n const directories: Promise<void>[] = [];\n for (const entry of await readdir(directory, { withFileTypes: true })) {\n const file = path.join(directory, entry.name);\n if (entry.isDirectory()) {\n if (!exclude(entry.name)) {\n directories.push(visit(file));\n }\n } else if (/\\.[cm]?[jt]sx?$/.test(entry.name) && !entry.name.endsWith('.d.ts')) {\n result.push(file.split(path.sep).join('/'));\n }\n }\n await Promise.all(directories);\n };\n await visit(root);\n return result.toSorted();\n}\n"],"mappings":";;;;;;AASA,MAAM,SAAS;;AAGf,SAAgB,aAAa,QAAyB;CACpD,OAAO,2DAA2D,KAAK,MAAM;AAC/E;;AAGA,SAAgB,aAAa,MAAuB;CAClD,OACE,kBAAkB,KAAK,IAAI,KAAK,CAAC,KAAK,SAAS,OAAO,KAAK,CAAC,qBAAqB,KAAK,IAAI;AAE9F;;AAGA,SAAS,WAAW,MAAoD;CACtE,OAAO,MAAM,SAAS,6BAA6B,MAAM,SAAS;AACpE;;;;;;AAOA,SAAgB,cAAc,QAAgB,UAA4B;CACxE,MAAM,EAAE,SAAS,WAAW,UAAU,UAAU,QAAQ,EAAE,MAAM,KAAK,CAAC;CACtE,IAAI,OAAO,IACT,MAAM,IAAI,MAAM,GAAG,OAAO,EAAE,CAAC,QAAQ,IAAI,SAAS,EAAE;CAEtD,MAAM,4BAAY,IAAI,IAAY;CAClC,MAAM,yBAAS,IAAI,IAAY;CAC/B,MAAM,wBAAQ,IAAI,IAAkC;CACpD,KAAK,MAAM,aAAa,QAAQ,MAAM;EACpC,MAAM,cACJ,UAAU,SAAS,2BAA2B,UAAU,cAAc;EACxE,IAAI,aAAa,SAAS,yBAAyB,YAAY,IAC7D,MAAM,IAAI,YAAY,GAAG,MAAM,UAAU;OACpC,IAAI,aAAa,SAAS,uBAC1B;QAAA,MAAM,cAAc,YAAY,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W,MAAM,IAAI;GAC5C,IAAI,MAAM,YAAY,GAChB;QAAA,CAAC,QAAQ,MAAM,IAAI,GACrB,YAAY,KAAK,MAAM,IAAI,CAAC;GAAA,OAEzB,IAAI,kBAAkB,KAAK,MAAM,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,OAAO,GAC3E,OAAO,KAAK,KAAK,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC;EAE9C;EACA,MAAM,QAAQ,IAAI,WAAW;CAC/B;CACA,MAAM,MAAM,IAAI;CAChB,OAAO,OAAO,SAAS;AACzB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1564,6 +1564,8 @@ interface Declaration {
|
|
|
1564
1564
|
* a guess, until the source changes or the server calls of the page finish.
|
|
1565
1565
|
*/
|
|
1566
1566
|
optimistic?: Declaration;
|
|
1567
|
+
/** For `let name = $signal(value)`, the value. */
|
|
1568
|
+
init?: Node;
|
|
1567
1569
|
/** True for a memo declared with `$optimistic`, before its source is known. */
|
|
1568
1570
|
writable?: boolean;
|
|
1569
1571
|
/** For `const name = $id()`, the position of the call among the `$id` calls of its scope. */
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as summarize, D as analyzeProjectProps, E as analyzeProject, F as analyze, I as isWithin, L as lookup, M as buildGraph, N as BrumateError, P as getLocation, T as unmarked, _ as getComponentName, a as CompileError, b as generateShared, c as generateClient$1, h as createPlan, j as parse, k as resolveRelative, l as generateRender$1, m as clientState, n as inspect, o as compile, p as blockBodies, r as report, t as formatReport, u as generateServer$1, x as sharedSites, y as SHARED_QUERY } from "./inspect-
|
|
1
|
+
import { A as summarize, D as analyzeProjectProps, E as analyzeProject, F as analyze, I as isWithin, L as lookup, M as buildGraph, N as BrumateError, P as getLocation, T as unmarked, _ as getComponentName, a as CompileError, b as generateShared, c as generateClient$1, h as createPlan, j as parse, k as resolveRelative, l as generateRender$1, m as clientState, n as inspect, o as compile, p as blockBodies, r as report, t as formatReport, u as generateServer$1, x as sharedSites, y as SHARED_QUERY } from "./inspect-BPchdWbU.js";
|
|
2
2
|
//#region src/print.ts
|
|
3
3
|
function at(source, range) {
|
|
4
4
|
const { line, column } = getLocation(source, range.start);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { u as insertValue } from "./html-DEeoepg3.js";
|
|
1
2
|
import { parseSync } from "oxc-parser";
|
|
2
3
|
import { parse } from "postcss";
|
|
3
4
|
import selectorParser from "postcss-selector-parser";
|
|
@@ -602,8 +603,11 @@ var Analyzer = class {
|
|
|
602
603
|
if (call.arguments.length > 1) this.report("error", `${name} takes one argument`, call);
|
|
603
604
|
const argument = call.arguments[0];
|
|
604
605
|
if (name === "$signal") {
|
|
605
|
-
this.addDeclaration(scope, id.name, "signal", id);
|
|
606
|
-
if (argument)
|
|
606
|
+
const declaration = this.addDeclaration(scope, id.name, "signal", id);
|
|
607
|
+
if (argument) {
|
|
608
|
+
declaration.init = argument;
|
|
609
|
+
this.createSite("setup", scope, argument, `signal ${id.name}`);
|
|
610
|
+
}
|
|
607
611
|
return;
|
|
608
612
|
}
|
|
609
613
|
const declaration = this.addDeclaration(scope, id.name, "memo", id);
|
|
@@ -3542,8 +3546,9 @@ function promiseName(name) {
|
|
|
3542
3546
|
* `$signal(x)` and `$memo(x)` become `(x)`, `$props()` becomes `$$props`
|
|
3543
3547
|
* and `$untrack(x)` becomes `(x)`. On the server, a memo is renamed to
|
|
3544
3548
|
* `$$p_<name>`, because it can hold a promise that is awaited where it is read.
|
|
3549
|
+
* The memos in `inline` are plain values on the server too.
|
|
3545
3550
|
*/
|
|
3546
|
-
function addValueEdits(patch, file, sites, mode = "server") {
|
|
3551
|
+
function addValueEdits(patch, file, sites, mode = "server", inline = /* @__PURE__ */ new Set()) {
|
|
3547
3552
|
const sitesByNode = /* @__PURE__ */ new Map();
|
|
3548
3553
|
for (const site of sites) sitesByNode.set(site.node, site);
|
|
3549
3554
|
const visit = (node) => {
|
|
@@ -3574,10 +3579,10 @@ function addValueEdits(patch, file, sites, mode = "server") {
|
|
|
3574
3579
|
return;
|
|
3575
3580
|
}
|
|
3576
3581
|
const site = name === "$memo" || name === "$optimistic" ? sitesByNode.get(argument) : void 0;
|
|
3577
|
-
const declaration = site?.declaration;
|
|
3582
|
+
const declaration = site?.declaration && !inline.has(site.declaration) ? site.declaration : void 0;
|
|
3578
3583
|
if (declaration && mode === "server") patch.replace(declaration.range.start, declaration.range.end, promiseName(declaration.name));
|
|
3579
3584
|
if (declaration && mode === "server") {
|
|
3580
|
-
for (const read of site.reads) if (read.range.start !== read.range.end && read.declaration !== declaration && (read.declaration.kind === "memo" || read.declaration.kind === "async-memo")) patch.replace(read.range.start, read.range.end, `$$b.read(${promiseName(read.declaration.name)})`);
|
|
3585
|
+
for (const read of site.reads) if (read.range.start !== read.range.end && read.declaration !== declaration && !inline.has(read.declaration) && (read.declaration.kind === "memo" || read.declaration.kind === "async-memo")) patch.replace(read.range.start, read.range.end, `$$b.read(${promiseName(read.declaration.name)})`);
|
|
3581
3586
|
patch.replace(node.start, argument.start, site.awaits ? "$$b.memo(async () => (" : "$$b.memo(() => (");
|
|
3582
3587
|
patch.replace(argument.end, node.end, "), $$ctx)");
|
|
3583
3588
|
} else if (site?.awaits) {
|
|
@@ -4382,6 +4387,117 @@ function awaitedMemos(plan, body, blocking) {
|
|
|
4382
4387
|
}
|
|
4383
4388
|
return [...result];
|
|
4384
4389
|
}
|
|
4390
|
+
/** Expressions that run code the compiler cannot see, or that a memo should not hold. */
|
|
4391
|
+
const UNSAFE_NODES = /* @__PURE__ */ new Set([
|
|
4392
|
+
"CallExpression",
|
|
4393
|
+
"NewExpression",
|
|
4394
|
+
"TaggedTemplateExpression",
|
|
4395
|
+
"AwaitExpression",
|
|
4396
|
+
"YieldExpression",
|
|
4397
|
+
"ImportExpression",
|
|
4398
|
+
"AssignmentExpression",
|
|
4399
|
+
"UpdateExpression",
|
|
4400
|
+
"FunctionExpression",
|
|
4401
|
+
"ArrowFunctionExpression",
|
|
4402
|
+
"ClassExpression"
|
|
4403
|
+
]);
|
|
4404
|
+
/**
|
|
4405
|
+
* True if an expression always gives a primitive, so it is never a promise
|
|
4406
|
+
* or an async iterable. A name is a primitive when `plain` says that it reads
|
|
4407
|
+
* a declaration that holds one.
|
|
4408
|
+
*/
|
|
4409
|
+
function isPrimitive(node, plain = () => false) {
|
|
4410
|
+
switch (node.type) {
|
|
4411
|
+
case "Identifier": return plain(node);
|
|
4412
|
+
case "Literal": return !("regex" in node && node.regex);
|
|
4413
|
+
case "TemplateLiteral":
|
|
4414
|
+
case "BinaryExpression":
|
|
4415
|
+
case "UnaryExpression": return true;
|
|
4416
|
+
case "LogicalExpression": return isPrimitive(node.left, plain) && isPrimitive(node.right, plain);
|
|
4417
|
+
case "ConditionalExpression": return isPrimitive(node.consequent, plain) && isPrimitive(node.alternate, plain);
|
|
4418
|
+
case "TSAsExpression":
|
|
4419
|
+
case "TSSatisfiesExpression":
|
|
4420
|
+
case "TSNonNullExpression": return isPrimitive(node.expression, plain);
|
|
4421
|
+
default: return false;
|
|
4422
|
+
}
|
|
4423
|
+
}
|
|
4424
|
+
/** True if an expression calls no code and, unless `members`, reads no property. */
|
|
4425
|
+
function isSafe(node, members) {
|
|
4426
|
+
if (UNSAFE_NODES.has(node.type)) return false;
|
|
4427
|
+
if (!members && (node.type === "MemberExpression" || node.type === "ChainExpression")) return false;
|
|
4428
|
+
for (const [, child] of children(node)) if (!isSafe(child, members)) return false;
|
|
4429
|
+
return true;
|
|
4430
|
+
}
|
|
4431
|
+
/** True if a name in the code of a site reads a declaration that `plain` accepts. */
|
|
4432
|
+
function readsPlain(site, name, plain) {
|
|
4433
|
+
return site?.reads.some((read) => read.range.start === name.start && read.range.end === name.end && plain(read.declaration)) ?? false;
|
|
4434
|
+
}
|
|
4435
|
+
const ARITHMETIC_ASSIGNMENTS = /* @__PURE__ */ new Set([
|
|
4436
|
+
"+=",
|
|
4437
|
+
"-=",
|
|
4438
|
+
"*=",
|
|
4439
|
+
"/=",
|
|
4440
|
+
"%=",
|
|
4441
|
+
"**=",
|
|
4442
|
+
"<<=",
|
|
4443
|
+
">>=",
|
|
4444
|
+
">>>=",
|
|
4445
|
+
"&=",
|
|
4446
|
+
"|=",
|
|
4447
|
+
"^="
|
|
4448
|
+
]);
|
|
4449
|
+
/** Returns the assignment or update of a site whose target is at `at`. */
|
|
4450
|
+
function writeAt(node, at) {
|
|
4451
|
+
if (node.type === "AssignmentExpression" && node.left.type === "Identifier" && node.left.start === at || node.type === "UpdateExpression" && node.argument.type === "Identifier" && node.argument.start === at) return node;
|
|
4452
|
+
for (const [, child] of children(node)) {
|
|
4453
|
+
const found = writeAt(child, at);
|
|
4454
|
+
if (found) return found;
|
|
4455
|
+
}
|
|
4456
|
+
}
|
|
4457
|
+
/**
|
|
4458
|
+
* Returns the memos and signals that always hold a primitive on the server,
|
|
4459
|
+
* so the server reads them as plain values.
|
|
4460
|
+
*
|
|
4461
|
+
* - A memo gives a primitive and calls nothing, so it is never a promise and
|
|
4462
|
+
* needs no render context. Each memo that it reads is one too, so no read
|
|
4463
|
+
* waits for a load. A memo that the server would not compute where it is
|
|
4464
|
+
* declared reads no property, because computing it early must not throw.
|
|
4465
|
+
* - A signal starts with a primitive, and each write gives a primitive, such
|
|
4466
|
+
* as `count++`. A signal that an element ref sets is left out.
|
|
4467
|
+
*/
|
|
4468
|
+
function plainValues(plan, blocking) {
|
|
4469
|
+
const result = /* @__PURE__ */ new Set();
|
|
4470
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
4471
|
+
const check = (declaration) => {
|
|
4472
|
+
if (result.has(declaration)) return true;
|
|
4473
|
+
if (visiting.has(declaration)) return false;
|
|
4474
|
+
visiting.add(declaration);
|
|
4475
|
+
const plain = declaration.kind === "signal" ? isPlainSignal(declaration) : isPlainMemo(declaration);
|
|
4476
|
+
visiting.delete(declaration);
|
|
4477
|
+
if (plain) result.add(declaration);
|
|
4478
|
+
return plain;
|
|
4479
|
+
};
|
|
4480
|
+
const isPlainMemo = (declaration) => {
|
|
4481
|
+
const site = declaration.site;
|
|
4482
|
+
return declaration.kind === "memo" && !declaration.writable && !declaration.optimistic && site !== void 0 && isPrimitive(site.node, (name) => readsPlain(site, name, check)) && isSafe(site.node, blocking.has(declaration)) && site.reads.every((read) => !isMemo(read.declaration) || check(read.declaration));
|
|
4483
|
+
};
|
|
4484
|
+
const isPlainSignal = (declaration) => {
|
|
4485
|
+
if (declaration.refs.length > 0) return false;
|
|
4486
|
+
const init = declaration.init;
|
|
4487
|
+
if (init) {
|
|
4488
|
+
const site = plan.graph.model.sites.find((item) => item.node === init);
|
|
4489
|
+
if (!isPrimitive(init, (name) => readsPlain(site, name, check))) return false;
|
|
4490
|
+
}
|
|
4491
|
+
return declaration.writes.every((write) => {
|
|
4492
|
+
const node = writeAt(write.site.node, write.range.start);
|
|
4493
|
+
if (node?.type === "UpdateExpression") return true;
|
|
4494
|
+
if (node?.type !== "AssignmentExpression") return false;
|
|
4495
|
+
return ARITHMETIC_ASSIGNMENTS.has(node.operator) || isPrimitive(node.right, (name) => readsPlain(write.site, name, check));
|
|
4496
|
+
});
|
|
4497
|
+
};
|
|
4498
|
+
for (const declaration of plan.graph.model.declarations) check(declaration);
|
|
4499
|
+
return result;
|
|
4500
|
+
}
|
|
4385
4501
|
//#endregion
|
|
4386
4502
|
//#region src/codegen/markup.ts
|
|
4387
4503
|
const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
@@ -4430,6 +4546,11 @@ const CLIENT_HELPERS = {
|
|
|
4430
4546
|
after: "afterInsert",
|
|
4431
4547
|
title: "title"
|
|
4432
4548
|
};
|
|
4549
|
+
/** The markup of an inserted literal, such as `{'<b>'}`, or undefined for other code. */
|
|
4550
|
+
function literalText(node) {
|
|
4551
|
+
if (node.type === "Literal") return "regex" in node && node.regex ? void 0 : insertValue(node.value);
|
|
4552
|
+
if (node.type === "TemplateLiteral" && node.expressions.length === 0) return insertValue(node.quasis[0].value.cooked);
|
|
4553
|
+
}
|
|
4433
4554
|
var Emitter = class {
|
|
4434
4555
|
lines = [];
|
|
4435
4556
|
file;
|
|
@@ -4444,6 +4565,8 @@ var Emitter = class {
|
|
|
4444
4565
|
ctxArgument;
|
|
4445
4566
|
/** Async memos that the server awaits where they are declared. */
|
|
4446
4567
|
blocking;
|
|
4568
|
+
/** Memos and signals that hold a primitive on the server, so it awaits none of them. */
|
|
4569
|
+
inline;
|
|
4447
4570
|
parts = [];
|
|
4448
4571
|
depth = 0;
|
|
4449
4572
|
/** How many of this component's elements contain the current node. */
|
|
@@ -4458,6 +4581,7 @@ var Emitter = class {
|
|
|
4458
4581
|
this.async = options.mode === "server" ? "async " : "";
|
|
4459
4582
|
this.ctxArgument = options.mode === "server" ? "$$ctx" : "undefined";
|
|
4460
4583
|
this.blocking = blockingMemos(plan);
|
|
4584
|
+
this.inline = options.mode === "server" ? plainValues(plan, this.blocking) : /* @__PURE__ */ new Set();
|
|
4461
4585
|
for (const site of plan.graph.model.sites) this.sitesByNode.set(site.node, site);
|
|
4462
4586
|
for (const declaration of plan.graph.model.declarations) for (const ref of declaration.refs) this.refTargets.set(ref.attribute, declaration);
|
|
4463
4587
|
}
|
|
@@ -4547,9 +4671,15 @@ var Emitter = class {
|
|
|
4547
4671
|
this.line(`${this.helper("slots")}(${this.scopeVar(scope)}, [${values.join(", ")}]${names});`);
|
|
4548
4672
|
}
|
|
4549
4673
|
}
|
|
4674
|
+
/** True if the server inserts a primitive, such as a memo that it computes as a plain value. */
|
|
4675
|
+
isPrimitiveInsert(expression) {
|
|
4676
|
+
if (this.options.mode !== "server") return false;
|
|
4677
|
+
const site = this.sitesByNode.get(expression.ast);
|
|
4678
|
+
return isPrimitive(expression.ast, (name) => readsPlain(site, name, (declaration) => this.inline.has(declaration)));
|
|
4679
|
+
}
|
|
4550
4680
|
/** True for an async memo that is a promise where its scope ends, on the server. */
|
|
4551
4681
|
isPromise(declaration) {
|
|
4552
|
-
return this.options.mode === "server" && isMemo(declaration) && !this.blocking.has(declaration);
|
|
4682
|
+
return this.options.mode === "server" && isMemo(declaration) && !this.blocking.has(declaration) && !this.inline.has(declaration);
|
|
4553
4683
|
}
|
|
4554
4684
|
/** Awaits the async memos of a statement that markup outside `#loading` reads. */
|
|
4555
4685
|
awaitMemos(statement) {
|
|
@@ -4558,7 +4688,7 @@ var Emitter = class {
|
|
|
4558
4688
|
const init = declarator.init;
|
|
4559
4689
|
const argument = init?.type === "CallExpression" ? init.arguments[0] : void 0;
|
|
4560
4690
|
const declaration = argument && this.sitesByNode.get(argument)?.declaration;
|
|
4561
|
-
if (declaration && isMemo(declaration) && this.blocking.has(declaration)) this.line(`const ${declaration.name} = ${this.memoValue(declaration.name)};`);
|
|
4691
|
+
if (declaration && isMemo(declaration) && this.blocking.has(declaration) && !this.inline.has(declaration)) this.line(`const ${declaration.name} = ${this.memoValue(declaration.name)};`);
|
|
4562
4692
|
}
|
|
4563
4693
|
}
|
|
4564
4694
|
emitStatements(statements, scope) {
|
|
@@ -4736,14 +4866,16 @@ var Emitter = class {
|
|
|
4736
4866
|
case "insert": {
|
|
4737
4867
|
const anchor = this.anchor(scope, node);
|
|
4738
4868
|
const at = this.options.mode === "server" ? `, $$ctx, ${this.scopeVar(instanceScope(this.plan, scope))}` : "";
|
|
4739
|
-
const value = this.ready(`$$b.insert(${this.expression(node.expression)}${at})`);
|
|
4869
|
+
const value = this.isPrimitiveInsert(node.expression) ? `$$b.insert(${this.expression(node.expression)})` : this.ready(`$$b.insert(${this.expression(node.expression)}${at})`);
|
|
4870
|
+
const text = literalText(node.expression.ast);
|
|
4871
|
+
const emitValue = () => text === void 0 ? this.code(value) : this.html(text);
|
|
4740
4872
|
if (anchor && !this.plan.contents.has(node)) {
|
|
4741
4873
|
this.html("<!--t");
|
|
4742
4874
|
this.code(anchor.scope);
|
|
4743
4875
|
this.html(`:${anchor.index}-->`);
|
|
4744
|
-
|
|
4876
|
+
emitValue();
|
|
4745
4877
|
this.html("<!--/t-->");
|
|
4746
|
-
} else
|
|
4878
|
+
} else emitValue();
|
|
4747
4879
|
return;
|
|
4748
4880
|
}
|
|
4749
4881
|
case "element":
|
|
@@ -5071,7 +5203,10 @@ var Emitter = class {
|
|
|
5071
5203
|
this.end(block);
|
|
5072
5204
|
this.line(`$$ctx.defer(${boundary}, async ($$ctx) => {`);
|
|
5073
5205
|
this.line(`let $$html = '';`);
|
|
5074
|
-
for (const declaration of awaitedMemos(this.plan, body, this.blocking))
|
|
5206
|
+
for (const declaration of awaitedMemos(this.plan, body, this.blocking)) {
|
|
5207
|
+
if (this.inline.has(declaration)) continue;
|
|
5208
|
+
this.line(`const ${declaration.name} = ${this.memoValue(declaration.name)};`);
|
|
5209
|
+
}
|
|
5075
5210
|
this.emitBody(block.body);
|
|
5076
5211
|
this.flush();
|
|
5077
5212
|
this.line("return $$html;");
|
|
@@ -5315,7 +5450,7 @@ function serverFunctions(plan) {
|
|
|
5315
5450
|
function generateServer(file, plan) {
|
|
5316
5451
|
const model = plan.graph.model;
|
|
5317
5452
|
const patch = new Patch(file.source);
|
|
5318
|
-
addValueEdits(patch, file, model.sites);
|
|
5453
|
+
addValueEdits(patch, file, model.sites, "server", plainValues(plan, blockingMemos(plan)));
|
|
5319
5454
|
addEventEdits(patch, model);
|
|
5320
5455
|
const source = (node) => patch.slice(node.start, node.end);
|
|
5321
5456
|
const header = ["import * as $$b from 'brumate/server';"];
|
|
@@ -6923,4 +7058,4 @@ function inspect(source, options = {}) {
|
|
|
6923
7058
|
//#endregion
|
|
6924
7059
|
export { summarize as A, resolveFromDisk as C, analyzeProjectProps as D, analyzeProject as E, analyze$1 as F, isWithin as I, lookup as L, buildGraph as M, BrumateError as N, forEachNode as O, getLocation as P, isReactiveModule as S, unmarked as T, getComponentName as _, CompileError as a, generateShared as b, generateClient as c, instanceScope as d, slotIndex as f, functionId as g, createPlan as h, scopeLabel as i, parse$1 as j, resolveRelative as k, generateRender as l, clientState as m, inspect as n, compile as o, blockBodies as p, report as r, DEFAULT_MERGE as s, formatReport as t, generateServer as u, hash as v, builtin as w, sharedSites as x, SHARED_QUERY as y };
|
|
6925
7060
|
|
|
6926
|
-
//# sourceMappingURL=inspect-
|
|
7061
|
+
//# sourceMappingURL=inspect-BPchdWbU.js.map
|