Skip to content

Commit

Permalink
Refactor & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Mar 20, 2023
1 parent a895e7d commit 23a0669
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 198 deletions.
34 changes: 4 additions & 30 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,13 @@ Public API surface is re-exported here.
Users should not load files under "/internal"


<a id="webpack_binary"></a>

## webpack_binary

<pre>
webpack_binary(<a href="#webpack_binary-name">name</a>, <a href="#webpack_binary-node_modules">node_modules</a>)
</pre>

Create a webpack binary target from linked node_modules in the user's workspace.

The following three packages must be linked into the node_modules supplied:

webpack, webpack-cli, webpack-dev-server


**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="webpack_binary-name"></a>name | Unique name for the binary target | none |
| <a id="webpack_binary-node_modules"></a>node_modules | Label pointing to the linked node_modules target where webpack is linked, e.g. <code>//:node_modules</code>. | none |


<a id="webpack_bundle"></a>

## webpack_bundle

<pre>
webpack_bundle(<a href="#webpack_bundle-name">name</a>, <a href="#webpack_bundle-node_modules">node_modules</a>, <a href="#webpack_bundle-srcs">srcs</a>, <a href="#webpack_bundle-args">args</a>, <a href="#webpack_bundle-deps">deps</a>, <a href="#webpack_bundle-chdir">chdir</a>, <a href="#webpack_bundle-data">data</a>, <a href="#webpack_bundle-env">env</a>, <a href="#webpack_bundle-output_dir">output_dir</a>, <a href="#webpack_bundle-entry_point">entry_point</a>,
<a href="#webpack_bundle-entry_points">entry_points</a>, <a href="#webpack_bundle-webpack_config">webpack_config</a>, <a href="#webpack_bundle-use_execroot_entry_point">use_execroot_entry_point</a>, <a href="#webpack_bundle-supports_workers">supports_workers</a>,
<a href="#webpack_bundle-allow_execroot_entry_point_with_no_copy_data_to_bin">allow_execroot_entry_point_with_no_copy_data_to_bin</a>, <a href="#webpack_bundle-kwargs">kwargs</a>)
<a href="#webpack_bundle-entry_points">entry_points</a>, <a href="#webpack_bundle-webpack_config">webpack_config</a>, <a href="#webpack_bundle-use_execroot_entry_point">use_execroot_entry_point</a>, <a href="#webpack_bundle-supports_workers">supports_workers</a>, <a href="#webpack_bundle-kwargs">kwargs</a>)
</pre>

