Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --tsconfig-path option for CLI #1197

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
7 changes: 5 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
`;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]
}
);

Expand Down
23 changes: 17 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {}
};
Expand Down Expand Up @@ -358,6 +368,7 @@ function ncc (
loader: eval('__dirname + "/loaders/ts-loader.js"'),
options: {
transpileOnly,