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

Function buildUnenvNodeRuntime

server/unenv.go:67–221  ·  view source on GitHub ↗

slow path

()

Source from the content-addressed store, hash-verified

65
66// slow path
67func buildUnenvNodeRuntime() (err error) {
68 wd := path.Join(config.WorkDir, "npm/"+unenvPkg.String())
69 err = ensureDir(wd)
70 if err != nil {
71 return err
72 }
73
74 npmrc := &NpmRC{globalRegistry: &NpmRegistry{NpmRegistryConfig: NpmRegistryConfig{Registry: npmRegistry}}}
75 pkgJson, err := npmrc.installPackage(unenvPkg)
76 if err != nil {
77 return
78 }
79 err = npmrc.installDependencies(wd, pkgJson, false, nil)
80 if err != nil {
81 return err
82 }
83
84 endpoints := make([]esbuild.EntryPoint, 0, len(nodeBuiltinModules))
85 for name := range nodeBuiltinModules {
86 // currently the module "sys" is just a alias of "util", no need to build it
87 if name != "sys" {
88 filename := path.Join(wd, "node_modules", unenvPkg.Name+"/dist/runtime/node/"+name+".mjs")
89 if existsFile(filename) {
90 endpoints = append(endpoints, esbuild.EntryPoint{
91 InputPath: filename,
92 OutputPath: name,
93 })
94 }
95 }
96 }
97
98 ret := esbuild.Build(esbuild.BuildOptions{
99 AbsWorkingDir: wd,
100 EntryPointsAdvanced: endpoints,
101 Platform: esbuild.PlatformBrowser,
102 Format: esbuild.FormatESModule,
103 Target: esbuild.ESNext,
104 Bundle: true,
105 Splitting: true,
106 MinifyWhitespace: true,
107 MinifyIdentifiers: true,
108 MinifySyntax: true,
109 OutExtension: map[string]string{".js": ".mjs"},
110 Write: false,
111 Outdir: "/",
112 Plugins: []esbuild.Plugin{
113 {
114 Name: "resolve-node-builtin-modules",
115 Setup: func(build esbuild.PluginBuild) {
116 // https://github.com/unjs/unenv/issues/365
117 build.OnResolve(esbuild.OnResolveOptions{Filter: `^unenv/dist/runtime/node/stream$`}, func(args esbuild.OnResolveArgs) (esbuild.OnResolveResult, error) {
118 return esbuild.OnResolveResult{Path: "/node/stream.mjs", External: true}, nil
119 })
120 build.OnResolve(esbuild.OnResolveOptions{Filter: `^node:`}, func(args esbuild.OnResolveArgs) (esbuild.OnResolveResult, error) {
121 return esbuild.OnResolveResult{Path: "/node/" + args.Path[5:] + ".mjs", External: true}, nil
122 })
123 },
124 },

Callers 1

loadNodeRuntimeFunction · 0.85

Calls 10

installPackageMethod · 0.95
installDependenciesMethod · 0.95
existsFileFunction · 0.85
BuildMethod · 0.80
WriteHeaderMethod · 0.80
WriteMethod · 0.80
ensureDirFunction · 0.70
isRelPathSpecifierFunction · 0.70
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected