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..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, 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"); @@ -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 @@ -109,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: dirname(entry), - filename: 'tsconfig.json', - }); fullTsconfig = loadTsconfig(configFileAbsolutePath) || { compilerOptions: {} }; @@ -358,6 +368,7 @@ function ncc ( loader: eval('__dirname + "/loaders/ts-loader.js"'), options: { transpileOnly, + configFile: configFileAbsolutePath, compiler: eval('__dirname + "/typescript.js"'), compilerOptions: { module: 'esnext',