wesl 0.6.47 → 0.6.49

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/index.d.ts CHANGED
@@ -689,6 +689,8 @@ declare class BundleResolver implements ModuleResolver {
689
689
  private moduleToFilePath;
690
690
  private modulePathToDebugPath;
691
691
  }
692
+ /** Convert file path to module path (e.g., "foo/bar.wesl" -> "package::foo::bar"). */
693
+ declare function fileToModulePath(filePath: string, packageName: string, treatLibAsRoot: boolean): string;
692
694
  //#endregion
693
695
  //#region src/Linker.d.ts
694
696
  type LinkerTransform = (boundAST: TransformedAST) => TransformedAST;
@@ -980,6 +982,31 @@ declare function sanitizePackageName(npmName: string): string;
980
982
  */
981
983
  declare function npmNameVariations(sanitizedPath: string): Generator<string>;
982
984
  //#endregion
985
+ //#region src/ModulePathUtil.d.ts
986
+ /** WESL module path utilities for converting between :: and / separators. */
987
+ /**
988
+ * Convert module path segments to relative file path.
989
+ * Handles package/packageName prefixes and super:: resolution.
990
+ *
991
+ * @param parts - module path as array e.g., ["package", "utils", "helper"]
992
+ * @param packageName - the current package's name (required)
993
+ * @param srcModuleParts - source module path for super:: resolution (optional)
994
+ * @returns relative file path e.g., "utils/helper", or undefined if external
995
+ */
996
+ declare function modulePartsToRelativePath(parts: string[], packageName: string, srcModuleParts?: string[]): string | undefined;
997
+ /** String variant of modulePartsToRelativePath. */
998
+ declare function moduleToRelativePath(modulePath: string, packageName: string, srcModulePath?: string): string | undefined;
999
+ /**
1000
+ * Resolve super:: elements to absolute module path.
1001
+ *
1002
+ * @param parts - module path with potential super:: elements
1003
+ * @param srcModuleParts - source module path for context
1004
+ * @returns absolute module path parts (no super:: elements)
1005
+ */
1006
+ declare function resolveSuper(parts: string[], srcModuleParts: string[]): string[];
1007
+ /** Normalize debug root to end with / or be empty. */
1008
+ declare function normalizeDebugRoot(root?: string): string;
1009
+ //#endregion
983
1010
  //#region src/PathUtil.d.ts
984
1011
  /** simplistic path manipulation utilities */
985
1012
  /** return path with ./ and foo/.. elements removed */
