子路径中的扩展


【Extensions in subpaths】

包的作者应该在其导出中提供带扩展名(import 'pkg/subpath.js')或不带扩展名(import 'pkg/subpath')的子路径。这确保每个导出模块只有一个子路径,从而使所有依赖方导入相同的一致的标识符,保持包的契约对使用者清晰,并简化包子路径的补全。

【Package authors should provide either extensioned (import 'pkg/subpath.js') or extensionless (import 'pkg/subpath') subpaths in their exports. This ensures that there is only one subpath for each exported module so that all dependents import the same consistent specifier, keeping the package contract clear for consumers and simplifying package subpath completions.】

传统上,包通常倾向于使用无扩展名的风格,这种风格具有可读性强以及隐藏包内文件真实路径的优点。

【Traditionally, packages tended to use the extensionless style, which has the benefits of readability and of masking the true path of the file within the package.】

随着 导入地图 现在为浏览器和其他 JavaScript 运行时提供了包解析的标准,使用无扩展名的风格可能会导致导入映射定义膨胀。显式的文件扩展名可以避免这个问题,因为它使导入映射能够利用 包文件夹映射 尽可能地映射多个子路径,而不必为每个包子路径导出创建单独的映射条目。这也反映了在相对和绝对导入说明符中使用 完整限定路径 的要求。

【With import maps now providing a standard for package resolution in browsers and other JavaScript runtimes, using the extensionless style can result in bloated import map definitions. Explicit file extensions can avoid this issue by enabling the import map to utilize a packages folder mapping to map multiple subpaths where possible instead of a separate map entry per package subpath export. This also mirrors the requirement of using the full specifier path in relative and absolute import specifiers.】