Skip to content

Commit

Permalink
feat(typescript): add devmode_target, devmode_module, prodmode_target…
Browse files Browse the repository at this point in the history
… & prodmode_module attributes

This allows users control over the language level & module formats produced by both the dev and prod outputs of ts_library
  • Loading branch information
gregmagolan committed Mar 27, 2020
1 parent 51785e5 commit 5d373c0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/typescript/src/internal/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ load("//internal:ts_config.bzl", "TsConfigInfo")
_DEFAULT_COMPILER = "@npm//@bazel/typescript/bin:tsc_wrapped"
_DEFAULT_NODE_MODULES = Label("@npm//typescript:typescript__typings")

_TYPESCRIPT_SCRIPT_TARGETS = ["es3", "es5", "es2015", "es2016", "es2017", "es2018", "esnext"]
_TYPESCRIPT_MODULE_KINDS = ["none", "commonjs", "amd", "umd", "system", "es2015", "esnext"]

def _trim_package_node_modules(package_name):
# trim a package name down to its path prior to a node_modules
# segment. 'foo/node_modules/bar' would become 'foo' and
Expand Down Expand Up @@ -219,9 +222,11 @@ def tsc_wrapped_tsconfig(
)
config["bazelOptions"]["nodeModulesPrefix"] = node_modules_root

# Override the target so we use es2015 for devmode
# Since g3 isn't ready to do this yet
config["compilerOptions"]["target"] = "es2015"
# Control target & module via attributes
# NB: target may still be overriden with a tsconfig bazelOpts.devmodeTargetOverride but that
# configuration settings will be removed in a future major release
config["compilerOptions"]["target"] = ctx.attr.devmode_target if devmode_manifest else ctx.attr.prodmode_target
config["compilerOptions"]["module"] = ctx.attr.devmode_module if devmode_manifest else ctx.attr.prodmode_module

# It's fine for users to have types[] in their tsconfig.json to help the editor
# know which of the node_modules/@types/* entries to include in the program.
Expand Down Expand Up @@ -318,6 +323,20 @@ the compiler attribute manually.
executable = True,
cfg = "host",
),
"devmode_module": attr.string(
doc = """Set the typescript `module` compiler option for devmode output.
This value will override the `module` option in the user supplied tsconfig.""",
values = _TYPESCRIPT_MODULE_KINDS,
default = "umd",
),
"devmode_target": attr.string(
doc = """Set the typescript `target` compiler option for devmode output.
This value will override the `target` option in the user supplied tsconfig.""",
values = _TYPESCRIPT_SCRIPT_TARGETS,
default = "es2015",
),
"internal_testing_type_check_dependencies": attr.bool(default = False, doc = "Testing only, whether to type check inputs that aren't srcs."),
"node_modules": attr.label(
doc = """The npm packages which should be available during the compile.
Expand Down Expand Up @@ -384,6 +403,20 @@ yarn_install(
""",
default = _DEFAULT_NODE_MODULES,
),
"prodmode_module": attr.string(
doc = """Set the typescript `module` compiler option for prodmode output.
This value will override the `module` option in the user supplied tsconfig.""",
values = _TYPESCRIPT_MODULE_KINDS,
default = "esnext",
),
"prodmode_target": attr.string(
doc = """Set the typescript `target` compiler option for prodmode output.
This value will override the `target` option in the user supplied tsconfig.""",
values = _TYPESCRIPT_SCRIPT_TARGETS,
default = "es2015",
),
"supports_workers": attr.bool(
doc = """Intended for internal use only.
Expand Down
33 changes: 33 additions & 0 deletions packages/typescript/test/target_module_attributes/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library")
load("//packages/typescript/test/devmode_consumer:devmode_consumer.bzl", "devmode_consumer")
load("//packages/typescript/test/es6_consumer:es6_consumer.bzl", "es6_consumer")

ts_library(
name = "override",
srcs = ["a.ts"],
devmode_module = "amd",
devmode_target = "es5",
prodmode_module = "amd",
prodmode_target = "es5",
tsconfig = ":tsconfig-override.json",
)

devmode_consumer(
name = "override_devmode_output",
deps = [":override"],
)

es6_consumer(
name = "override_prodmode_output",
deps = [":override"],
)

jasmine_node_test(
name = "override_output_test",
srcs = ["override_output_test.js"],
data = [
":override_devmode_output",
":override_prodmode_output",
],
)
1 change: 1 addition & 0 deletions packages/typescript/test/target_module_attributes/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a: any = () => 'hello world';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs');
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);

describe('googmodule', () => {
let devmodeOutput;
let prodmodeOutput;
beforeAll(() => {
devmodeOutput = runfiles.resolvePackageRelative('a.js');
prodmodeOutput = runfiles.resolvePackageRelative('a.mjs');
});

it('should have amd module syntax in devmode', () => {
expect(fs.readFileSync(devmodeOutput, {encoding: 'utf-8'}))
.toContain(
`define("build_bazel_rules_nodejs/packages/typescript/test/target_module_attributes/a", ["require", "exports"], function (require, exports) {`);
});

it('should have es5 in devmode', () => {
expect(fs.readFileSync(devmodeOutput, {
encoding: 'utf-8'
})).toContain(`exports.a = function () { return 'hello world'; };`);
});

it('should have amd module syntax in prodmode', () => {
expect(fs.readFileSync(prodmodeOutput, {encoding: 'utf-8'}))
.toContain(
`define("build_bazel_rules_nodejs/packages/typescript/test/target_module_attributes/a", ["require", "exports"], function (require, exports) {`);
});

it('should have es5 in prodmode', () => {
expect(fs.readFileSync(prodmodeOutput, {
encoding: 'utf-8'
})).toContain(`exports.a = function () { return 'hello world'; };`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
// Explicitly set types settings so typescript doesn't auto-discover types.
// If all types are discovered then all types need to be included as deps
// or typescript may error out with TS2688: Cannot find type definition file for 'foo'.
"types": []
},
}

0 comments on commit 5d373c0

Please sign in to comment.