@@ -25,7 +25,14 @@ js_binary(
2525 name = "bin" ,
2626 # Reference the location where the acorn npm module was linked in the root Bazel package
2727 data = ["//:node_modules/acorn" ],
28- entry_point = "require_acorn.js" ,
28+ entry_point = "require_acorn.mjs" ,
29+ )
30+
31+ js_binary (
32+ name = "bin_cjs" ,
33+ # Reference the location where the acorn npm module was linked in the root Bazel package
34+ data = ["//:node_modules/acorn" ],
35+ entry_point = "require_acorn.cjs" ,
2936)
3037
3138####################################################
@@ -48,6 +55,22 @@ diff_test(
4855 file2 = "out1" ,
4956)
5057
58+ genrule (
59+ name = "run1_cjs" ,
60+ srcs = [],
61+ outs = ["out1_cjs" ],
62+ # All js_binary rules need a BAZEL_BINDIR environment variable set so they can
63+ # run from that directory as the working directory.
64+ cmd = "BAZEL_BINDIR=$(BINDIR) $(location :bin_cjs) {}/out1_cjs" .format (package_name ()),
65+ tools = [":bin_cjs" ],
66+ )
67+
68+ diff_test (
69+ name = "test_js_binary_under_genrule_cjs" ,
70+ file1 = "//examples:expected_one_ast.json" ,
71+ file2 = "out1_cjs" ,
72+ )
73+
5174####################################################
5275# Use case 2
5376# Using js_run_binary has some nice syntax sugar vs.
@@ -73,6 +96,26 @@ diff_test(
7396 file2 = "out2" ,
7497)
7598
99+ js_run_binary (
100+ name = "run2_cjs" ,
101+ srcs = [],
102+ outs = ["out2_cjs" ],
103+ args = ["out2_cjs" ],
104+ chdir = package_name (),
105+ # Request that the rules_js launcher prints extra information
106+ log_level = "debug" ,
107+ tool = ":bin_cjs" ,
108+ # Uncomment the setting below to see debug output even on a
109+ # successful run of the build action.
110+ # silent_on_success = False,
111+ )
112+
113+ diff_test (
114+ name = "test_js_binary_under_js_run_binary_cjs" ,
115+ file1 = "//examples:expected_one_ast.json" ,
116+ file2 = "out2_cjs" ,
117+ )
118+
76119# Also test with local (no sandbox) execution by setting execution_requirements "local" to "1".
77120# Bazel sets different environment variables in this case such as RUNFILES_MANIFEST_FILE.
78121# This case tests for regression of the fix in https://github.com/aspect-build/rules_js/pull/323.
@@ -101,16 +144,16 @@ diff_test(
101144
102145# Also test with copy_data_to_bin disabled
103146copy_to_bin (
104- name = "require_acorn_js " ,
105- srcs = ["require_acorn.js " ],
147+ name = "require_acorn_mjs " ,
148+ srcs = ["require_acorn.mjs " ],
106149)
107150
108151js_binary (
109152 name = "bin_no_copy_data_to_bin" ,
110153 copy_data_to_bin = False ,
111154 # Reference the location where the acorn npm module was linked in the root Bazel package
112155 data = ["//:node_modules/acorn" ],
113- entry_point = ":require_acorn_js " ,
156+ entry_point = ":require_acorn_mjs " ,
114157)
115158
116159js_run_binary (
@@ -142,7 +185,7 @@ diff_test(
142185js_test (
143186 name = "test_test" ,
144187 data = ["//:node_modules/@types/node" ],
145- entry_point = "test.js " ,
188+ entry_point = "test.mjs " ,
146189)
147190
148191###############################
@@ -152,11 +195,12 @@ js_test(
152195
153196write_file (
154197 name = "write4" ,
155- out = "case4.js " ,
198+ out = "case4.mjs " ,
156199 content = [
157- """require('fs').writeFileSync(
200+ "import { writeFileSync } from 'fs'" ,
201+ """writeFileSync(
158202 process.argv[2],
159- require( process.argv[3]).toAst("1")
203+ (await import( process.argv[3]) ).toAst("1")
160204 )""" ,
161205 ],
162206)
@@ -167,7 +211,7 @@ js_binary(
167211 ":node_modules/@mycorp/pkg-a" ,
168212 "//:node_modules/@mycorp/pkg-b" ,
169213 ],
170- entry_point = "case4.js " ,
214+ entry_point = "case4.mjs " ,
171215)
172216
173217js_run_binary (
@@ -557,11 +601,13 @@ js_test(
557601
558602write_file (
559603 name = "write13" ,
560- out = "case13.js " ,
604+ out = "case13.mjs " ,
561605 content = [
562- """require('fs').writeFileSync(
606+ "import { writeFileSync } from 'fs';" ,
607+ """
608+ writeFileSync(
563609 process.argv[2],
564- JSON.stringify(require( process.argv[3]))
610+ JSON.stringify((await import( process.argv[3])).default )
565611 )""" ,
566612 ],
567613)
@@ -572,7 +618,7 @@ js_binary(
572618 "//:node_modules/@mycorp/pkg-c1" ,
573619 "//:node_modules/@mycorp/pkg-c2" ,
574620 ],
575- entry_point = "case13.js " ,
621+ entry_point = "case13.mjs " ,
576622)
577623
578624write_file (
0 commit comments