@@ -25,6 +25,14 @@ load("//internal:ts_config.bzl", "TsConfigInfo")
2525_DEFAULT_COMPILER = "@npm//@bazel/typescript/bin:tsc_wrapped"
2626_DEFAULT_NODE_MODULES = Label ("@npm//typescript:typescript__typings" )
2727
28+ _TYPESCRIPT_SCRIPT_TARGETS = ["es3" , "es5" , "es2015" , "es2016" , "es2017" , "es2018" , "esnext" ]
29+ _TYPESCRIPT_MODULE_KINDS = ["none" , "commonjs" , "amd" , "umd" , "system" , "es2015" , "esnext" ]
30+
31+ _DEVMODE_TARGET_DEFAULT = "es2015"
32+ _DEVMODE_MODULE_DEFAULT = "umd"
33+ _PRODMODE_TARGET_DEFAULT = "es2015"
34+ _PRODMODE_MODULE_DEFAULT = "esnext"
35+
2836def _trim_package_node_modules (package_name ):
2937 # trim a package name down to its path prior to a node_modules
3038 # segment. 'foo/node_modules/bar' would become 'foo' and
@@ -219,9 +227,15 @@ def tsc_wrapped_tsconfig(
219227 )
220228 config ["bazelOptions" ]["nodeModulesPrefix" ] = node_modules_root
221229
222- # Override the target so we use es2015 for devmode
223- # Since g3 isn't ready to do this yet
224- config ["compilerOptions" ]["target" ] = "es2015"
230+ # Control target & module via attributes
231+ if devmode_manifest :
232+ # NB: devmode target may still be overriden with a tsconfig bazelOpts.devmodeTargetOverride but that
233+ # configuration settings will be removed in a future major release
234+ config ["compilerOptions" ]["target" ] = ctx .attr .devmode_target if hasattr (ctx .attr , "devmode_target" ) else _DEVMODE_TARGET_DEFAULT
235+ config ["compilerOptions" ]["module" ] = ctx .attr .devmode_module if hasattr (ctx .attr , "devmode_module" ) else _DEVMODE_MODULE_DEFAULT
236+ else :
237+ config ["compilerOptions" ]["target" ] = ctx .attr .prodmode_target if hasattr (ctx .attr , "prodmode_target" ) else _PRODMODE_TARGET_DEFAULT
238+ config ["compilerOptions" ]["module" ] = ctx .attr .prodmode_module if hasattr (ctx .attr , "prodmode_module" ) else _PRODMODE_MODULE_DEFAULT
225239
226240 # It's fine for users to have types[] in their tsconfig.json to help the editor
227241 # know which of the node_modules/@types/* entries to include in the program.
@@ -318,6 +332,20 @@ the compiler attribute manually.
318332 executable = True ,
319333 cfg = "host" ,
320334 ),
335+ "devmode_module" : attr .string (
336+ doc = """Set the typescript `module` compiler option for devmode output.
337+
338+ This value will override the `module` option in the user supplied tsconfig.""" ,
339+ values = _TYPESCRIPT_MODULE_KINDS ,
340+ default = _DEVMODE_MODULE_DEFAULT ,
341+ ),
342+ "devmode_target" : attr .string (
343+ doc = """Set the typescript `target` compiler option for devmode output.
344+
345+ This value will override the `target` option in the user supplied tsconfig.""" ,
346+ values = _TYPESCRIPT_SCRIPT_TARGETS ,
347+ default = _DEVMODE_TARGET_DEFAULT ,
348+ ),
321349 "internal_testing_type_check_dependencies" : attr .bool (default = False , doc = "Testing only, whether to type check inputs that aren't srcs." ),
322350 "node_modules" : attr .label (
323351 doc = """The npm packages which should be available during the compile.
@@ -384,6 +412,20 @@ yarn_install(
384412""" ,
385413 default = _DEFAULT_NODE_MODULES ,
386414 ),
415+ "prodmode_module" : attr .string (
416+ doc = """Set the typescript `module` compiler option for prodmode output.
417+
418+ This value will override the `module` option in the user supplied tsconfig.""" ,
419+ values = _TYPESCRIPT_MODULE_KINDS ,
420+ default = _PRODMODE_MODULE_DEFAULT ,
421+ ),
422+ "prodmode_target" : attr .string (
423+ doc = """Set the typescript `target` compiler option for prodmode output.
424+
425+ This value will override the `target` option in the user supplied tsconfig.""" ,
426+ values = _TYPESCRIPT_SCRIPT_TARGETS ,
427+ default = _PRODMODE_TARGET_DEFAULT ,
428+ ),
387429 "supports_workers" : attr .bool (
388430 doc = """Intended for internal use only.
389431
0 commit comments