Skip to content

Commit

Permalink
feat(builtin): allow bundling ESM output with the pkg_npm rule
Browse files Browse the repository at this point in the history
  • Loading branch information
rebu-dt committed May 3, 2021
1 parent 4965db6 commit bdf9329
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
18 changes: 13 additions & 5 deletions internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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", "JSNamedModuleInfo", "LinkablePackageInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo")

_DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact.
Expand Down Expand Up @@ -98,6 +98,10 @@ PKG_NPM_ATTRS = dict(NODE_CONTEXT_ATTRS, **{
doc = """Other targets which produce files that should be included in the package, such as `rollup_bundle`""",
allow_files = True,
),
"include_esm": attr.bool(
doc = """Whether generated ESM files should also be included in the package, in addition to CommonJS modules.""",
default = False,
),
"nested_packages": attr.label_list(
doc = """Other pkg_npm rules whose content is copied into this package.""",
allow_files = True,
Expand Down Expand Up @@ -295,11 +299,15 @@ def _pkg_npm(ctx):
# Only collect DefaultInfo files (not transitive)
deps_files_depsets.append(dep.files)

# All direct & transitive JavaScript-producing deps
if JSModuleInfo in dep:
deps_files_depsets.append(dep[JSModuleInfo].sources)
# 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 and ctx.attr.include_esm:
deps_files_depsets.append(dep[JSEcmaScriptModuleInfo].sources)

# Include all transitive declerations
# Include all transitive declarations
if DeclarationInfo in dep:
deps_files_depsets.append(dep[DeclarationInfo].transitive_declarations)

Expand Down
11 changes: 11 additions & 0 deletions internal/pkg_npm/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ pkg_npm(
],
)

pkg_npm(
name = "test_esm_pkg",
srcs = [],
include_esm = True,
deps = [
":ts_library",
],
)

pkg_npm(
name = "test_noop_pkg",
# Special case where these is a single dep that is a directory artifact
Expand Down Expand Up @@ -109,12 +118,14 @@ jasmine_node_test(
name = "test",
srcs = ["pkg_npm.spec.js"],
data = [
":test_esm_pkg",
":test_noop2_pkg",
":test_noop_pkg",
":test_pkg",
],
templated_args = [
"$(rootpath :test_pkg)",
"$(rootpath :test_esm_pkg)",
"$(rootpath :test_noop_pkg)",
"$(rootpath :test_noop2_pkg)",
],
Expand Down
16 changes: 14 additions & 2 deletions internal/pkg_npm/test/pkg_npm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ const fs = require('fs');
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
const args = process.argv.slice(2);
const testPkgRootpath = args[0];
const testNoopPkgRootpath = args[1];
const testNoop2PkgRootpath = args[2];
const testEsmPkgRootpath = args[1];
const testNoopPkgRootpath = args[2];
const testNoop2PkgRootpath = args[3];
const testPkgPath = runfiles.resolveWorkspaceRelative(testPkgRootpath);
const testEsmPkgPath = runfiles.resolveWorkspaceRelative(testEsmPkgRootpath);
const testNoopPkgPath = runfiles.resolveWorkspaceRelative(testNoopPkgRootpath);
const testNoop2PkgPath = runfiles.resolveWorkspaceRelative(testNoop2PkgRootpath);

function readFromPkg(p) {
return fs.readFileSync(`${testPkgPath}/${p}`, {encoding: 'utf-8'}).trim();
}

function readFromEsmPkg(p) {
return fs.readFileSync(`${testEsmPkgPath}/${p}`, {encoding: 'utf-8'}).trim();
}

function readFromNoopPkg(p) {
return fs.readFileSync(`${testNoopPkgPath}/${p}`, {encoding: 'utf-8'}).trim();
}
Expand Down Expand Up @@ -39,6 +45,12 @@ 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 if include_esm is True', () => {
expect(readFromEsmPkg('foo.mjs')).toContain('export const a = \'\';');
});
it('does not copy ESM files if include_esm is False', () => {
expect(() => readFromPkg('foo.mjs')).toThrow();
});
it('copies data dependencies', () => {
expect(readFromPkg('data.json')).toEqual('[]');
});
Expand Down

0 comments on commit bdf9329

Please sign in to comment.