Skip to content

Commit 911529f

Browse files
authored
feat(builtin): allow bundling ESM output with the pkg_npm rule (#2648)
1 parent 5ad1596 commit 911529f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

internal/pkg_npm/pkg_npm.bzl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ If all users of your library code use Bazel, they should just add your library
66
to the `deps` of one of their targets.
77
"""
88

9-
load("//:providers.bzl", "DeclarationInfo", "JSModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo")
9+
load("//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "JSNamedModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo")
1010

1111
_DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact.
1212
@@ -299,7 +299,15 @@ def _pkg_npm(ctx):
299299
if JSModuleInfo in dep:
300300
deps_files_depsets.append(dep[JSModuleInfo].sources)
301301

302-
# Include all transitive declerations
302+
# All direct and transitive deps that produce CommonJS modules
303+
if JSNamedModuleInfo in dep:
304+
deps_files_depsets.append(dep[JSNamedModuleInfo].sources)
305+
306+
# All direct and transitive deps that produce ES6 modules
307+
if JSEcmaScriptModuleInfo in dep:
308+
deps_files_depsets.append(dep[JSEcmaScriptModuleInfo].sources)
309+
310+
# Include all transitive declarations
303311
if DeclarationInfo in dep:
304312
deps_files_depsets.append(dep[DeclarationInfo].transitive_declarations)
305313

internal/pkg_npm/test/pkg_npm.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ describe('pkg_npm', () => {
3939
it('copies declaration files from ts_library', () => {
4040
expect(readFromPkg('foo.d.ts')).toContain('export declare const a: string;');
4141
});
42+
it('copies ESM files from ts_library', () => {
43+
expect(readFromPkg('foo.mjs')).toContain('export const a = \'\';');
44+
});
4245
it('copies data dependencies', () => {
4346
expect(readFromPkg('data.json')).toEqual('[]');
4447
});

0 commit comments

Comments
 (0)