From a4b3999f573e51b92953817945cf4896adbbc6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Orkisz?= Date: Fri, 7 Jun 2024 13:50:22 +0200 Subject: [PATCH 1/2] add --tsconfig-path argument. Works only with absolute paths though! --- readme.md | 1 + src/cli.js | 7 +++++-- src/index.js | 8 +++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 5068ac36..fe398509 100644 --- a/readme.md +++ b/readme.md @@ -71,6 +71,7 @@ Outputs the Node.js compact build of `input.js` into `dist/index.js`. --license [file] Adds a file containing licensing information to the output --stats-out [file] Emit webpack stats as json to the specified output file --target [es] ECMAScript target to use for output (default: es2015) + --tsconfig-path [file] Specify tsconfig.json to use for build (default: resolve tsconfig.json from entrypoint) Learn more: https://webpack.js.org/configuration/target -d, --debug Show debug logs ``` diff --git a/src/cli.js b/src/cli.js index e1c747e6..72d77be1 100755 --- a/src/cli.js +++ b/src/cli.js @@ -37,6 +37,7 @@ Options: --license [file] Adds a file containing licensing information to the output --stats-out [file] Emit webpack stats as json to the specified output file --target [es] ECMAScript target to use for output (default: es2015) + --tsconfig-path [file] Specify tsconfig.json to use for build (default: resolve tsconfig.json from entrypoint) Learn more: https://webpack.js.org/configuration/target -d, --debug Show debug logs `; @@ -166,7 +167,8 @@ async function runCmd (argv, stdout, stderr) { "-t": "--transpile-only", "--license": String, "--stats-out": String, - "--target": String + "--target": String, + "--tsconfig-path": String }, { permissive: false, argv @@ -269,7 +271,8 @@ async function runCmd (argv, stdout, stderr) { transpileOnly: args["--transpile-only"], license: args["--license"], quiet, - target: args["--target"] + target: args["--target"], + tsconfigPath: args["--tsconfig-path"] } ); diff --git a/src/index.js b/src/index.js index 8ffa3022..d35636c8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const resolve = require("resolve"); const fs = require("graceful-fs"); const crypto = require("crypto"); -const { join, dirname, extname, resolve: pathResolve } = require("path"); +const { join, basename, dirname, extname, resolve: pathResolve } = require("path"); const webpack = require("webpack"); const MemoryFS = require("memory-fs"); const terser = require("terser"); @@ -54,6 +54,7 @@ function ncc ( transpileOnly = false, license = '', target, + tsconfigPath = undefined, production = true, // webpack defaults to `module` and `main`, but that's // not really what node.js supports, so we reset it @@ -116,8 +117,8 @@ function ncc ( try { const configFileAbsolutePath = walkParentDirs({ base: process.cwd(), - start: dirname(entry), - filename: 'tsconfig.json', + start: tsconfigPath !== undefined ? dirname(tsconfigPath) : dirname(entry), + filename: tsconfigPath !== undefined ? basename(tsconfigPath) : 'tsconfig.json', }); fullTsconfig = loadTsconfig(configFileAbsolutePath) || { compilerOptions: {} @@ -358,6 +359,7 @@ function ncc ( loader: eval('__dirname + "/loaders/ts-loader.js"'), options: { transpileOnly, + configFile: tsconfigPath, compiler: eval('__dirname + "/typescript.js"'), compilerOptions: { module: 'esnext', From 2c1ca9b4f65dfa58da186ec49e4aa847e5692418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Orkisz?= Date: Fri, 7 Jun 2024 14:20:06 +0200 Subject: [PATCH 2/2] add support for relative paths to --tsconfig-path argument --- src/index.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index d35636c8..4737e68d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ const resolve = require("resolve"); const fs = require("graceful-fs"); const crypto = require("crypto"); -const { join, basename, dirname, extname, resolve: pathResolve } = require("path"); +const { basename, dirname, extname, isAbsolute, join, resolve: pathResolve } = require("path"); const webpack = require("webpack"); const MemoryFS = require("memory-fs"); const terser = require("terser"); @@ -110,16 +110,25 @@ function ncc ( existingAssetNames.push(`${filename}.cache${ext}`); } const resolvePlugins = []; + + let configFileAbsolutePath; + if (tsconfigPath !== undefined) { + configFileAbsolutePath = isAbsolute(tsconfigPath) + ? tsconfigPath + : join(process.cwd(), tsconfigPath); + } else { + configFileAbsolutePath = walkParentDirs({ + base: process.cwd(), + start: dirname(entry), + filename: 'tsconfig.json', + }); + } + // add TsconfigPathsPlugin to support `paths` resolution in tsconfig // we need to catch here because the plugin will // error if there's no tsconfig in the working directory let fullTsconfig = {}; try { - const configFileAbsolutePath = walkParentDirs({ - base: process.cwd(), - start: tsconfigPath !== undefined ? dirname(tsconfigPath) : dirname(entry), - filename: tsconfigPath !== undefined ? basename(tsconfigPath) : 'tsconfig.json', - }); fullTsconfig = loadTsconfig(configFileAbsolutePath) || { compilerOptions: {} }; @@ -359,7 +368,7 @@ function ncc ( loader: eval('__dirname + "/loaders/ts-loader.js"'), options: { transpileOnly, - configFile: tsconfigPath, + configFile: configFileAbsolutePath, compiler: eval('__dirname + "/typescript.js"'), compilerOptions: { module: 'esnext',