Runs the webpack-cli under bazel
Expand All @@ -47,7 +22,7 @@ Runs the webpack-cli under bazel
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="webpack_bundle-name"></a>name | A unique name for this target. | none |
| <a id="webpack_bundle-node_modules"></a>node_modules | Label pointing to the linked node_modules target where webpack is linked, e.g. <code>//:node_modules</code>.<br><br>The following three packages must be linked into the node_modules supplied:<br><br> webpack, webpack-cli, webpack-dev-server | none |
| <a id="webpack_bundle-node_modules"></a>node_modules | Label pointing to the linked node_modules target where webpack is linked, e.g. <code>//:node_modules</code>.<br><br>The following packages must be linked into the node_modules supplied:<br><br> webpack, webpack-cli | none |
| <a id="webpack_bundle-srcs"></a>srcs | Non-entry point JavaScript source files from the workspace.<br><br>You must not repeat file(s) passed to entry_point/entry_points. | <code>[]</code> |
| <a id="webpack_bundle-args"></a>args | Command line arguments to pass to Webpack.<br><br>These argument passed on the command line before arguments that are added by the rule. Run <code>bazel</code> with <code>--subcommands</code> to see what Webpack CLI command line was invoked.<br><br>See the [Webpack CLI docs](https://webpack.js.org/api/cli/) for a complete list of supported arguments. | <code>[]</code> |
| <a id="webpack_bundle-deps"></a>deps | Runtime dependencies which may be loaded during compilation. | <code>[]</code> |
Expand All @@ -58,9 +33,8 @@ Runs the webpack-cli under bazel
| <a id="webpack_bundle-entry_point"></a>entry_point | The point where to start the application bundling process.<br><br>See https://webpack.js.org/concepts/entry-points/<br><br>Exactly one of <code>entry_point</code> to <code>entry_points</code> must be specified if <code>output_dir</code> is <code>False</code>. | <code>None</code> |
| <a id="webpack_bundle-entry_points"></a>entry_points | The map of entry points to bundle names.<br><br>See https://webpack.js.org/concepts/entry-points/<br><br>Exactly one of <code>entry_point</code> to <code>entry_points</code> must be specified if <code>output_dir</code> is <code>False</code>. | <code>{}</code> |
| <a id="webpack_bundle-webpack_config"></a>webpack_config | Webpack configuration file.<br><br>See https://webpack.js.org/configuration/ | <code>None</code> |
| <a id="webpack_bundle-use_execroot_entry_point"></a>use_execroot_entry_point | Use the <code>entry_point</code> script of the <code>webpack</code> <code>js_binary</code> that is in the execroot output tree instead of the copy that is in runfiles.<br><br>When set, runfiles are hoisted to the target platform when this is configured and included as target platform execroot inputs to the action.<br><br>Using the entry point script that is in the execroot output tree means that there will be no conflicting runfiles <code>node_modules</code> in the node_modules resolution path which can confuse npm packages such as next and react that don't like being resolved in multiple node_modules trees. This more closely emulates the environment that tools such as Next.js see when they are run outside of Bazel.<br><br>When True, the <code>webpack</code> <code>js_binary</code> must have <code>copy_data_to_bin</code> set to True (the default) so that all data files needed by the binary are available in the execroot output tree. This requirement can be turned off with by setting <code>allow_execroot_entry_point_with_no_copy_data_to_bin</code> to True. | <code>True</code> |
| <a id="webpack_bundle-use_execroot_entry_point"></a>use_execroot_entry_point | Use the <code>entry_point</code> script of the <code>webpack</code> <code>js_binary</code> that is in the execroot output tree instead of the copy that is in runfiles.<br><br>When set, runfiles are hoisted to the target platform when this is configured and included as target platform execroot inputs to the action.<br><br>Using the entry point script that is in the execroot output tree means that there will be no conflicting runfiles <code>node_modules</code> in the node_modules resolution path which can confuse npm packages such as next and react that don't like being resolved in multiple node_modules trees. This more closely emulates the environment that tools such as Next.js see when they are run outside of Bazel. | <code>True</code> |
| <a id="webpack_bundle-supports_workers"></a>supports_workers | Experimental! Use only with caution.<br><br>Allows you to enable the Bazel Worker strategy for this library. | <code>False</code> |
| <a id="webpack_bundle-allow_execroot_entry_point_with_no_copy_data_to_bin"></a>allow_execroot_entry_point_with_no_copy_data_to_bin | Turn off validation that the <code>webpack</code> <code>js_binary</code> has <code>copy_data_to_bin</code> set to True when <code>use_execroot_entry_point</code> is set to True.<br><br>See <code>use_execroot_entry_point</code> doc for more info. | <code>False</code> |
| <a id="webpack_bundle-kwargs"></a>kwargs | Additional arguments | none |


Expand All @@ -86,7 +60,7 @@ under the hood.
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="webpack_devserver-name"></a>name | A unique name for this target. | none |
| <a id="webpack_devserver-node_modules"></a>node_modules | Label pointing to the linked node_modules target where webpack is linked, e.g. <code>//:node_modules</code>.<br><br>The following three packages must be linked into the node_modules supplied:<br><br> webpack, webpack-cli, webpack-dev-server | none |
| <a id="webpack_devserver-node_modules"></a>node_modules | Label pointing to the linked node_modules target where webpack is linked, e.g. <code>//:node_modules</code>.<br><br>The following packages must be linked into the node_modules supplied:<br><br> webpack, webpack-cli, webpack-dev-server | none |
| <a id="webpack_devserver-chdir"></a>chdir | Working directory to run Webpack under.<br><br>This is needed to workaround some buggy resolvers in webpack loaders, which assume that the node_modules tree is located in a parent of the working directory rather than a parent of the script with the require statement.<br><br>Note that any relative paths in your configuration may need to be adjusted so they are relative to the new working directory.<br><br>See also: https://github.com/aspect-build/rules_js/blob/main/docs/js_binary.md#js_binary-chdir | <code>None</code> |
| <a id="webpack_devserver-env"></a>env | Environment variables of the action.<br><br>Subject to <code>$(location)</code> and make variable expansion. | <code>{}</code> |
| <a id="webpack_devserver-entry_point"></a>entry_point | The point where to start the application bundling process.<br><br>See https://webpack.js.org/concepts/entry-points/<br><br>Only one of <code>entry_point</code> to <code>entry_points</code> must be specified. | <code>None</code> |
Expand Down
15 changes: 15 additions & 0 deletions e2e/worker/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test")
load("@aspect_rules_webpack//webpack:defs.bzl", "webpack_bundle")
load("@npm//:defs.bzl", "npm_link_all_packages")

Expand All @@ -17,3 +18,17 @@ write_source_files(
"expected.js_": ":bundle.js",
},
)

