diff --git a/internal/npm_install/generate_build_file.ts b/internal/npm_install/generate_build_file.ts index 6fecd26d..a03bbcc6 100755 --- a/internal/npm_install/generate_build_file.ts +++ b/internal/npm_install/generate_build_file.ts @@ -747,71 +747,6 @@ function findEntryFile(pkg: Dep, path: string) { return entryFile; } -/** - * Tries to resolve the entryPoint file from the pkg for a given mainFileName - * - * @param {any} pkg - * @param {'browser' | 'module' | 'main'} mainFileName - * @returns {string | undefined} the path or undefined if we cant resolve the file - */ -function resolveMainFile(pkg: Dep, mainFileName: string) { - const mainEntryField = pkg[mainFileName]; - - if (mainEntryField) { - if (typeof mainEntryField === 'string') { - return findEntryFile(pkg, mainEntryField) - - } else if (typeof mainEntryField === 'object' && mainFileName === 'browser') { - // browser has a weird way of defining this - // the browser value is an object listing files to alias, usually pointing to a browser dir - const indexEntryPoint = mainEntryField['index.js'] || mainEntryField['./index.js']; - if (indexEntryPoint) { - return findEntryFile(pkg, indexEntryPoint) - } - } - } -} - -/** - * Tries to resolve the mainFile from a given pkg - * This uses seveal mainFileNames in priority to find a correct usable file - * @param {any} pkg - * @returns {string | undefined} - */ -function resolvePkgMainFile(pkg: Dep) { - // es2015 is another option for mainFile here - // but its very uncommon and im not sure what priority it takes - // - // this list is ordered, we try resolve `browser` first, then `module` and finally fall back to - // `main` - const mainFileNames = ['browser', 'module', 'main'] - - for (const mainFile of mainFileNames) { - const resolvedMainFile = resolveMainFile(pkg, mainFile); - if (resolvedMainFile) { - return resolvedMainFile; - } - } - - // if we cant find any correct file references from the pkg - // then we just try looking around for common patterns - const maybeRootIndex = findEntryFile(pkg, 'index.js'); - if (maybeRootIndex) { - return maybeRootIndex - } - - const maybeSelfNamedIndex = findEntryFile(pkg, `${pkg._name}.js`); - if (maybeSelfNamedIndex) { - return maybeSelfNamedIndex; - } - - // none of the methods we tried resulted in a file - log_verbose(`could not find entry point for npm package ${pkg._name}`); - - // at this point there's nothing left for us to try, so return nothing - return undefined; -} - type Bag = { [k: string]: T @@ -1020,24 +955,6 @@ alias( ) `; - 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; } @@ -1166,24 +1083,6 @@ js_library( `; - 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; } diff --git a/internal/npm_install/index.js b/internal/npm_install/index.js index 6d2e5256..bb2708a8 100755 --- a/internal/npm_install/index.js +++ b/internal/npm_install/index.js @@ -600,20 +600,6 @@ alias( actual = "${pkg._name}__contents", ) `; - let mainEntryPoint = resolvePkgMainFile(pkg); - 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; } function printPackage(pkg) { @@ -707,20 +693,6 @@ js_library( ) `; - let mainEntryPoint = resolvePkgMainFile(pkg); - 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; } function _findExecutables(pkg) { diff --git a/internal/npm_install/npm_umd_bundle.bzl b/internal/npm_install/npm_umd_bundle.bzl deleted file mode 100755 index 31462bcc..00000000 --- a/internal/npm_install/npm_umd_bundle.bzl +++ /dev/null @@ -1,129 +0,0 @@ -# Copyright 2017 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Node package UMD bundling - -For use by yarn_install and npm_install. Not meant to be part of the public API. -""" - -load("//:providers.bzl", "DirectoryFilePathInfo", "ExternalNpmPackageInfo", "node_modules_aspect") -load("//internal/common:maybe_directory_file_path.bzl", "maybe_directory_file_path") - -def _entry_point_path(ctx): - if len(ctx.attr.entry_point.files.to_list()) > 1: - fail("labels in entry_point must contain exactly one file") - if len(ctx.files.entry_point) == 1: - return ctx.files.entry_point[0].path - if DirectoryFilePathInfo in ctx.attr.entry_point: - return "/".join([ - ctx.attr.entry_point[DirectoryFilePathInfo].directory.path, - ctx.attr.entry_point[DirectoryFilePathInfo].path, - ]) - fail("entry_point must either be a file, or provide DirectoryFilePathInfo") - -def _impl(ctx): - output = ctx.actions.declare_file("%s.umd.js" % ctx.attr.package_name) - - args = ctx.actions.args() - - args.add(ctx.workspace_name) - args.add(ctx.attr.package_name) - args.add(_entry_point_path(ctx)) - args.add(output.path) - args.add_joined(ctx.attr.excluded, join_with = ",") - - sources = ctx.attr.package[ExternalNpmPackageInfo].sources.to_list() - - if ctx.attr.package[ExternalNpmPackageInfo].has_directories: - # If sources contain directories then we cannot filter by extension - inputs = sources - else: - # Only pass .js and package.json files as inputs to browserify. - # The latter is required for module resolution in some cases. - inputs = [ - f - for f in sources - if f.path.endswith(".js") or f.path.endswith(".json") - ] - - ctx.actions.run( - progress_message = "Generated UMD bundle for %s npm package [browserify]" % ctx.attr.package_name, - executable = ctx.executable._browserify_wrapped, - inputs = inputs, - outputs = [output], - arguments = [args], - ) - - return [ - DefaultInfo(files = depset([output]), runfiles = ctx.runfiles([output])), - OutputGroupInfo(umd = depset([output])), - ] - -_ATTRS = { - "entry_point": attr.label( - doc = """Entry point for the npm package""", - mandatory = True, - allow_files = True, - ), - "excluded": attr.string_list( - doc = """List of excluded packages that should not be bundled by browserify. - -Packages listed here are passed to browserify with the `-u` argument. See https://github.com/browserify/browserify#usage -for details. - -For example, `typeorm` npm package has an optional dependency on `react-native-sqlite-storage`. For browserify to -ignore this optional require and leave it as `require('react-native-sqlite-storage')` in the output UMD bundle, you -must specify `react-native-sqlite-storage` in the excluded attribute: - -``` -npm_umd_bundle( - name = "typeorm_umd", - package_name = "typeorm", - entry_point = "@npm//:node_modules/typeorm/browser/index.js", - excluded = ["react-native-sqlite-storage"], - package = "@npm//typeorm", -) -``` - -This target would be then be used instead of the generated `@npm//typeorm:typeorm__umd` target in other rules.""", - ), - "package": attr.label( - doc = """The npm package target""", - mandatory = True, - aspects = [node_modules_aspect], - ), - "package_name": attr.string( - doc = """The name of the npm package""", - mandatory = True, - ), - "_browserify_wrapped": attr.label( - executable = True, - cfg = "host", - default = Label("@build_bazel_rules_nodejs//internal/npm_install:browserify-wrapped"), - ), -} - -_npm_umd_bundle = rule( - implementation = _impl, - attrs = _ATTRS, - outputs = {"umd": "%{package_name}.umd.js"}, - doc = """Node package umd bundling""", -) - -def npm_umd_bundle(name, **kwargs): - _npm_umd_bundle( - name = name, - entry_point = maybe_directory_file_path(name, kwargs.pop("entry_point", None)), - **kwargs - )