From 911529fd364eb3ee1b8ecdc568a9fcf38a8b55ca Mon Sep 17 00:00:00 2001 From: Rene Buchmayer Date: Tue, 11 May 2021 21:35:11 +0200 Subject: [PATCH] feat(builtin): allow bundling ESM output with the pkg_npm rule (#2648) --- internal/pkg_npm/pkg_npm.bzl | 12 ++++++++++-- internal/pkg_npm/test/pkg_npm.spec.js | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/pkg_npm/pkg_npm.bzl b/internal/pkg_npm/pkg_npm.bzl index 7a84d38145..a4737ef048 100644 --- a/internal/pkg_npm/pkg_npm.bzl +++ b/internal/pkg_npm/pkg_npm.bzl @@ -6,7 +6,7 @@ If all users of your library code use Bazel, they should just add your library to the `deps` of one of their targets. """ -load("//:providers.bzl", "DeclarationInfo", "JSModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo") +load("//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "JSNamedModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo") _DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact. @@ -299,7 +299,15 @@ def _pkg_npm(ctx): if JSModuleInfo in dep: deps_files_depsets.append(dep[JSModuleInfo].sources) - # Include all transitive declerations + # All direct and transitive deps that produce CommonJS modules + if JSNamedModuleInfo in dep: + deps_files_depsets.append(dep[JSNamedModuleInfo].sources) + + # All direct and transitive deps that produce ES6 modules + if JSEcmaScriptModuleInfo in dep: + deps_files_depsets.append(dep[JSEcmaScriptModuleInfo].sources) + + # Include all transitive declarations if DeclarationInfo in dep: deps_files_depsets.append(dep[DeclarationInfo].transitive_declarations) diff --git a/internal/pkg_npm/test/pkg_npm.spec.js b/internal/pkg_npm/test/pkg_npm.spec.js index bcc22da6b0..2fa978e178 100644 --- a/internal/pkg_npm/test/pkg_npm.spec.js +++ b/internal/pkg_npm/test/pkg_npm.spec.js @@ -39,6 +39,9 @@ describe('pkg_npm', () => { it('copies declaration files from ts_library', () => { expect(readFromPkg('foo.d.ts')).toContain('export declare const a: string;'); }); + it('copies ESM files from ts_library', () => { + expect(readFromPkg('foo.mjs')).toContain('export const a = \'\';'); + }); it('copies data dependencies', () => { expect(readFromPkg('data.json')).toEqual('[]'); });