webpack_bundle(
name = "bundle_no_execroot_entry_point",
entry_point = "module.js",
node_modules = "//:node_modules",
supports_workers = True,
use_execroot_entry_point = False,
)

diff_test(
name = "bundles_match",
file1 = "bundle.js",
file2 = "bundle_no_execroot_entry_point.js",
)
3 changes: 1 addition & 2 deletions webpack/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
Users should not load files under "/internal"
"""

load("//webpack/private:webpack_bundle.bzl", _webpack_binary = "webpack_binary", _webpack_bundle = "webpack_bundle")
load("//webpack/private:webpack_bundle.bzl", _webpack_bundle = "webpack_bundle")
load("//webpack/private:webpack_devserver.bzl", _webpack_devserver = "webpack_devserver")

webpack_binary = _webpack_binary
webpack_bundle = _webpack_bundle
webpack_devserver = _webpack_devserver
20 changes: 19 additions & 1 deletion webpack/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "webpack_binary",
srcs = ["webpack_binary.bzl"],
visibility = ["//webpack:__subpackages__"],
)

bzl_library(
name = "webpack_bundle",
srcs = ["webpack_bundle.bzl"],
visibility = ["//webpack:__subpackages__"],
deps = [
":webpack_binary",
":webpack_create_configs",
"@aspect_bazel_lib//lib:copy_file",
"@aspect_bazel_lib//lib:copy_to_bin",
"@aspect_rules_js//js:libs",
"@aspect_rules_js//js:providers",
],
)

bzl_library(
name = "webpack_create_configs",
srcs = ["webpack_create_configs.bzl"],
visibility = ["//webpack:__subpackages__"],
)

bzl_library(
name = "webpack_devserver",
srcs = ["webpack_devserver.bzl"],
visibility = ["//webpack:__subpackages__"],
deps = ["@aspect_rules_js//js:defs"],
deps = [
":webpack_binary",
":webpack_create_configs",
"@aspect_rules_js//js:defs",
],
)

exports_files([
Expand Down
35 changes: 35 additions & 0 deletions webpack/private/webpack_binary.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""webpack_binary helper macro"""

load("@aspect_bazel_lib//lib:directory_path.bzl", "directory_path")
load("@aspect_rules_js//js:defs.bzl", "js_binary")

def webpack_binary(
name,
node_modules,
additional_packages):
"""Create a webpack binary target from linked node_modules in the user's workspace.
Requires that `webpack` and any additional packages specified are linked into the supplied node_modules tree.
Args:
name: Unique name for the binary target
node_modules: Label pointing to the linked node_modules tree where webpack is linked, e.g. `//:node_modules`.
additional_packages: list of additional packages required. For example ["webpack-cli", "webpack-dev-server"]
"""

directory_path(
name = "{}_entrypoint".format(name),
directory = "{}/webpack/dir".format(node_modules),
path = "bin/webpack.js",
)

data = ["{}/webpack".format(node_modules)]
for p in additional_packages:
data.append("{}/{}".format(node_modules, p))

js_binary(
name = name,
data = data,
entry_point = ":{}_entrypoint".format(name),
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 23a0669

Please sign in to comment.