Skip to content

Commit a7d56ed

Browse files
build: add --output-transpiled-ts configuration
1 parent c1b80f2 commit a7d56ed

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

configure.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,12 @@
11691169
default=False,
11701170
help='node will load builtin modules from disk instead of from binary')
11711171

1172+
parser.add_argument('--output-transpiled-ts',
1173+
action='store',
1174+
dest='output_transpiled_ts',
1175+
default='',
1176+
help='write transpiled TypeScript output to the given directory for debugging')
1177+
11721178
parser.add_argument('--node-snapshot-main',
11731179
action='store',
11741180
dest='node_snapshot_main',
@@ -1976,6 +1982,11 @@ def configure_node(o):
19761982
print('Warning! Loading builtin modules from disk is for development')
19771983
o['variables']['node_builtin_modules_path'] = options.node_builtin_modules_path
19781984

1985+
if options.output_transpiled_ts:
1986+
o['variables']['output_transpiled_ts'] = os.path.abspath(options.output_transpiled_ts)
1987+
else:
1988+
o['variables']['output_transpiled_ts'] = ''
1989+
19791990
def configure_napi(output):
19801991
version = getnapibuildversion.get_napi_version()
19811992
output['variables']['napi_build_version'] = version

node.gyp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'v8_enable_31bit_smis_on_64bit_arch%': 0,
77
'force_dynamic_crt%': 0,
88
'node_builtin_modules_path%': '',
9+
'output_transpiled_ts%': '',
910
'node_core_target_name%': 'node',
1011
'node_enable_v8_vtunejit%': 'false',
1112
'node_intermediate_lib_type%': 'static_library',
@@ -457,12 +458,16 @@
457458
],
458459
'node_mksnapshot_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_mksnapshot<(EXECUTABLE_SUFFIX)',
459460
'node_js2c_exec': '<(PRODUCT_DIR)/<(EXECUTABLE_PREFIX)node_js2c<(EXECUTABLE_SUFFIX)',
461+
'node_js2c_flags%': [],
460462
'conditions': [
461463
['GENERATOR == "ninja"', {
462464
'node_text_start_object_path': 'src/large_pages/node_text_start.node_text_start.o'
463465
}, {
464466
'node_text_start_object_path': 'node_text_start/src/large_pages/node_text_start.o'
465467
}],
468+
[ 'output_transpiled_ts!=""', {
469+
'node_js2c_flags': [ '--output-transpiled-ts', '<(output_transpiled_ts)' ],
470+
}],
466471
[ 'node_shared=="true"', {
467472
'node_target_type%': 'shared_library',
468473
'conditions': [
@@ -1108,6 +1113,7 @@
11081113
],
11091114
'action': [
11101115
'<(node_js2c_exec)',
1116+
'<@(node_js2c_flags)',
11111117
'<@(_outputs)',
11121118
'lib',
11131119
'config.gypi',

tools/js2c.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace js2c {
3030
int Main(int argc, char* argv[]);
3131

3232
static bool is_verbose = false;
33+
static std::string output_transpiled_dir;
3334

3435
void Debug(const char* format, ...) {
3536
va_list arguments;
@@ -327,6 +328,25 @@ int WriteIfChanged(const Fragment& out, const std::string& dest) {
327328
return WriteFileSync(out, dest.c_str());
328329
}
329330

331+
int MkdirpSync(const std::string& path) {
332+
uv_fs_t req;
333+
int r = uv_fs_mkdir(nullptr, &req, path.c_str(), 0755, nullptr);
334+
uv_fs_req_cleanup(&req);
335+
if (r == 0 || r == UV_EEXIST) return 0;
336+
if (r == UV_ENOENT) {
337+
// Parent doesn't exist, create it first.
338+
size_t pos = path.find_last_of('/');
339+
if (pos == std::string::npos || pos == 0) return r;
340+
int pr = MkdirpSync(path.substr(0, pos));
341+
if (pr != 0) return pr;
342+
r = uv_fs_mkdir(nullptr, &req, path.c_str(), 0755, nullptr);
343+
uv_fs_req_cleanup(&req);
344+
if (r == UV_EEXIST) return 0;
345+
return r;
346+
}
347+
return r;
348+
}
349+
330350
std::string GetFileId(const std::string& filename) {
331351
size_t end = filename.size();
332352
size_t start = 0;
@@ -705,6 +725,26 @@ int AddModule(const std::string& filename,
705725
}
706726
code.assign(result.code, result.code + result.code_len);
707727
swc_transform_free_result(&result);
728+
729+
if (!output_transpiled_dir.empty()) {
730+
// Write transpiled output as .js for debuggability.
731+
std::string out_path = output_transpiled_dir + "/" +
732+
filename.substr(0, filename.size() - kTsSuffix.size()) + ".js";
733+
size_t last_slash = out_path.find_last_of('/');
734+
if (last_slash != std::string::npos) {
735+
int mr = MkdirpSync(out_path.substr(0, last_slash));
736+
if (mr != 0) {
737+
PrintUvError("mkdir", out_path.c_str(), mr);
738+
return mr;
739+
}
740+
}
741+
int wr = WriteFileSync(code, out_path.c_str());
742+
if (wr != 0) {
743+
PrintUvError("write", out_path.c_str(), wr);
744+
return wr;
745+
}
746+
Debug("Wrote transpiled output to %s\n", out_path.c_str());
747+
}
708748
}
709749

710750
std::string file_id = GetFileId(filename);
@@ -891,6 +931,7 @@ int JS2C(const FileList& js_files,
891931
int PrintUsage(const char* argv0) {
892932
fprintf(stderr,
893933
"Usage: %s [--verbose] [--root /path/to/project/root] "
934+
"[--output-transpiled-ts path/to/dir] "
894935
"path/to/output.cc path/to/directory "
895936
"[extra-files ...]\n",
896937
argv0);
@@ -915,6 +956,12 @@ int Main(int argc, char* argv[]) {
915956
return 1;
916957
}
917958
root_dir = argv[++i];
959+
} else if (arg == "--output-transpiled-ts") {
960+
if (i == argc - 1) {
961+
fprintf(stderr, "--output-transpiled-ts must be followed by a path\n");
962+
return 1;
963+
}
964+
output_transpiled_dir = argv[++i];
918965
} else {
919966
args.emplace_back(argv[i]);
920967
}

0 commit comments

Comments
 (0)