in internal/npm_install/generate_build_file.ts [1105:1246]
function printPackageLegacy(pkg: Dep) {
function starlarkFiles(attr: string, files: string[], comment: string = '') {
return `
${comment ? comment + '\n ' : ''}${attr} = [
${files.map((f: string) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
],`;
}
const includedRunfiles = filterFiles(pkg._runfiles, config.included_files);
// Files that are part of the npm package not including its nested node_modules
// (filtered by the 'included_files' attribute)
const pkgFiles = includedRunfiles.filter((f: string) => !f.startsWith('node_modules/'));
const pkgFilesStarlark = pkgFiles.length ? starlarkFiles('srcs', pkgFiles) : '';
// Files that are in the npm package's nested node_modules
// (filtered by the 'included_files' attribute)
const nestedNodeModules = includedRunfiles.filter((f: string) => f.startsWith('node_modules/'));
const nestedNodeModulesStarlark =
nestedNodeModules.length ? starlarkFiles('srcs', nestedNodeModules) : '';
// Files that have been excluded from the ${pkg._name}__files target above because
// they are filtered out by 'included_files' or because they are not valid runfiles
// See https://github.com/bazelbuild/bazel/issues/4327.
const notPkgFiles = pkg._files.filter(
(f: string) => !f.startsWith('node_modules/') && !includedRunfiles.includes(f));
const notPkgFilesStarlark = notPkgFiles.length ? starlarkFiles('srcs', notPkgFiles) : '';
// If the package is in the Angular package format returns list
// of package files that end with `.umd.js`, `.ngfactory.js` and `.ngsummary.js`.
// TODO(gmagolan): add UMD & AMD scripts to scripts even if not an APF package _but_ only if they
// are named?
const namedSources = isNgApfPackage(pkg) ?
filterFiles(pkg._runfiles, ['.umd.js', '.ngfactory.js', '.ngsummary.js']) :
[];
const namedSourcesStarlark = namedSources.length ?
starlarkFiles(
'named_module_srcs', namedSources,
'# subset of srcs that are javascript named-UMD or named-AMD scripts') :
'';
// Typings files that are part of the npm package not including nested node_modules
const dtsSources =
filterFiles(pkg._runfiles, ['.d.ts']).filter((f: string) => !f.startsWith('node_modules/'));
const dtsStarlark = dtsSources.length ?
starlarkFiles(
'srcs', dtsSources,
`# ${
pkg._dir} package declaration files (and declaration files in nested node_modules)`) :
'';
// Flattened list of direct and transitive dependencies hoisted to root by the package manager
const deps = [pkg].concat(pkg._dependencies.filter(dep => dep !== pkg && !dep._isNested));
const depsStarlark =
deps.map(dep => `"//${dep._dir}:${dep._name}__contents",`).join('\n ');
let result = `load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
# Generated targets for npm package "${pkg._dir}"
${printJson(pkg)}
# Files that are part of the npm package not including its nested node_modules
# (filtered by the 'included_files' attribute)
filegroup(
name = "${pkg._name}__files",${pkgFilesStarlark}
)
# Files that are in the npm package's nested node_modules
# (filtered by the 'included_files' attribute)
filegroup(
name = "${pkg._name}__nested_node_modules",${nestedNodeModulesStarlark}
visibility = ["//:__subpackages__"],
)
# Files that have been excluded from the ${pkg._name}__files target above because
# they are filtered out by 'included_files' or because they are not valid runfiles
# See https://github.com/bazelbuild/bazel/issues/4327.
filegroup(
name = "${pkg._name}__not_files",${notPkgFilesStarlark}
visibility = ["//visibility:private"],
)
# All of the files in the npm package including files that have been
# filtered out by 'included_files' or because they are not valid runfiles
# but not including nested node_modules.
filegroup(
name = "${pkg._name}__all_files",
srcs = [":${pkg._name}__files", ":${pkg._name}__not_files"],
)
# The primary target for this package for use in rule deps
js_library(
name = "${pkg._name}",
package_name = "${LEGACY_NODE_MODULES_PACKAGE_NAME}",
package_path = "${config.package_path}",
# direct sources listed for strict deps support
srcs = [":${pkg._name}__files"],
# nested node_modules for this package plus flattened list of direct and transitive dependencies
# hoisted to root by the package manager
deps = [
${depsStarlark}
],
)
# Target is used as dep for main targets to prevent circular dependencies errors
js_library(
name = "${pkg._name}__contents",
package_name = "${LEGACY_NODE_MODULES_PACKAGE_NAME}",
package_path = "${config.package_path}",
srcs = [":${pkg._name}__files", ":${pkg._name}__nested_node_modules"],${namedSourcesStarlark}
visibility = ["//:__subpackages__"],
)
# Typings files that are part of the npm package not including nested node_modules
js_library(
name = "${pkg._name}__typings",
package_name = "${LEGACY_NODE_MODULES_PACKAGE_NAME}",
package_path = "${config.package_path}",${dtsStarlark}
)
`;
let mainEntryPoint = resolvePkgMainFile(pkg);
// add an `npm_umd_bundle` target to generate an UMD bundle if one does
// not exists
if (mainEntryPoint && !findFile(pkg, `${pkg._name}.umd.js`)) {
result +=
`load("@build_bazel_rules_nodejs//internal/npm_install:npm_umd_bundle.bzl", "npm_umd_bundle")
npm_umd_bundle(
name = "${pkg._name}__umd",
package_name = "${pkg._moduleName}",
entry_point = "@${config.workspace}//:node_modules/${pkg._dir}/${mainEntryPoint}",
package = ":${pkg._name}",
)
`;
}
return result;
}