Skip to content

Commit 85207a7

Browse files
authored
feat: support keepNames and ignoreAnnotations options of esm.sh field in package.json (esm-dev#1341)
1 parent ed1319d commit 85207a7

5 files changed

Lines changed: 47 additions & 17 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ import unescape from "https://esm.sh/lodash/unescape?no-dts";
382382
This will prevent the `X-TypeScript-Types` header from being included in the network request, and you can manually
383383
specify the types for the imported module.
384384

385+
## esm.sh Configuration
386+
387+
esm.sh supports configuring the build options by adding the `esm.sh` field to your `package.json`:
388+
389+
```jsonc
390+
{
391+
"name": "your-package",
392+
"esm.sh": {
393+
// set to false to disable the default bundling behavior
394+
"bundle": true,
395+
// set to true to prevent class/function names erasing
396+
"keepNames": false,
397+
// set to true to ignore side-effect annotations
398+
"ignoreAnnotations": false
399+
}
400+
}
401+
```
402+
385403
## Supporting Node.js/Bun
386404

387405
esm.sh is not supported by Node.js/Bun currently.

internal/npm/package_json.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ type PackageJSON struct {
5656
Main string
5757
Module string
5858
Types string
59-
Typings string
6059
SideEffectsFalse bool
6160
SideEffects set.ReadOnlySet[string]
6261
Browser map[string]string
@@ -173,7 +172,6 @@ func (a *PackageJSONRaw) ToNpmPackage() *PackageJSON {
173172
Main: a.Main.String(),
174173
Module: a.Module.String(),
175174
Types: a.Types.String(),
176-
Typings: a.Typings.String(),
177175
Browser: browser,
178176
SideEffectsFalse: sideEffectsFalse,
179177
SideEffects: *sideEffects.ReadOnly(),
@@ -187,6 +185,10 @@ func (a *PackageJSONRaw) ToNpmPackage() *PackageJSON {
187185
Dist: dist,
188186
}
189187

188+
if p.Types == "" {
189+
p.Types = a.Typings.String()
190+
}
191+
190192
// normalize package module field
191193
if p.Module == "" {
192194
if es2015 := a.ES2015.String(); es2015 != "" {

server/build.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (ctx *BuildContext) Build(buildCtx context.Context) (meta *BuildMeta, err e
170170
}
171171

172172
// analyze splitting modules if bundling
173-
if ctx.pkgJson.Exports.Len() > 1 && !ctx.isNoBundle() {
173+
if ctx.pkgJson.Exports.Len() > 1 && ctx.shouldBundle() {
174174
ctx.status = "analyze"
175175
err = ctx.analyzeSplitting()
176176
if err != nil {
@@ -400,7 +400,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
400400

401401
browserExclude := map[string]*set.Set[string]{}
402402
implicitExternal := set.New[string]()
403-
noBundle := ctx.isNoBundle()
403+
noBundle := !ctx.shouldBundle()
404404
esmifyPlugin := esbuild.Plugin{
405405
Name: "esmify",
406406
Setup: func(build esbuild.PluginBuild) {
@@ -1083,6 +1083,20 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
10831083
// disable minification for development build
10841084
minify = false
10851085
}
1086+
keepNames := ctx.args.KeepNames
1087+
ignoreAnnotations := ctx.args.IgnoreAnnotations
1088+
if esmsh := ctx.pkgJson.Esmsh; esmsh != nil {
1089+
if v, ok := esmsh["keepNames"]; ok {
1090+
if b, ok := v.(bool); ok {
1091+
keepNames = b
1092+
}
1093+
}
1094+
if v, ok := esmsh["ignoreAnnotations"]; ok {
1095+
if b, ok := v.(bool); ok {
1096+
ignoreAnnotations = b
1097+
}
1098+
}
1099+
}
10861100
options := esbuild.BuildOptions{
10871101
AbsWorkingDir: ctx.wd,
10881102
PreserveSymlinks: true,
@@ -1097,8 +1111,8 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
10971111
MinifyWhitespace: minify,
10981112
MinifyIdentifiers: minify,
10991113
MinifySyntax: minify,
1100-
KeepNames: ctx.args.KeepNames, // prevent class/function names erasing
1101-
IgnoreAnnotations: ctx.args.IgnoreAnnotations, // some libs maybe use wrong side-effect annotations
1114+
KeepNames: keepNames,
1115+
IgnoreAnnotations: ignoreAnnotations,
11021116
Conditions: conditions,
11031117
Loader: loaders,
11041118
Plugins: []esbuild.Plugin{esmifyPlugin},
@@ -1598,14 +1612,16 @@ func (ctx *BuildContext) install() (err error) {
15981612
return
15991613
}
16001614

1601-
func (ctx *BuildContext) isNoBundle() bool {
1602-
noBundle := ctx.bundleMode == BundleFalse || ctx.pkgJson.SideEffects.Len() > 0
1615+
func (ctx *BuildContext) shouldBundle() bool {
1616+
if ctx.bundleMode == BundleFalse || ctx.pkgJson.SideEffects.Len() > 0 {
1617+
return false
1618+
}
16031619
if ctx.pkgJson.Esmsh != nil {
16041620
if v, ok := ctx.pkgJson.Esmsh["bundle"]; ok {
16051621
if b, ok := v.(bool); ok && !b {
1606-
noBundle = true
1622+
return false
16071623
}
16081624
}
16091625
}
1610-
return noBundle
1626+
return true
16111627
}

server/build_resolver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
227227
if entry.types == "" {
228228
if p.Types != "" && ctx.existsPkgFile(subPath, p.Types) {
229229
entry.types = "./" + path.Join(subPath, p.Types)
230-
} else if p.Typings != "" && ctx.existsPkgFile(subPath, p.Typings) {
231-
entry.types = "./" + path.Join(subPath, p.Typings)
232230
}
233231
}
234232
}
@@ -307,8 +305,6 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
307305
}
308306
if pkgJson.Types != "" {
309307
entry.types = normalizeEntryPath(pkgJson.Types)
310-
} else if pkgJson.Typings != "" {
311-
entry.types = normalizeEntryPath(pkgJson.Typings)
312308
}
313309
if len(pkgJson.Browser) > 0 && ctx.isBrowserTarget() {
314310
if path, ok := pkgJson.Browser["."]; ok && ctx.existsPkgFile(path) {
@@ -1034,7 +1030,7 @@ func (ctx *BuildContext) resolveDTS(entry BuildEntry) (string, error) {
10341030
), nil
10351031
}
10361032

1037-
if ctx.esmPath.SubPath != "" && (ctx.pkgJson.Types != "" || ctx.pkgJson.Typings != "") {
1033+
if ctx.esmPath.SubPath != "" && ctx.pkgJson.Types != "" {
10381034
return "", nil
10391035
}
10401036

server/router.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,6 @@ func esmRouter(esmStorage storage.Storage, logger *log.Logger) rex.Handle {
557557
types := "index.d.ts"
558558
if info.Types != "" {
559559
types = info.Types
560-
} else if info.Typings != "" {
561-
types = info.Typings
562560
} else if info.Main != "" && endsWith(info.Main, ".d.ts", ".d.mts", ".d.cts") {
563561
types = info.Main
564562
}

0 commit comments

Comments
 (0)