-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use esbuild for javascript bundling. #5829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6c8d8d6
6d1759d
6e5d9fa
40f602d
90f6e5d
c86b42a
b06e4fe
08fb3fc
750e402
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,11 @@ | |
| # limitations under the License. | ||
| """External-only delegates for various BUILD rules.""" | ||
|
|
||
| load("@npm//@bazel/rollup:index.bzl", "rollup_bundle") | ||
| load("@io_bazel_rules_sass//:defs.bzl", "npm_sass_library", "sass_binary", "sass_library") | ||
| load("@npm//@bazel/concatjs:index.bzl", "karma_web_test_suite") | ||
| load("@npm//@bazel/esbuild:index.bzl", "esbuild") | ||
| load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_library") | ||
| load("@io_bazel_rules_sass//:defs.bzl", "npm_sass_library", "sass_binary", "sass_library") | ||
| load("@npm//@bazel/terser:index.bzl", "terser_minified") | ||
| load("//tensorboard/defs/internal:js.bzl", _tf_dev_js_binary = "tf_dev_js_binary") | ||
|
|
||
| tf_dev_js_binary = _tf_dev_js_binary | ||
|
|
||
| def tensorboard_webcomponent_library(**kwargs): | ||
| """Rules referencing this will be deleted from the codebase soon.""" | ||
|
|
@@ -29,7 +26,6 @@ def tensorboard_webcomponent_library(**kwargs): | |
| def tf_js_binary( | ||
| name, | ||
| compile, | ||
| deps, | ||
| visibility = None, | ||
| dev_mode_only = False, | ||
| includes_polymer = False, | ||
|
|
@@ -39,61 +35,50 @@ def tf_js_binary( | |
| Args: | ||
| name: Name of the target. | ||
| compile: whether to compile when bundling. Only used internally. | ||
| deps: dependencies of the js_binary. | ||
| visibility: visibility of the target. | ||
| dev_mode_only: whether the binary is for development. When True, it will | ||
| omit the Terser. | ||
| omit the minification step. | ||
| includes_polymer: whether this binary contains Polymer. Only used | ||
| internally. | ||
| **kwargs: keyword arguments to rollup_bundle. Please refer to | ||
| https://bazelbuild.github.io/rules_nodejs/Built-ins.html#rollup_bundle | ||
| **kwargs: Other keyword arguments to esbuild(). Typically used for | ||
| entry_point and deps. Please refer to https://esbuild.github.io/api/ | ||
| for more details. | ||
| """ | ||
|
|
||
| # `compile` option is used internally but is not used by rollup_bundle. | ||
| # Discard it. | ||
| internal_rollup_name = name + "_rollup_internal_dbg" | ||
| rollup_bundle( | ||
| name = internal_rollup_name, | ||
| config_file = "//tensorboard/defs:rollup_config.js", | ||
| # Must pass `true` here specifically, else the input file argument to | ||
| # Rollup (appended by `rollup_binary`) is interpreted as a value for | ||
| # the preceding option. | ||
| args = ["--failAfterWarnings", "true", "--silent", "true"], | ||
| deps = deps + [ | ||
| "@npm//@rollup/plugin-commonjs", | ||
| "@npm//@rollup/plugin-node-resolve", | ||
| ], | ||
| format = "iife", | ||
| sourcemap = "false", | ||
| visibility = ["//visibility:private"], | ||
| **kwargs | ||
| ) | ||
|
|
||
| if dev_mode_only: | ||
| internal_result_name = internal_rollup_name | ||
| else: | ||
| internal_result_name = name + "_terser_internal_min" | ||
| terser_minified( | ||
| name = internal_result_name, | ||
| src = internal_rollup_name, | ||
| # Notes about the terser config: | ||
| # compress.passes - this is set to '1' to workaround issue with | ||
| # terser and threejs. In practice it (surprisingly) generates | ||
| # smaller results than when it was previously set to '3'. | ||
| config_file = "//tensorboard/defs:terser_config.json", | ||
| visibility = ["//visibility:private"], | ||
| sourcemap = False, | ||
| ) | ||
|
|
||
| # For some reason, terser_minified is not visible from other targets. Copy | ||
| # or re-export seems to work okay. | ||
| native.genrule( | ||
| # esbuild is a fast JavaScript bundler[1] appropriate for both production | ||
| # and development builds. | ||
| # | ||
| # Bazel documents[2] how to use esbuild bundling with ts_project but we use | ||
| # the not-quite-deprecated ts_library rule instead of ts_project. We've | ||
| # managed to get esbuild working with ts_library but its long-term support | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you have to do anything besides specifying .mjs extensions to get ts_library to work?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's all I was certain was ts_library-specific. |
||
| # is unknown. | ||
| # | ||
| # [1]: https://esbuild.github.io/ | ||
| # [2]: https://www.npmjs.com/package/@bazel/esbuild | ||
| esbuild( | ||
| name = name, | ||
| srcs = [internal_result_name], | ||
| outs = [name + ".js"], | ||
| visibility = visibility, | ||
| cmd = "cat $(SRCS) > $@", | ||
| # Use "iife" format instead of "esm" because "esm" writes symbols at | ||
| # the global level and tends to overwrite `window` functions. "iife" is | ||
| # just a thin wrapper around "esm" (it adds 11 bytes) and doesn't | ||
| # suffer from the same overwriting problem. | ||
| format="iife", | ||
| minify= False if dev_mode_only else True, | ||
| args = { | ||
| # Must specify that 'mjs' extensions are preferred, since that is | ||
| # the extension that is used for es2015/esm code generated by | ||
| # ts_library. | ||
| # https://github.com/bazelbuild/rules_nodejs/issues/2691#issuecomment-846429871 | ||
| "resolveExtensions": [".mjs", ".js"], | ||
| # The reasoning for these particular mainFields values are lost to | ||
| # history. These come from the old rollup bundler configuration. | ||
| # We do know that the esbuild default values for mainFields do not | ||
| # work for us. In particular we ran into problems with | ||
| # esbuild pulling in "node"-specific versions of some libraries that | ||
| # are incompatible with browsers. | ||
| "mainFields": ["browser", "es2015", "module", "jsnext:main", "main"], | ||
| }, | ||
| **kwargs | ||
| ) | ||
|
|
||
| def tf_ts_config(**kwargs): | ||
|
|
||
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this back down below these other loads for git blame purposes? Or is there a reason this was moved up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it up to sort the imports alphabetically.