@@ -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 (
@@ -228,12 +272,13 @@ diff_test(
228272
229273write_file (
230274 name = "write5" ,
231- out = "case5.js" ,
232- content = ["""\
233- require('fs').writeFileSync(
234- process.argv[2],
235- JSON.stringify(require(require('path').join(process.cwd(), "data.json")))
236- )""" ],
275+ out = "case5.mjs" ,
276+ content = [
277+ "import { writeFileSync } from 'fs';" ,
278+ "import { join } from 'path';" ,
279+ "const jsonData = await import(join(process.cwd(), 'data.json'), { with: { type: 'json' } });" ,
280+ "writeFileSync(process.argv[2], JSON.stringify(jsonData.default))" ,
281+ ],
237282)
238283
239284write_file (
@@ -244,7 +289,7 @@ write_file(
244289
245290js_binary (
246291 name = "bin5" ,
247- entry_point = "case5.js " ,
292+ entry_point = "case5.mjs " ,
248293)
249294
250295js_run_binary (
@@ -557,11 +602,13 @@ js_test(
557602
558603write_file (
559604 name = "write13" ,
560- out = "case13.js " ,
605+ out = "case13.mjs " ,
561606 content = [
562- """require('fs').writeFileSync(
607+ "import { writeFileSync } from 'fs';" ,
608+ """
609+ writeFileSync(
563610 process.argv[2],
564- JSON.stringify(require( process.argv[3]))
611+ JSON.stringify((await import( process.argv[3])).default )
565612 )""" ,
566613 ],
567614)
@@ -572,7 +619,7 @@ js_binary(
572619 "//:node_modules/@mycorp/pkg-c1" ,
573620 "//:node_modules/@mycorp/pkg-c2" ,
574621 ],
575- entry_point = "case13.js " ,
622+ entry_point = "case13.mjs " ,
576623)
577624
578625write_file (
0 commit comments