addImport adds an import to the import map.
(mark *set.Set[string], imp ImportMeta, indirect bool, targetImports *Imports, noSRI bool)
| 308 | |
| 309 | // addImport adds an import to the import map. |
| 310 | func (im *ImportMap) addImport(mark *set.Set[string], imp ImportMeta, indirect bool, targetImports *Imports, noSRI bool) (warnings []string, errors []error) { |
| 311 | specifier := imp.Specifier(false) |
| 312 | if mark.Has(specifier) { |
| 313 | return |
| 314 | } |
| 315 | mark.Add(specifier) |
| 316 | |
| 317 | cdnOrigin := im.cdnOrigin() |
| 318 | cdnScopeImportsMap, cdnScoped := im.GetScopeImports(cdnOrigin + "/") |
| 319 | if !cdnScoped { |
| 320 | cdnScopeImportsMap = &Imports{imports: map[string]string{}} |
| 321 | im.SetScopeImports(cdnOrigin+"/", cdnScopeImportsMap) |
| 322 | } |
| 323 | |
| 324 | imports := im.Imports |
| 325 | if indirect { |
| 326 | if targetImports != nil { |
| 327 | imports = targetImports |
| 328 | } else { |
| 329 | imports = cdnScopeImportsMap |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | var target string |
| 334 | switch v := im.config.Target; v { |
| 335 | case "es2015", "es2016", "es2017", "es2018", "es2019", "es2020", "es2021", "es2022", "es2023", "es2024", "esnext": |
| 336 | target = v |
| 337 | default: |
| 338 | target = "es2022" |
| 339 | } |
| 340 | |
| 341 | moduleUrl := cdnOrigin + "/" + imp.EsmSpecifier() + "/" |
| 342 | moduleUrl += target + "/" |
| 343 | if imp.SubPath != "" { |
| 344 | if imp.Dev || imp.SubPath == "jsx-dev-runtime" { |
| 345 | moduleUrl += imp.SubPath + ".development.mjs" |
| 346 | } else { |
| 347 | moduleUrl += imp.SubPath + ".mjs" |
| 348 | } |
| 349 | } else { |
| 350 | if strings.ContainsRune(imp.Name, '/') { |
| 351 | _, name := utils.SplitByFirstByte(imp.Name, '/') |
| 352 | moduleUrl += name + ".mjs" |
| 353 | } else { |
| 354 | moduleUrl += imp.Name + ".mjs" |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | imports.Set(specifier, moduleUrl) |
| 359 | if !indirect { |
| 360 | cdnScopeImportsMap.Delete(specifier) |
| 361 | } |
| 362 | if !noSRI { |
| 363 | if !imp.HasExternalImports() { |
| 364 | if imp.Integrity != "" { |
| 365 | im.integrity.Set(moduleUrl, imp.Integrity) |
| 366 | } |
| 367 | } else { |
no test coverage detected