MCPcopy
hub / github.com/esm-dev/esm.sh / resolveExternalModule

Method resolveExternalModule

server/build_resolver.go:698–1021  ·  view source on GitHub ↗
(specifier string, kind esbuild.ResolveKind, withTypeJSON bool, analyzeMode bool)

Source from the content-addressed store, hash-verified

696}
697
698func (ctx *BuildContext) resolveExternalModule(specifier string, kind esbuild.ResolveKind, withTypeJSON bool, analyzeMode bool) (resolvedPath string, sideEffects esbuild.SideEffects, err error) {
699 if strings.HasPrefix(specifier, "file:") {
700 err = errors.New("file: protocol is not supported: " + specifier)
701 return
702 }
703
704 // return the specifier directly in analyze mode
705 if analyzeMode {
706 resolvedPath = specifier
707 return
708 }
709
710 defer func() {
711 if err == nil && !withTypeJSON {
712 resolvedPathFull := resolvedPath
713 // use relative path for sub-module of current package
714 if pkgJson := ctx.pkgJson; specifier == pkgJson.Name || strings.HasPrefix(specifier, pkgJson.Name+"/") {
715 rp, err := relPath(path.Dir(ctx.Path()), resolvedPath)
716 if err == nil {
717 resolvedPath = rp
718 }
719 }
720 if kind == esbuild.ResolveJSRequireCall {
721 ctx.cjsRequires = append(ctx.cjsRequires, [3]string{specifier, resolvedPathFull, resolvedPath})
722 resolvedPath = specifier
723 } else if kind == esbuild.ResolveJSImportStatement && !withTypeJSON {
724 ctx.esmImports = append(ctx.esmImports, [2]string{resolvedPathFull, resolvedPath})
725 }
726 }
727 }()
728
729 // if it's a http module
730 if isHttpSpecifier(specifier) {
731 resolvedPath = specifier
732 return
733 }
734
735 // check `?external`
736 packageName := toPackageName(specifier)
737 if ctx.externalAll || ctx.args.External.Has(packageName) || isPackageInExternalNamespace(packageName, ctx.args.External) {
738 resolvedPath = specifier
739 return
740 }
741
742 // if it's a node builtin module
743 if isNodeBuiltinSpecifier(specifier) {
744 if ctx.externalAll || ctx.target == "node" || ctx.target == "denonext" || ctx.args.External.Has(specifier) {
745 resolvedPath = specifier
746 } else if ctx.target == "deno" {
747 resolvedPath = fmt.Sprintf("https://deno.land/std@0.177.1/node/%s.ts", specifier[5:])
748 } else {
749 resolvedPath = fmt.Sprintf("/node/%s.mjs", specifier[5:])
750 }
751 return
752 }
753
754 // if it's `main` entry of current package
755 // Also handles scoped fork self-reference: e.g. `@scope/three` source importing `three`

Callers 1

buildModuleMethod · 0.95

Calls 15

PathMethod · 0.95
resolveEntryMethod · 0.95
getImportPathMethod · 0.95
getBuildArgsPrefixMethod · 0.95
StringMethod · 0.95
PackageIdMethod · 0.95
ContextMethod · 0.95
installMethod · 0.95
ResolveDependencyVersionFunction · 0.92
IsExactVersionFunction · 0.92
relPathFunction · 0.85
isHttpSpecifierFunction · 0.85

Tested by

no test coverage detected