generateImportMapJson static method
Generates a standard W3C Import Map JSON string with imports and scopes.
When pretty is true, the output JSON string is indented with 2 spaces.
{
"imports": {
"zod": "https://esm.sh/zod@^3.23.0"
}
}
Implementation
static String generateImportMapJson({bool pretty = false}) {
final mainImports = <String, String>{};
final scopes = <String, Map<String, String>>{};
for (final e in _deps.entries) {
if (e.value.subPath != null) {
// Emit as a scopes entry.
final baseUrl = '${e.value.cdn}/${e.value.name}@${e.value.version}/';
scopes[baseUrl] = {e.value.specifier: e.value.url};
} else {
mainImports[e.key] = e.value.url;
}
}
final map = <String, dynamic>{
'imports': mainImports,
if (scopes.isNotEmpty) 'scopes': scopes,
};
return pretty
? const JsonEncoder.withIndent(' ').convert(map)
: jsonEncode(map);
}