@@ -1119,4 +1146,4 @@ declare function offsetToLineNumber(offset: number, text: string): [lineNum: num
1119
1146
  */
1120
1147
  declare function errorHighlight(source: string, span: Span): [string, string];
1121
1148
  //#endregion
1122
- export { AbstractElem, AbstractElemBase, AliasElem, Attribute, AttributeElem, BatchModuleResolver, BinaryExpression, BinaryOperator, BindIdentsParams, BindResults, BindingAST, BindingStructElem, BoundAndTransformed, BuiltinAttribute, BundleResolver, ComponentExpression, ComponentMemberExpression, CompositeResolver, Conditions, ConstAssertElem, ConstElem, ContainerElem, DeclIdent, DeclIdentElem, DeclarationElem, DiagnosticAttribute, DiagnosticDirective, DirectiveElem, DirectiveVariant, ElemWithAttributes, ElemWithContentsBase, ElifAttribute, ElseAttribute, EmittableElem, EnableDirective, ExpressionElem, ExtendedGPUValidationError, FnElem, FnParamElem, FunctionCallExpression, GlobalDeclarationElem, GlobalVarElem, GrammarElem, HasAttributes, Ident, IfAttribute, ImportCollection, ImportElem, ImportItem, ImportSegment, ImportStatement, InterpolateAttribute, LetElem, LexicalScope, LinkConfig, LinkParams, LinkRegistryParams, LinkedWesl, LinkerTransform, Literal, LiveDecls, ManglerFn, ModuleElem, ModuleResolver, NameElem, OverrideElem, ParenthesizedExpression, PartialScope, RecordResolver, RecordResolverOptions, RefIdent, RefIdentElem, RequiresDirective, Scope, SimpleMemberRef, SrcModule, StableState, StandardAttribute, StatementElem, StructElem, StructMemberElem, StuffElem, SwitchClauseElem, SyntheticElem, TerminalElem, TextElem, TransformedAST, TranslateTimeExpressionElem, TranslateTimeFeature, TypeRefElem, TypeTemplateParameter, TypedDeclElem, UnaryExpression, UnaryOperator, UnknownExpressionElem, VarElem, VirtualLibrary, VirtualLibraryFn, VirtualLibrarySet, WeslAST, WeslBundle, WeslDevice, WeslGPUCompilationInfo, WeslGPUCompilationMessage, WeslJsPlugin, WeslParseContext, WeslParseError, WeslParseState, WeslStream, _linkSync, astToString, attributeToString, bindAndTransform, bindIdents, bindIdentsRecursive, bindingStructsPlugin, blankWeslParseState, childIdent, childScope, containsScope, debugContentsToString, emptyScope, errorHighlight, filterMap, findMap, findRefsToBindingStructs, findUnboundIdents, findValidRootDecls, flatImports, groupBy, grouped, identToString, isGlobal, last, lengthPrefixMangle, link, linkRegistry, liveDeclsToString, lowerBindingStructs, makeLiveDecls, makeWeslDevice, mapForward, mapValues, markBindingStructs, markEntryTypes, mergeScope, minimalMangle, minimallyMangledName, multiKeySet, nextIdentId, noSuffix, normalize, normalizeModuleName, npmNameVariations, offsetToLineNumber, overlapTail, parseSrcModule, partition, publicDecl, replaceWords, requestWeslDevice, resetScopeIds, sanitizePackageName, scan, scopeToString, scopeToStringLong, syntheticWeslParseState, transformBindingReference, transformBindingStruct, underscoreMangle };
1149
+ export { AbstractElem, AbstractElemBase, AliasElem, Attribute, AttributeElem, BatchModuleResolver, BinaryExpression, BinaryOperator, BindIdentsParams, BindResults, BindingAST, BindingStructElem, BoundAndTransformed, BuiltinAttribute, BundleResolver, ComponentExpression, ComponentMemberExpression, CompositeResolver, Conditions, ConstAssertElem, ConstElem, ContainerElem, DeclIdent, DeclIdentElem, DeclarationElem, DiagnosticAttribute, DiagnosticDirective, DirectiveElem, DirectiveVariant, ElemWithAttributes, ElemWithContentsBase, ElifAttribute, ElseAttribute, EmittableElem, EnableDirective, ExpressionElem, ExtendedGPUValidationError, FnElem, FnParamElem, FunctionCallExpression, GlobalDeclarationElem, GlobalVarElem, GrammarElem, HasAttributes, Ident, IfAttribute, ImportCollection, ImportElem, ImportItem, ImportSegment, ImportStatement, InterpolateAttribute, LetElem, LexicalScope, LinkConfig, LinkParams, LinkRegistryParams, LinkedWesl, LinkerTransform, Literal, LiveDecls, ManglerFn, ModuleElem, ModuleResolver, NameElem, OverrideElem, ParenthesizedExpression, PartialScope, RecordResolver, RecordResolverOptions, RefIdent, RefIdentElem, RequiresDirective, Scope, SimpleMemberRef, SrcModule, StableState, StandardAttribute, StatementElem, StructElem, StructMemberElem, StuffElem, SwitchClauseElem, SyntheticElem, TerminalElem, TextElem, TransformedAST, TranslateTimeExpressionElem, TranslateTimeFeature, TypeRefElem, TypeTemplateParameter, TypedDeclElem, UnaryExpression, UnaryOperator, UnknownExpressionElem, VarElem, VirtualLibrary, VirtualLibraryFn, VirtualLibrarySet, WeslAST, WeslBundle, WeslDevice, WeslGPUCompilationInfo, WeslGPUCompilationMessage, WeslJsPlugin, WeslParseContext, WeslParseError, WeslParseState, WeslStream, _linkSync, astToString, attributeToString, bindAndTransform, bindIdents, bindIdentsRecursive, bindingStructsPlugin, blankWeslParseState, childIdent, childScope, containsScope, debugContentsToString, emptyScope, errorHighlight, fileToModulePath, filterMap, findMap, findRefsToBindingStructs, findUnboundIdents, findValidRootDecls, flatImports, groupBy, grouped, identToString, isGlobal, last, lengthPrefixMangle, link, linkRegistry, liveDeclsToString, lowerBindingStructs, makeLiveDecls, makeWeslDevice, mapForward, mapValues, markBindingStructs, markEntryTypes, mergeScope, minimalMangle, minimallyMangledName, modulePartsToRelativePath, moduleToRelativePath, multiKeySet, nextIdentId, noSuffix, normalize, normalizeDebugRoot, normalizeModuleName, npmNameVariations, offsetToLineNumber, overlapTail, parseSrcModule, partition, publicDecl, replaceWords, requestWeslDevice, resetScopeIds, resolveSuper, sanitizePackageName, scan, scopeToString, scopeToStringLong, syntheticWeslParseState, transformBindingReference, transformBindingStruct, underscoreMangle };
package/dist/index.js CHANGED
@@ -1024,6 +1024,47 @@ function minimallyMangledName(proposedName, globalNames) {
1024
1024
  return renamed;
1025
1025
  }
1026
1026
 
1027
+ //#endregion
1028
+ //#region src/ModulePathUtil.ts
1029
+ /** WESL module path utilities for converting between :: and / separators. */
1030
+ /**
1031
+ * Convert module path segments to relative file path.
1032
+ * Handles package/packageName prefixes and super:: resolution.
1033
+ *
1034
+ * @param parts - module path as array e.g., ["package", "utils", "helper"]
1035
+ * @param packageName - the current package's name (required)
1036
+ * @param srcModuleParts - source module path for super:: resolution (optional)
1037
+ * @returns relative file path e.g., "utils/helper", or undefined if external
1038
+ */
1039
+ function modulePartsToRelativePath(parts, packageName, srcModuleParts) {
1040
+ const resolved = srcModuleParts ? resolveSuper(parts, srcModuleParts) : parts;
1041
+ const rootSegment = resolved[0];
1042
+ if (rootSegment === "package" || rootSegment === packageName) return resolved.slice(1).join("/");
1043
+ }
1044
+ /** String variant of modulePartsToRelativePath. */
1045
+ function moduleToRelativePath(modulePath, packageName, srcModulePath) {
1046
+ const srcParts = srcModulePath?.split("::");
1047
+ return modulePartsToRelativePath(modulePath.split("::"), packageName, srcParts);
1048
+ }
1049
+ /**
1050
+ * Resolve super:: elements to absolute module path.
1051
+ *
1052
+ * @param parts - module path with potential super:: elements
1053
+ * @param srcModuleParts - source module path for context
1054
+ * @returns absolute module path parts (no super:: elements)
1055
+ */
1056
+ function resolveSuper(parts, srcModuleParts) {
1057
+ const lastSuper = parts.lastIndexOf("super");
1058
+ if (lastSuper === -1) return parts;
1059
+ return [...srcModuleParts.slice(0, -(lastSuper + 1)), ...parts.slice(lastSuper + 1)];
1060
+ }
1061
+ /** Normalize debug root to end with / or be empty. */
1062
+ function normalizeDebugRoot(root) {
1063
+ if (root === void 0) return "./";
1064
+ if (root === "") return "";
1065
+ return root.endsWith("/") ? root : root + "/";
1066
+ }
1067
+
1027
1068
  //#endregion
1028
1069
  //#region src/FlattenTreeImport.ts
1029
1070
  /**
@@ -2544,7 +2585,7 @@ function matchingImport(identParts, flatImports$1) {
2544
2585
  }
2545
2586
  /** @return an exported root declIdent for the provided path */
2546
2587
  function findExport(modulePathParts, srcModule, resolver, conditions = {}, virtuals) {
2547
- const modulePath = absoluteModulePath(modulePathParts, srcModule).slice(0, -1).join("::");
2588
+ const modulePath = resolveSuper(modulePathParts, srcModule.modulePath.split("::")).slice(0, -1).join("::");
2548
2589
  const moduleAst = resolver.resolveModule(modulePath) ?? virtualModule(modulePathParts[0], conditions, virtuals);
2549
2590
  if (!moduleAst) return void 0;
2550
2591
  const name$1 = last(modulePathParts);
@@ -2609,14 +2650,6 @@ function stdWgsl(name$1) {
2609
2650
  function qualifiedIdent(identParts) {
2610
2651
  if (identParts.length > 1) return identParts;
2611
2652
  }
2612
- /** Convert a module path with super:: elements to one with no super:: elements */
2613
- function absoluteModulePath(modulePathParts, srcModule) {
2614
- const lastSuper = modulePathParts.lastIndexOf("super");
2615
- if (lastSuper === -1) return modulePathParts;
2616
- const base = srcModule.modulePath.split("::").slice(0, -(lastSuper + 1));
2617
- const noSupers = modulePathParts.slice(lastSuper + 1);
2618
- return [...base, ...noSupers];
2619
- }
2620
2653
  /** Collect all declarations in a scope (used when scope is already validated) */
2621
2654
  function collectDecls(scope, found) {
2622
2655
  for (const item of scope.contents) if (item.kind === "decl") found.push(item);
@@ -2884,9 +2917,7 @@ var RecordResolver = class {
2884
2917
  return findInVariants(this.sources, filePath);
2885
2918
  }
2886
2919
  moduleToFilePath(modulePath) {
2887
- const parts = modulePath.split("::");
2888
- if (parts[0] !== this.packageName && parts[0] !== "package") return modulePath;
2889
- return parts.slice(1).join("/");
2920
+ return moduleToRelativePath(modulePath, this.packageName) ?? modulePath;
2890
2921
  }
2891
2922
  modulePathToDebugPath(modulePath) {
2892
2923
  const filePath = this.moduleToFilePath(modulePath);
@@ -2895,7 +2926,8 @@ var RecordResolver = class {
2895
2926
  /** Return all modules, parsing them on-demand if needed. */
2896
2927
  allModules() {
2897
2928
  for (const filePath of Object.keys(this.sources)) {
2898
- const modulePath = fileToModulePath(filePath, this.packageName, this.packageName !== "package");
2929
+ const treatLibAsRoot = this.packageName !== "package";
2930
+ const modulePath = fileToModulePath(filePath, this.packageName, treatLibAsRoot);
2899
2931
  this.resolveModule(modulePath);
2900
2932
  }
2901
2933
  return this.astCache.entries();
@@ -2926,7 +2958,8 @@ var BundleResolver = class {
2926
2958
  this.debugWeslRoot = normalizeDebugRoot(debugWeslRoot);
2927
2959
  }
2928
2960
  resolveModule(modulePath) {
2929
- if (modulePath !== this.packageName && !modulePath.startsWith(this.packageName + "::")) return;
2961
+ const pkgPrefix = this.packageName + "::";
2962
+ if (modulePath !== this.packageName && !modulePath.startsWith(pkgPrefix)) return;
2930
2963
  const cached = this.astCache.get(modulePath);
2931
2964
  if (cached) return cached;
2932
2965
  const source = this.findSource(modulePath);
@@ -2948,9 +2981,7 @@ var BundleResolver = class {
2948
2981
  return findInVariants(this.sources, filePath);
2949
2982
  }
2950
2983
  moduleToFilePath(modulePath) {
2951
- const parts = modulePath.split("::");
2952
- if (parts[0] !== this.packageName) return modulePath;
2953
- return parts.slice(1).join("/");
2984
+ return moduleToRelativePath(modulePath, this.packageName) ?? modulePath;
2954
2985
  }
2955
2986
  modulePathToDebugPath(modulePath) {
2956
2987
  const filePath = this.moduleToFilePath(modulePath);
@@ -2964,12 +2995,6 @@ function fileToModulePath(filePath, packageName, treatLibAsRoot) {
2964
2995
  const moduleSuffix = noSuffix(normalize(filePath)).replaceAll("/", "::");
2965
2996
  return packageName + "::" + moduleSuffix;
2966
2997
  }
2967
- /** Normalize debug root to end with / or be empty. */
2968
- function normalizeDebugRoot(debugWeslRoot) {
2969
- if (debugWeslRoot === void 0) return "./";
2970
- if (debugWeslRoot === "") return "";
2971
- return debugWeslRoot.endsWith("/") ? debugWeslRoot : debugWeslRoot + "/";
2972
- }
2973
2998
  /** Try path variants with and without ./ prefix and extension suffixes. */
2974
2999
  function findInVariants(sources, basePath, extensions = ["wesl", "wgsl"]) {
2975
3000
  for (const prefix of ["", "./"]) {
@@ -3486,4 +3511,4 @@ function makeWeslDevice(device) {
3486
3511
  }
3487
3512
 
3488
3513
  //#endregion
3489
- export { BundleResolver, CompositeResolver, LinkedWesl, RecordResolver, WeslParseError, WeslStream, _linkSync, astToString, attributeToString, bindAndTransform, bindIdents, bindIdentsRecursive, bindingStructsPlugin, blankWeslParseState, childIdent, childScope, containsScope, debugContentsToString, emptyScope, errorHighlight, filterMap, findMap, findRefsToBindingStructs, findUnboundIdents, findValidRootDecls, flatImports, groupBy, grouped, identToString, isGlobal, last, lengthPrefixMangle, link, linkRegistry, liveDeclsToString, lowerBindingStructs, makeLiveDecls, makeWeslDevice, mapForward, mapValues, markBindingStructs, markEntryTypes, mergeScope, minimalMangle, minimallyMangledName, multiKeySet, nextIdentId, noSuffix, normalize, normalizeModuleName, npmNameVariations, offsetToLineNumber, overlapTail, parseSrcModule, partition, publicDecl, replaceWords, requestWeslDevice, resetScopeIds, sanitizePackageName, scan, scopeToString, scopeToStringLong, syntheticWeslParseState, transformBindingReference, transformBindingStruct, underscoreMangle };
3514
+ export { BundleResolver, CompositeResolver, LinkedWesl, RecordResolver, WeslParseError, WeslStream, _linkSync, astToString, attributeToString, bindAndTransform, bindIdents, bindIdentsRecursive, bindingStructsPlugin, blankWeslParseState, childIdent, childScope, containsScope, debugContentsToString, emptyScope, errorHighlight, fileToModulePath, filterMap, findMap, findRefsToBindingStructs, findUnboundIdents, findValidRootDecls, flatImports, groupBy, grouped, identToString, isGlobal, last, lengthPrefixMangle, link, linkRegistry, liveDeclsToString, lowerBindingStructs, makeLiveDecls, makeWeslDevice, mapForward, mapValues, markBindingStructs, markEntryTypes, mergeScope, minimalMangle, minimallyMangledName, modulePartsToRelativePath, moduleToRelativePath, multiKeySet, nextIdentId, noSuffix, normalize, normalizeDebugRoot, normalizeModuleName, npmNameVariations, offsetToLineNumber, overlapTail, parseSrcModule, partition, publicDecl, replaceWords, requestWeslDevice, resetScopeIds, resolveSuper, sanitizePackageName, scan, scopeToString, scopeToStringLong, syntheticWeslParseState, transformBindingReference, transformBindingStruct, underscoreMangle };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wesl",
3
- "version": "0.6.47",
3
+ "version": "0.6.49",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -11,6 +11,9 @@
11
11
  ".": {
12
12
  "types": "./dist/index.d.ts",
13
13
  "import": "./dist/index.js"
14
+ },
15
+ "./bundle-decl": {
16
+ "import": "./src/weslBundleDeclUrl.ts"
14
17
  }
15
18
  },
16
19
  "types": "dist/index.d.ts",
package/src/BindIdents.ts CHANGED
@@ -7,6 +7,7 @@ import type { FlatImport } from "./FlattenTreeImport.ts";
7
7
  import type { LinkRegistryParams, VirtualLibraryFn } from "./Linker.ts";
8
8
  import { type LiveDecls, makeLiveDecls } from "./LiveDeclarations.ts";
9
9
  import { type ManglerFn, minimalMangle } from "./Mangler.ts";
10
+ import { resolveSuper } from "./ModulePathUtil.ts";
10
11
  import type { ModuleResolver } from "./ModuleResolver.ts";
11
12
  import { flatImports, parseSrcModule, type WeslAST } from "./ParseWESL.ts";
12
13
  import type {
@@ -485,7 +486,8 @@ function findExport(
485
486
  conditions: Conditions = {},
486
487
  virtuals?: VirtualLibrarySet,
487
488
  ): FoundDecl | undefined {
488
- const fqPathParts = absoluteModulePath(modulePathParts, srcModule);
489
+ const srcModuleParts = srcModule.modulePath.split("::");
490
+ const fqPathParts = resolveSuper(modulePathParts, srcModuleParts);
489
491
  const modulePath = fqPathParts.slice(0, -1).join("::");
490
492
  const moduleAst =
491
493
  resolver.resolveModule(modulePath) ??
@@ -586,20 +588,6 @@ function qualifiedIdent(identParts: string[]): string[] | undefined {
586
588
  if (identParts.length > 1) return identParts;
587
589
  }
588
590
 
589
- /** Convert a module path with super:: elements to one with no super:: elements */
590
- function absoluteModulePath(
591
- modulePathParts: string[],
592
- srcModule: SrcModule,
593
- ): string[] {
594
- const lastSuper = modulePathParts.lastIndexOf("super");
595
- if (lastSuper === -1) return modulePathParts;
596
-
597
- const srcModuleParts = srcModule.modulePath.split("::");
598
- const base = srcModuleParts.slice(0, -(lastSuper + 1));
599
- const noSupers = modulePathParts.slice(lastSuper + 1);
600
- return [...base, ...noSupers];
601
- }
602
-
603
591
  /** Collect all declarations in a scope (used when scope is already validated) */
604
592
  function collectDecls(scope: Scope, found: DeclIdent[]): void {
605
593
  for (const item of scope.contents) {
@@ -0,0 +1,59 @@
1
+ /** WESL module path utilities for converting between :: and / separators. */
2
+
3
+ /**
4
+ * Convert module path segments to relative file path.
5
+ * Handles package/packageName prefixes and super:: resolution.
6
+ *
7
+ * @param parts - module path as array e.g., ["package", "utils", "helper"]
8
+ * @param packageName - the current package's name (required)
9
+ * @param srcModuleParts - source module path for super:: resolution (optional)
10
+ * @returns relative file path e.g., "utils/helper", or undefined if external
11
+ */
12
+ export function modulePartsToRelativePath(
13
+ parts: string[],
14
+ packageName: string,
15
+ srcModuleParts?: string[],
16
+ ): string | undefined {
17
+ const resolved = srcModuleParts ? resolveSuper(parts, srcModuleParts) : parts;
18
+
19
+ const rootSegment = resolved[0];
20
+ if (rootSegment === "package" || rootSegment === packageName) {
21
+ return resolved.slice(1).join("/");
22
+ }
23
+ return undefined;
24
+ }
25
+
26
+ /** String variant of modulePartsToRelativePath. */
27
+ export function moduleToRelativePath(
28
+ modulePath: string,
29
+ packageName: string,
30
+ srcModulePath?: string,
31
+ ): string | undefined {
32
+ const srcParts = srcModulePath?.split("::");
33
+ const parts = modulePath.split("::");
34
+ return modulePartsToRelativePath(parts, packageName, srcParts);
35
+ }
36
+
37
+ /**
38
+ * Resolve super:: elements to absolute module path.
39
+ *
40
+ * @param parts - module path with potential super:: elements
41
+ * @param srcModuleParts - source module path for context
42
+ * @returns absolute module path parts (no super:: elements)
43
+ */
44
+ export function resolveSuper(
45
+ parts: string[],
46
+ srcModuleParts: string[],
47
+ ): string[] {
48
+ const lastSuper = parts.lastIndexOf("super");
49
+ if (lastSuper === -1) return parts;
50
+ const base = srcModuleParts.slice(0, -(lastSuper + 1));
51
+ return [...base, ...parts.slice(lastSuper + 1)];
52
+ }
53
+
54
+ /** Normalize debug root to end with / or be empty. */
55
+ export function normalizeDebugRoot(root?: string): string {
56
+ if (root === undefined) return "./";
57
+ if (root === "") return "";
58
+ return root.endsWith("/") ? root : root + "/";
59
+ }
@@ -1,9 +1,8 @@
1
+ import { moduleToRelativePath, normalizeDebugRoot } from "./ModulePathUtil.ts";
1
2
  import { parseSrcModule, type WeslAST } from "./ParseWESL.ts";
2
3
  import { normalize, noSuffix } from "./PathUtil.ts";
3
4
  import type { WeslBundle } from "./WeslBundle.ts";
4
5
 
5
- const libRegex = /^lib\.w[eg]sl$/i;
6
-
7
6
  /**
8
7
  * Resolves module paths to parsed ASTs.
9
8
  *
@@ -35,6 +34,8 @@ export interface RecordResolverOptions {
35
34
  debugWeslRoot?: string;
36
35
  }
37
36
 
37
+ const libRegex = /^lib\.w[eg]sl$/i;
38
+
38
39
  /** Module resolver for in-memory source records. Lazy by default. */
39
40
  export class RecordResolver implements BatchModuleResolver {
40
41
  readonly astCache = new Map<string, WeslAST>();
@@ -60,12 +61,7 @@ export class RecordResolver implements BatchModuleResolver {
60
61
  if (!source) return undefined;
61
62
 
62
63
  const debugFilePath = this.modulePathToDebugPath(modulePath);
63
-
64
- const ast = parseSrcModule({
65
- modulePath,
66
- debugFilePath,
67
- src: source,
68
- });
64
+ const ast = parseSrcModule({ modulePath, debugFilePath, src: source });
69
65
  this.astCache.set(modulePath, ast);
70
66
  return ast;
71
67
  }
@@ -78,13 +74,7 @@ export class RecordResolver implements BatchModuleResolver {
78
74
  }
79
75
 
80
76
  private moduleToFilePath(modulePath: string): string {
81
- const parts = modulePath.split("::");
82
- if (parts[0] !== this.packageName && parts[0] !== "package") {
83
- return modulePath;
84
- }
85
-
86
- const pathParts = parts.slice(1);
87
- return pathParts.join("/");
77
+ return moduleToRelativePath(modulePath, this.packageName) ?? modulePath;
88
78
  }
89
79
 
90
80
  private modulePathToDebugPath(modulePath: string): string {
@@ -95,10 +85,11 @@ export class RecordResolver implements BatchModuleResolver {
95
85
  /** Return all modules, parsing them on-demand if needed. */
96
86
  allModules(): Iterable<[string, WeslAST]> {
97
87
  for (const filePath of Object.keys(this.sources)) {
88
+ const treatLibAsRoot = this.packageName !== "package";
98
89
  const modulePath = fileToModulePath(
99
90
  filePath,
100
91
  this.packageName,
101
- this.packageName !== "package",
92
+ treatLibAsRoot,
102
93
  );
103
94
  this.resolveModule(modulePath);
104
95
  }
@@ -137,10 +128,8 @@ export class BundleResolver implements ModuleResolver {
137
128
  }
138
129
 
139
130
  resolveModule(modulePath: string): WeslAST | undefined {
140
- if (
141
- modulePath !== this.packageName &&
142
- !modulePath.startsWith(this.packageName + "::")
143
- ) {
131
+ const pkgPrefix = this.packageName + "::";
132
+ if (modulePath !== this.packageName && !modulePath.startsWith(pkgPrefix)) {
144
133
  return undefined;
145
134
  }
146
135
 
@@ -151,11 +140,7 @@ export class BundleResolver implements ModuleResolver {
151
140
  if (!source) return undefined;
152
141
 
153
142
  const debugFilePath = this.modulePathToDebugPath(modulePath);
154
- const ast = parseSrcModule({
155
- modulePath,
156
- debugFilePath,
157
- src: source,
158
- });
143
+ const ast = parseSrcModule({ modulePath, debugFilePath, src: source });
159
144
  this.astCache.set(modulePath, ast);
160
145
  return ast;
161
146
  }
@@ -173,11 +158,7 @@ export class BundleResolver implements ModuleResolver {
173
158
  }
174
159
 
175
160
  private moduleToFilePath(modulePath: string): string {
176
- const parts = modulePath.split("::");
177
- if (parts[0] !== this.packageName) {
178
- return modulePath;
179
- }
180
- return parts.slice(1).join("/");
161
+ return moduleToRelativePath(modulePath, this.packageName) ?? modulePath;
181
162
  }
182
163
 
183
164
  private modulePathToDebugPath(modulePath: string): string {
@@ -187,7 +168,7 @@ export class BundleResolver implements ModuleResolver {
187
168
  }
188
169
 
189
170
  /** Convert file path to module path (e.g., "foo/bar.wesl" -> "package::foo::bar"). */
190
- function fileToModulePath(
171
+ export function fileToModulePath(
191
172
  filePath: string,
192
173
  packageName: string,
193
174
  treatLibAsRoot: boolean,
@@ -204,13 +185,6 @@ function fileToModulePath(
204
185
  return packageName + "::" + moduleSuffix;
205
186
  }
206
187
 
207
- /** Normalize debug root to end with / or be empty. */
208
- function normalizeDebugRoot(debugWeslRoot?: string): string {
209
- if (debugWeslRoot === undefined) return "./";
210
- if (debugWeslRoot === "") return "";
211
- return debugWeslRoot.endsWith("/") ? debugWeslRoot : debugWeslRoot + "/";
212
- }
213
-
214
188
  /** Try path variants with and without ./ prefix and extension suffixes. */
215
189
  function findInVariants(
216
190
  sources: Record<string, string>,
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./LinkedWesl.ts";
8
8
  export * from "./Linker.ts";
9
9
  export * from "./LiveDeclarations.ts";
10
10
  export * from "./Mangler.ts";
11
+ export * from "./ModulePathUtil.ts";
11
12
  export * from "./ModuleResolver.ts";
12
13
  export * from "./ParseWESL.ts";
13
14
  export * from "./PathUtil.ts";
@@ -0,0 +1,2 @@
1
+ export const weslBundleDeclUrl = new URL("./WeslBundle.ts", import.meta.url)
2
+ .href;