Extensions in subpaths


包作者应在其导出中提供扩展 (import 'pkg/subpath.js') 或无扩展 (import 'pkg/subpath') 子路径。 这确保每个导出的模块只有一个子路径,以便所有依赖项导入相同的一致说明符,使消费者清楚地了解包合同并简化包子路径的完成。

传统上,包倾向于使用无扩展名风格,它具有可读性和掩盖包中文件的真实路径的好处。

随着导入映射现在为浏览器和其他 JavaScript 运行时中的包解析提供标准,使用无扩展风格可能会导致导入映射定义臃肿。 显式的文件扩展名可以通过启用导入映射来避免此问题,以利用包文件夹映射在可能的情况下映射多个子路径,而不是每个包子路径导出单独的映射条目。 这也反映了在相对和绝对导入说明符中使用完整说明符路径的要求。

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.

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.