@@ -14,6 +14,7 @@ import (
14
14
"sync"
15
15
"time"
16
16
17
+ "github.com/esm-dev/esm.sh/server/storage"
17
18
"github.com/evanw/esbuild/pkg/api"
18
19
"github.com/ije/gox/utils"
19
20
)
@@ -111,8 +112,8 @@ func (ctx *BuildContext) Query() (BuildResult, bool) {
111
112
} else {
112
113
_ , err = fs .Stat (normalizeSavePath (ctx .zoneId , path .Join ("types" , b .Dts )))
113
114
}
114
- // ensure the build files exist
115
- if err == nil || os . IsExist ( err ) {
115
+ // ensure the build file exists
116
+ if err == nil || err != storage . ErrNotFound {
116
117
return b , true
117
118
}
118
119
}
@@ -175,9 +176,14 @@ func (ctx *BuildContext) Build() (ret BuildResult, err error) {
175
176
}
176
177
177
178
func (ctx * BuildContext ) install () (err error ) {
178
- if ctx .wd == "" {
179
+ if ctx .wd == "" || ctx . pkgJson . Name == "" {
179
180
ctx .wd = path .Join (ctx .npmrc .Dir (), ctx .pkg .Fullname ())
180
181
ctx .pkgDir = path .Join (ctx .wd , "node_modules" , ctx .pkg .Name )
182
+ if rp , e := os .Readlink (ctx .pkgDir ); e == nil {
183
+ ctx .pnpmPkgDir = path .Join (path .Dir (ctx .pkgDir ), rp )
184
+ } else {
185
+ ctx .pnpmPkgDir = ctx .pkgDir
186
+ }
181
187
err = ctx .npmrc .installPackage (ctx .pkg )
182
188
if err != nil {
183
189
return
@@ -188,11 +194,6 @@ func (ctx *BuildContext) install() (err error) {
188
194
return
189
195
}
190
196
ctx .pkgJson = ctx .normalizePackageJSON (pkgJson )
191
- if rp , e := os .Readlink (ctx .pkgDir ); e == nil {
192
- ctx .pnpmPkgDir = path .Join (path .Dir (ctx .pkgDir ), rp )
193
- } else {
194
- ctx .pnpmPkgDir = ctx .pkgDir
195
- }
196
197
}
197
198
return
198
199
}
@@ -222,19 +223,25 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
222
223
}
223
224
224
225
entry := ctx .resolveEntry (ctx .pkg )
225
- log .Debugf ("build(%s): Entry%+v" , ctx .pkg , entry )
226
-
227
- result , reexport , err := ctx .lexer (& entry , false )
228
- if err != nil && ! strings .HasPrefix (err .Error (), "cjsLexer: Can't resolve" ) {
226
+ if entry .isEmpty () {
227
+ err = fmt .Errorf ("could not resolve entry" )
229
228
return
230
229
}
230
+ log .Debugf ("build(%s): Entry%+v" , ctx .pkg , entry )
231
231
232
- if result .TypesOnly {
232
+ typesOnly := strings .HasPrefix (ctx .pkgJson .Name , "@types/" ) || (entry .esm == "" && entry .cjs == "" && entry .dts != "" )
233
+ if typesOnly {
234
+ result .TypesOnly = true
233
235
result .Dts = "/" + ctx .pkg .ghPrefix () + ctx .pkg .Fullname () + entry .dts [1 :]
234
236
ctx .transformDTS (entry .dts )
235
237
return
236
238
}
237
239
240
+ result , reexport , err := ctx .lexer (& entry , false )
241
+ if err != nil && ! strings .HasPrefix (err .Error (), "cjsLexer: Can't resolve" ) {
242
+ return
243
+ }
244
+
238
245
// cjs reexport
239
246
if reexport != "" {
240
247
pkg , _ , _ , e := ctx .lookupDep (reexport )
@@ -265,7 +272,7 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
265
272
if err != nil {
266
273
return
267
274
}
268
- result .Dts = ctx .lookupTypes (entry )
275
+ result .Dts , err = ctx .resloveDTS (entry )
269
276
return
270
277
}
271
278
@@ -278,10 +285,6 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
278
285
}
279
286
280
287
if entry .esm == "" {
281
- if entry .cjs == "" {
282
- err = fmt .Errorf ("could not resolve \" %s\" " , entryModuleSpecifier )
283
- return
284
- }
285
288
buf := bytes .NewBuffer (nil )
286
289
fmt .Fprintf (buf , `import * as __module from "%s";` , entryModuleSpecifier )
287
290
if len (result .NamedExports ) > 0 {
@@ -862,6 +865,7 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
862
865
if ctx .isDenoTarget () {
863
866
conditions = append (conditions , "deno" )
864
867
}
868
+ minify := config .Minify == nil || ! bytes .Equal (config .Minify , []byte ("false" ))
865
869
options := api.BuildOptions {
866
870
Outdir : "/esbuild" ,
867
871
Write : false ,
@@ -870,9 +874,9 @@ func (ctx *BuildContext) buildModule() (result BuildResult, err error) {
870
874
Format : api .FormatESModule ,
871
875
Target : targets [ctx .target ],
872
876
Platform : api .PlatformBrowser ,
873
- MinifyWhitespace : ! ctx . dev ,
874
- MinifyIdentifiers : ! ctx . dev ,
875
- MinifySyntax : ! ctx . dev ,
877
+ MinifyWhitespace : minify ,
878
+ MinifyIdentifiers : minify ,
879
+ MinifySyntax : minify ,
876
880
KeepNames : ctx .args .keepNames , // prevent class/function names erasing
877
881
IgnoreAnnotations : ctx .args .ignoreAnnotations , // some libs maybe use wrong side-effect annotations
878
882
Conditions : conditions ,
@@ -1207,20 +1211,7 @@ rebuild:
1207
1211
deps .Sort ()
1208
1212
1209
1213
result .Deps = deps
1210
- result .Dts = ctx .lookupTypes (entry )
1211
- return
1212
- }
1213
-
1214
- func (ctx * BuildContext ) LookupTypes () (dts string , err error ) {
1215
- // install the package
1216
- ctx .stage = "install"
1217
- err = ctx .install ()
1218
- if err != nil {
1219
- return
1220
- }
1221
-
1222
- entry := ctx .resolveEntry (ctx .pkg )
1223
- dts = ctx .lookupTypes (entry )
1214
+ result .Dts , err = ctx .resloveDTS (entry )
1224
1215
return
1225
1216
}
1226
1217
@@ -1252,56 +1243,6 @@ func (ctx *BuildContext) buildTypes() (ret BuildResult, err error) {
1252
1243
return
1253
1244
}
1254
1245
1255
- func (ctx * BuildContext ) lookupTypes (entry BuildEntry ) string {
1256
- if entry .dts != "" {
1257
- if ! ctx .existsPkgFile (entry .dts ) {
1258
- return ""
1259
- }
1260
- return fmt .Sprintf (
1261
- "/%s%s/%s%s" ,
1262
- ctx .pkg .ghPrefix (),
1263
- ctx .pkg .Fullname (),
1264
- ctx .getBuildArgsPrefix (ctx .pkg , true ),
1265
- strings .TrimPrefix (entry .dts , "./" ),
1266
- )
1267
- }
1268
-
1269
- // use types from package "@types/[task.npm.Name]" if it exists
1270
- if ctx .pkgJson .Types == "" && ! strings .HasPrefix (ctx .pkgJson .Name , "@types/" ) && regexpFullVersion .MatchString (ctx .pkgJson .Version ) {
1271
- versionParts := strings .Split (ctx .pkgJson .Version , "." )
1272
- versions := []string {
1273
- versionParts [0 ] + "." + versionParts [1 ], // major.minor
1274
- versionParts [0 ], // major
1275
- }
1276
- typesPkgName := toTypesPkgName (ctx .pkgJson .Name )
1277
- pkgVersion , ok := ctx .args .deps [typesPkgName ]
1278
- if ok {
1279
- // use the version of the `?deps` query if it exists
1280
- versions = append ([]string {pkgVersion }, versions ... )
1281
- }
1282
- for _ , version := range versions {
1283
- p , err := ctx .npmrc .getPackageInfo (typesPkgName , version )
1284
- if err == nil {
1285
- typesPkg := Pkg {
1286
- Name : typesPkgName ,
1287
- Version : p .Version ,
1288
- SubPath : ctx .pkg .SubPath ,
1289
- SubModule : ctx .pkg .SubModule ,
1290
- }
1291
- b := NewBuildContext (ctx .zoneId , ctx .npmrc , typesPkg , ctx .args , "types" , BundleFalse , false , false )
1292
- dts , _ := b .LookupTypes ()
1293
- if dts != "" {
1294
- // use tilde semver range instead of the exact version
1295
- return strings .ReplaceAll (dts , fmt .Sprintf ("%s@%s" , typesPkgName , p .Version ), fmt .Sprintf ("%s@~%s" , typesPkgName , p .Version ))
1296
- }
1297
- break
1298
- }
1299
- }
1300
- }
1301
-
1302
- return ""
1303
- }
1304
-
1305
1246
func (ctx * BuildContext ) transformDTS (types string ) (err error ) {
1306
1247
start := time .Now ()
1307
1248
buildArgsPrefix := ctx .getBuildArgsPrefix (ctx .pkg , true )
0 commit comments