Skip to content

Commit 493e78c

Browse files
authored
feat: add configure_mode & configure_devtool attributes to webpack_bundle and webpack_devserver (#112)
1 parent e2d4a39 commit 493e78c

13 files changed

+283
-28
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/pnpm-lock.yaml
22
docs/*.md
3+
webpack/tests/create_configs/expected.*

docs/rules.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Users should not load files under "/internal"
1111

1212
<pre>
1313
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>,
14-
<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>)
14+
<a href="#webpack_bundle-entry_points">entry_points</a>, <a href="#webpack_bundle-webpack_config">webpack_config</a>, <a href="#webpack_bundle-configure_mode">configure_mode</a>, <a href="#webpack_bundle-configure_devtool">configure_devtool</a>,
15+
<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>)
1516
</pre>
1617

1718
Runs the webpack-cli under bazel
@@ -33,6 +34,8 @@ Runs the webpack-cli under bazel
3334
| <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> |
3435
| <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> |
3536
| <a id="webpack_bundle-webpack_config"></a>webpack_config | Webpack configuration file.<br><br>See https://webpack.js.org/configuration/ | <code>None</code> |
37+
| <a id="webpack_bundle-configure_mode"></a>configure_mode | Configure <code>mode</code> in the generated base webpack config.<br><br><code>mode</code> is set to <code>production</code> if the Bazel compilation mode is <code>opt</code> otherwise it is set to <code>development</code>.<br><br>The configured value will be overridden if it is set in a supplied <code>webpack_config</code>.<br><br>See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode. | <code>True</code> |
38+
| <a id="webpack_bundle-configure_devtool"></a>configure_devtool | Configure <code>devtool</code> in the generated base webpack config.<br><br><code>devtool</code> is set to <code>eval</code> if the Bazel compilation mode is <code>fastbuild</code>, <code>eval-source-map</code> if the Bazel compilation mode is <code>dbg</code>, otherwise it is left unset.<br><br>The configured value will be overridden if it is set in a supplied <code>webpack_config</code>.<br><br>See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode. | <code>True</code> |
3639
| <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> |
3740
| <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> |
3841
| <a id="webpack_bundle-kwargs"></a>kwargs | Additional arguments | none |
@@ -43,8 +46,8 @@ Runs the webpack-cli under bazel
4346
## webpack_devserver
4447

4548
<pre>
46-
webpack_devserver(<a href="#webpack_devserver-name">name</a>, <a href="#webpack_devserver-node_modules">node_modules</a>, <a href="#webpack_devserver-chdir">chdir</a>, <a href="#webpack_devserver-env">env</a>, <a href="#webpack_devserver-entry_point">entry_point</a>, <a href="#webpack_devserver-entry_points">entry_points</a>, <a href="#webpack_devserver-webpack_config">webpack_config</a>, <a href="#webpack_devserver-args">args</a>,
47-
<a href="#webpack_devserver-data">data</a>, <a href="#webpack_devserver-mode">mode</a>, <a href="#webpack_devserver-kwargs">kwargs</a>)
49+
webpack_devserver(<a href="#webpack_devserver-name">name</a>, <a href="#webpack_devserver-node_modules">node_modules</a>, <a href="#webpack_devserver-chdir">chdir</a>, <a href="#webpack_devserver-env">env</a>, <a href="#webpack_devserver-entry_point">entry_point</a>, <a href="#webpack_devserver-entry_points">entry_points</a>, <a href="#webpack_devserver-webpack_config">webpack_config</a>,
50+
<a href="#webpack_devserver-configure_mode">configure_mode</a>, <a href="#webpack_devserver-configure_devtool">configure_devtool</a>, <a href="#webpack_devserver-args">args</a>, <a href="#webpack_devserver-data">data</a>, <a href="#webpack_devserver-mode">mode</a>, <a href="#webpack_devserver-kwargs">kwargs</a>)
4851
</pre>
4952

5053
Runs the webpack devserver.
@@ -66,6 +69,8 @@ under the hood.
6669
| <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> |
6770
| <a id="webpack_devserver-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>Only one of <code>entry_point</code> to <code>entry_points</code> must be specified. | <code>{}</code> |
6871
| <a id="webpack_devserver-webpack_config"></a>webpack_config | Webpack configuration file. See https://webpack.js.org/configuration/. | <code>None</code> |
72+
| <a id="webpack_devserver-configure_mode"></a>configure_mode | Configure <code>mode</code> in the generated base webpack config.<br><br><code>mode</code> is set to <code>production</code> if the Bazel compilation mode is <code>opt</code> otherwise it is set to <code>development</code>.<br><br>The configured value will be overridden if it is set in a supplied <code>webpack_config</code>.<br><br>See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode. | <code>True</code> |
73+
| <a id="webpack_devserver-configure_devtool"></a>configure_devtool | Configure <code>devtool</code> in the generated base webpack config.<br><br><code>devtool</code> is set to <code>eval</code> if the Bazel compilation mode is <code>fastbuild</code>, <code>eval-source-map</code> if the Bazel compilation mode is <code>dbg</code>, otherwise it is left unset.<br><br>The configured value will be overridden if it is set in a supplied <code>webpack_config</code>.<br><br>See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode. | <code>True</code> |
6974
| <a id="webpack_devserver-args"></a>args | Additional arguments to pass to webpack.<br><br>The <code>serve</code> command, the webpack config file (<code>--config</code>) and the mode (<code>--mode</code>) are automatically set. | <code>[]</code> |
7075
| <a id="webpack_devserver-data"></a>data | Bundle and runtime dependencies of the program.<br><br>Should include the <code>webpack_bundle</code> rule <code>srcs</code> and <code>deps</code>.<br><br>The webpack config and entry_point[s] are automatically passed to data and should not be repeated. | <code>[]</code> |
7176
| <a id="webpack_devserver-mode"></a>mode | The mode to pass to <code>--mode</code>. | <code>"development"</code> |

webpack/private/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ bzl_library(
3838
)
3939

4040
exports_files([
41-
"webpack.config.js",
41+
"webpack.config.js.tmpl",
4242
"webpack_worker.js",
4343
])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
infrastructureLogging: {
3+
colors: false,
4+
console: new console.Console(process.stderr, process.stderr),
5+
level: 'error',
6+
},
7+
// We want webpack to spit out deterministic file hashes and module ids
8+
// this will improve remote caching
9+
// https://webpack.js.org/guides/caching/#module-identifiers
10+
optimization: {
11+
moduleIds: 'deterministic',
12+
chunkIds: 'deterministic',
13+
},
14+
output: {
15+
// We want deterministic sourceMaps among other stuff that depends on the unique name.
16+
// Default behavior of this field causes non-hermetic behavior. Eg; it looks into package.json
17+
// which is always present in a non-sandboxed environment.
18+
// See: https://webpack.js.org/configuration/output/#outputuniquename
19+
// and https://webpack.js.org/configuration/output/#outputdevtoolmodulefilenametemplate
20+
uniqueName: process.env.BAZEL_WORKSPACE,
21+
},{{ENTRIES}}{{DEVTOOL}}{{MODE}}
22+
}

webpack/private/webpack_bundle.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def webpack_bundle(
255255
entry_point = None,
256256
entry_points = {},
257257
webpack_config = None,
258+
configure_mode = True,
259+
configure_devtool = True,
258260
use_execroot_entry_point = True,
259261
supports_workers = False,
260262
**kwargs):
@@ -323,6 +325,23 @@ def webpack_bundle(
323325
324326
See https://webpack.js.org/configuration/
325327
328+
configure_mode: Configure `mode` in the generated base webpack config.
329+
330+
`mode` is set to `production` if the Bazel compilation mode is `opt` otherwise it is set to `development`.
331+
332+
The configured value will be overridden if it is set in a supplied `webpack_config`.
333+
334+
See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode.
335+
336+
configure_devtool: Configure `devtool` in the generated base webpack config.
337+
338+
`devtool` is set to `eval` if the Bazel compilation mode is `fastbuild`, `eval-source-map` if the Bazel
339+
compilation mode is `dbg`, otherwise it is left unset.
340+
341+
The configured value will be overridden if it is set in a supplied `webpack_config`.
342+
343+
See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode.
344+
326345
use_execroot_entry_point: Use the `entry_point` script of the `webpack` `js_binary` that is in the execroot output tree instead of the copy that is in runfiles.
327346
328347
When set, runfiles are hoisted to the target platform when this is configured and included as target
@@ -353,6 +372,8 @@ def webpack_bundle(
353372
entry_point = entry_point,
354373
entry_points = entry_points,
355374
webpack_config = webpack_config,
375+
configure_mode = configure_mode,
376+
configure_devtool = configure_devtool,
356377
chdir = chdir,
357378
entry_points_mandatory = not output_dir,
358379
)

webpack/private/webpack_create_configs.bzl

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
44

55
_config_attrs = {
66
"chdir": attr.string(),
7-
"entry_points": attr.label_keyed_string_dict(
8-
allow_files = True,
9-
),
7+
"entry_points": attr.label_keyed_string_dict(allow_files = True),
108
"config_out": attr.output(),
119
"output_dir": attr.bool(),
12-
"_webpack_config_file": attr.label(
13-
doc = "Internal use only",
14-
allow_single_file = [".js"],
15-
default = Label("//webpack/private:webpack.config.js"),
10+
"configure_mode": attr.bool(),
11+
"configure_devtool": attr.bool(),
12+
"_webpack_config_template": attr.label(
13+
allow_single_file = True,
14+
default = Label("//webpack/private:webpack.config.js.tmpl"),
1615
),
1716
}
1817

@@ -55,25 +54,30 @@ def _create_base_config_impl(ctx):
5554
# See: https://docs.bazel.build/versions/main/user-manual.html#flag--compilation_mode
5655
# See: https://webpack.js.org/configuration/devtool/#devtool
5756
compilation_mode = ctx.var["COMPILATION_MODE"]
58-
devtool = None
59-
mode = "development"
6057

61-
if compilation_mode == "fastbuild":
62-
devtool = "eval"
63-
elif compilation_mode == "dbg":
64-
devtool = "eval-source-map"
65-
elif compilation_mode == "opt":
66-
mode = "production"
58+
mode = None
59+
if ctx.attr.configure_mode:
60+
if compilation_mode == "opt":
61+
mode = "production"
62+
else:
63+
mode = "development"
64+
65+
devtool = None
66+
if ctx.attr.configure_devtool:
67+
if compilation_mode == "fastbuild":
68+
devtool = "eval"
69+
elif compilation_mode == "dbg":
70+
devtool = "eval-source-map"
6771

6872
# Expand webpack config for the entry mapping
6973
inputs.append(config)
7074
ctx.actions.expand_template(
71-
template = ctx.file._webpack_config_file,
75+
template = ctx.file._webpack_config_template,
7276
output = ctx.outputs.config_out,
7377
substitutions = {
74-
"{ ENTRIES }": json.encode(entry_mapping),
75-
"devtool: 'DEVTOOL',": "devtool: '{}',".format(devtool) if devtool else "",
76-
"mode: 'MODE',": "mode: '{}',".format(mode),
78+
"{{ENTRIES}}": "\n entry: {},".format(json.encode(entry_mapping)) if entry_mapping else "",
79+
"{{DEVTOOL}}": "\n devtool: '{}',".format(devtool) if devtool else "",
80+
"{{MODE}}": "\n mode: '{}',".format(mode) if mode else "",
7781
},
7882
)
7983

@@ -83,7 +87,15 @@ _create_base_config = rule(
8387
doc = "",
8488
)
8589

86-
def webpack_create_configs(name, entry_point, entry_points, webpack_config, chdir, entry_points_mandatory):
90+
def webpack_create_configs(
91+
name,
92+
entry_point,
93+
entry_points,
94+
webpack_config,
95+
configure_mode,
96+
configure_devtool,
97+
chdir,
98+
entry_points_mandatory):
8799
"""
88100
Internal use only. Not public API.
89101
@@ -94,6 +106,8 @@ def webpack_create_configs(name, entry_point, entry_points, webpack_config, chdi
94106
entry_point: a single entry
95107
entry_points: multiple entries
96108
webpack_config: a custom webpack config file
109+
configure_mode: configure `mode` in the generated base webpack config
110+
configure_devtool: configure `devtool` in the generated base webpack config
97111
chdir: the dir webpack is run in
98112
entry_points_mandatory: whether or not entry points must be specified
99113
@@ -111,6 +125,8 @@ def webpack_create_configs(name, entry_point, entry_points, webpack_config, chdi
111125
name = "_%s_config" % name,
112126
config_out = default_config,
113127
entry_points = {entry_point: name} if entry_point else entry_points,
128+
configure_mode = configure_mode,
129+
configure_devtool = configure_devtool,
114130
chdir = chdir,
115131
tags = ["manual"],
116132
)

webpack/private/webpack_devserver.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def webpack_devserver(
1212
entry_point = None,
1313
entry_points = {},
1414
webpack_config = None,
15+
configure_mode = True,
16+
configure_devtool = True,
1517
args = [],
1618
data = [],
1719
mode = "development",
@@ -46,6 +48,23 @@ def webpack_devserver(
4648
4749
webpack_config: Webpack configuration file. See https://webpack.js.org/configuration/.
4850
51+
configure_mode: Configure `mode` in the generated base webpack config.
52+
53+
`mode` is set to `production` if the Bazel compilation mode is `opt` otherwise it is set to `development`.
54+
55+
The configured value will be overridden if it is set in a supplied `webpack_config`.
56+
57+
See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode.
58+
59+
configure_devtool: Configure `devtool` in the generated base webpack config.
60+
61+
`devtool` is set to `eval` if the Bazel compilation mode is `fastbuild`, `eval-source-map` if the Bazel
62+
compilation mode is `dbg`, otherwise it is left unset.
63+
64+
The configured value will be overridden if it is set in a supplied `webpack_config`.
65+
66+
See https://bazel.build/docs/user-manual#compilation-mode for more info on how to configure the compilation mode.
67+
4968
args: Additional arguments to pass to webpack.
5069
5170
The `serve` command, the webpack config file (`--config`) and the mode (`--mode`) are
@@ -91,6 +110,8 @@ def webpack_devserver(
91110
entry_point = entry_point,
92111
entry_points = entry_points,
93112
webpack_config = webpack_config,
113+
configure_mode = configure_mode,
114+
configure_devtool = configure_devtool,
94115
chdir = chdir,
95116
entry_points_mandatory = False, # devserver rule doesn't have outputs so entry points are not needed to predict output files
96117
)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
2+
load("//webpack/private:webpack_create_configs.bzl", "webpack_create_configs")
3+
4+
webpack_create_configs(
5+
name = "basic",
6+
chdir = None,
7+
configure_devtool = False,
8+
configure_mode = False,
9+
entry_point = None,
10+
entry_points = {},
11+
entry_points_mandatory = False,
12+
webpack_config = None,
13+
)
14+
15+
webpack_create_configs(
16+
name = "devtool_mode",
17+
chdir = None,
18+
configure_devtool = True,
19+
configure_mode = True,
20+
entry_point = None,
21+
entry_points = {},
22+
entry_points_mandatory = False,
23+
webpack_config = None,
24+
)
25+
26+
webpack_create_configs(
27+
name = "entry",
28+
chdir = None,
29+
configure_devtool = True,
30+
configure_mode = True,
31+
entry_point = "entry.js",
32+
entry_points = {},
33+
entry_points_mandatory = False,
34+
webpack_config = None,
35+
)
36+
37+
webpack_create_configs(
38+
name = "entries",
39+
chdir = None,
40+
configure_devtool = True,
41+
configure_mode = True,
42+
entry_point = None,
43+
entry_points = {
44+
"foo": "foo.js",
45+
"bar": "bar.js",
46+
},
47+
entry_points_mandatory = False,
48+
webpack_config = None,
49+
)
50+
51+
webpack_create_configs(
52+
name = "chdir",
53+
chdir = package_name(),
54+
configure_devtool = True,
55+
configure_mode = True,
56+
entry_point = None,
57+
entry_points = {
58+
"foo": "foo.js",
59+
"bar": "bar.js",
60+
},
61+
entry_points_mandatory = False,
62+
webpack_config = None,
63+
)
64+
65+
write_source_files(
66+
name = "tests",
67+
files = {
68+
"expected.basic.webpack.js": "basic.webpack.config.js",
69+
"expected.devtool_mode.webpack.js": "devtool_mode.webpack.config.js",
70+
"expected.entry.webpack.js": "entry.webpack.config.js",
71+
"expected.entries.webpack.js": "entries.webpack.config.js",
72+
"expected.chdir.webpack.js": "chdir.webpack.config.js",
73+
},
74+
)

webpack/private/webpack.config.js renamed to webpack/tests/create_configs/expected.basic.webpack.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/** @type {import("webpack").Configuration} */
21
module.exports = {
3-
entry: { ENTRIES },
42
infrastructureLogging: {
53
colors: false,
64
console: new console.Console(process.stderr, process.stderr),
@@ -21,6 +19,4 @@ module.exports = {
2119
// and https://webpack.js.org/configuration/output/#outputdevtoolmodulefilenametemplate
2220
uniqueName: process.env.BAZEL_WORKSPACE,
2321
},
24-
devtool: 'DEVTOOL',
25-
mode: 'MODE',
2622
}

0 commit comments

Comments
 (0)