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

Function cjsModuleLexer

server/cjs_module_lexer.go:52–184  ·  view source on GitHub ↗
(b *BuildContext, cjsEntry string)

Source from the content-addressed store, hash-verified

50}
51
52func cjsModuleLexer(b *BuildContext, cjsEntry string) (ret cjsModuleLexerResult, err error) {
53 h := sha1.New()
54 h.Write([]byte(cjsModuleLexerVersion))
55 h.Write([]byte(cjsEntry))
56 h.Write([]byte(b.getNodeEnv()))
57 cacheFileName := path.Join(b.wd, ".cjs-module-lexer", base64.RawURLEncoding.EncodeToString(h.Sum(nil))+".json")
58
59 // check the cache first
60 if existsFile(cacheFileName) && utils.ParseJSONFile(cacheFileName, &ret) == nil {
61 return
62 }
63
64 start := time.Now()
65 defer func() {
66 if err == nil {
67 if DEBUG {
68 b.logger.Debugf("[cjsModuleLexer] parse %s in %s", path.Join(b.esmPath.PkgName, cjsEntry), time.Since(start))
69 }
70 if !existsFile(cacheFileName) {
71 ensureDir(path.Dir(cacheFileName))
72 utils.WriteJSONFile(cacheFileName, ret, "")
73 }
74 }
75 }()
76
77 if cjsModuleLexerIgnoredPackages.Has(b.esmPath.PkgName) {
78 denoPath := deno.ResolveDenoPath(config.WorkDir)
79 err = doOnce("check-deno", func() (err error) {
80 return deno.CheckDenoPath(denoPath)
81 })
82 if err != nil {
83 return
84 }
85 js := path.Join(b.wd, "reveal_"+strings.ReplaceAll(cjsEntry[2:], "/", "_"))
86 err = os.WriteFile(js, fmt.Appendf(nil, `console.log(JSON.stringify(Object.keys((await import("npm:%s")).default)))`, path.Join(b.esmPath.PackageId(), cjsEntry)), 0644)
87 if err != nil {
88 return
89 }
90 cancelCtx, cancel := context.WithTimeout(b.Context(), time.Minute)
91 defer cancel()
92 cmd := exec.CommandContext(cancelCtx,
93 denoPath,
94 "run",
95 "--allow-env",
96 "--no-prompt",
97 "--no-config",
98 "--no-lock",
99 "--quiet",
100 js)
101 cmd.Env = append(os.Environ(), "DENO_NO_UPDATE_CHECK=1")
102 var data []byte
103 data, err = cmd.Output()
104 if err != nil {
105 err = errors.New("cjsModuleLexer(fallback mode): " + err.Error())
106 return
107 }
108 var namedExports []string
109 err = json.Unmarshal(data, &namedExports)

Callers 1

lexerMethod · 0.85

Calls 15

ResolveDenoPathFunction · 0.92
CheckDenoPathFunction · 0.92
existsFileFunction · 0.85
doOnceFunction · 0.85
isJsReservedWordFunction · 0.85
isJsIdentifierFunction · 0.85
WriteMethod · 0.80
getNodeEnvMethod · 0.80
HasMethod · 0.80
PackageIdMethod · 0.80
ContextMethod · 0.80

Tested by

no test coverage detected