22esbuild rule
33"""
44
5- load ("@build_bazel_rules_nodejs//:providers.bzl" , "JSEcmaScriptModuleInfo" , "JSModuleInfo" , "NpmPackageInfo" , "node_modules_aspect" )
5+ load ("@build_bazel_rules_nodejs//:index.bzl" , "nodejs_binary" )
6+ load ("@build_bazel_rules_nodejs//:providers.bzl" , "JSEcmaScriptModuleInfo" , "JSModuleInfo" , "NpmPackageInfo" , "node_modules_aspect" , "run_node" )
67load ("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl" , "MODULE_MAPPINGS_ASPECT_RESULTS_NAME" , "module_mappings_aspect" )
78load (":helpers.bzl" , "filter_files" , "generate_path_mapping" , "resolve_entry_point" , "write_jsconfig_file" )
89
@@ -14,10 +15,6 @@ def _esbuild_impl(ctx):
1415 # Path alias mapings are used to create a jsconfig with mappings so that esbuild
1516 # how to resolve custom package or module names
1617 path_alias_mappings = dict ()
17- npm_workspaces = []
18-
19- if (ctx .attr .link_workspace_root ):
20- path_alias_mappings .update (generate_path_mapping (ctx .workspace_name , "." ))
2118
2219 for dep in ctx .attr .deps :
2320 if JSEcmaScriptModuleInfo in dep :
@@ -33,19 +30,12 @@ def _esbuild_impl(ctx):
3330
3431 if NpmPackageInfo in dep :
3532 deps_depsets .append (dep [NpmPackageInfo ].sources )
36- npm_workspaces .append (dep [NpmPackageInfo ].workspace )
3733
3834 # Collect the path alias mapping to resolve packages correctly
3935 if hasattr (dep , MODULE_MAPPINGS_ASPECT_RESULTS_NAME ):
4036 for key , value in getattr (dep , MODULE_MAPPINGS_ASPECT_RESULTS_NAME ).items ():
4137 path_alias_mappings .update (generate_path_mapping (key , value [1 ].replace (ctx .bin_dir .path + "/" , "" )))
4238
43- node_modules_mappings = [
44- "../../../external/%s/node_modules/*" % workspace
45- for workspace in depset (npm_workspaces ).to_list ()
46- ]
47- path_alias_mappings .update ({"*" : node_modules_mappings })
48-
4939 deps_inputs = depset (transitive = deps_depsets ).to_list ()
5040 inputs = filter_files (ctx .files .entry_point ) + ctx .files .srcs + deps_inputs
5141
@@ -55,6 +45,7 @@ def _esbuild_impl(ctx):
5545 entry_point = resolve_entry_point (ctx .file .entry_point , inputs , ctx .files .srcs )
5646
5747 args = ctx .actions .args ()
48+ args .use_param_file (param_file_arg = "--esbuild_flags=%s" , use_always = True )
5849
5950 args .add ("--bundle" , entry_point .path )
6051
@@ -125,19 +116,25 @@ def _esbuild_impl(ctx):
125116 if "no-remote-exec" in ctx .attr .tags :
126117 execution_requirements = {"no-remote-exec" : "1" }
127118
128- ctx .actions .run (
119+ launcher_args = ctx .actions .args ()
120+ launcher_args .add ("--esbuild=%s" % ctx .executable .tool .path )
121+
122+ run_node (
123+ ctx = ctx ,
129124 inputs = depset (inputs ),
130125 outputs = outputs ,
131- executable = ctx .executable .tool ,
132- arguments = [args ],
126+ arguments = [launcher_args , args ],
133127 progress_message = "%s Javascript %s [esbuild]" % ("Bundling" if not ctx .attr .output_dir else "Splitting" , entry_point .short_path ),
134128 execution_requirements = execution_requirements ,
135129 mnemonic = "esbuild" ,
136130 env = env ,
131+ executable = "launcher" ,
132+ link_workspace_root = ctx .attr .link_workspace_root ,
133+ tools = [ctx .executable .tool ],
137134 )
138135
139136 return [
140- DefaultInfo (files = depset (outputs + [ jsconfig_file ] )),
137+ DefaultInfo (files = depset (outputs )),
141138 ]
142139
143140esbuild = rule (
@@ -189,6 +186,12 @@ and cjs when platform is node. If performing code splitting, defaults to esm.
189186See https://esbuild.github.io/api/#format for more details
190187 """ ,
191188 ),
189+ "launcher" : attr .label (
190+ mandatory = True ,
191+ executable = True ,
192+ doc = "Internal use only" ,
193+ cfg = "exec" ,
194+ ),
192195 "link_workspace_root" : attr .bool (
193196 doc = """Link the workspace root to the bin_dir to support absolute requires like 'my_wksp/path/to/file'.
194197 If source files need to be required then they can be copied to the bin_dir with copy_to_bin.""" ,
@@ -296,10 +299,18 @@ def esbuild_macro(name, output_dir = False, **kwargs):
296299 **kwargs: All other args from `esbuild_bundle`
297300 """
298301
302+ kwargs .pop ("launcher" , None )
303+ _launcher = "_%s_esbuild_launcher" % name
304+ nodejs_binary (
305+ name = _launcher ,
306+ entry_point = Label ("@build_bazel_rules_nodejs//packages/esbuild:launcher.js" ),
307+ )
308+
299309 if output_dir == True :
300310 esbuild (
301311 name = name ,
302312 output_dir = True ,
313+ launcher = _launcher ,
303314 ** kwargs
304315 )
305316 else :
@@ -316,5 +327,6 @@ def esbuild_macro(name, output_dir = False, **kwargs):
316327 name = name ,
317328 output = output ,
318329 output_map = output_map ,
330+ launcher = _launcher ,
319331 ** kwargs
320332 )
0 commit comments