From 8eac1464814c27aaa1fd35cd8bf272c4f7f32273 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Thu, 25 Nov 2021 16:26:58 +0800 Subject: [PATCH] fix(tsup-node): don't mark `compilerOptions.paths` as external, closes #409 --- package-lock.json | 14 ++++++------- package.json | 2 +- src/esbuild/external.ts | 22 ++++++++++++++++---- src/esbuild/index.ts | 1 + src/index.ts | 46 ++++++++++++++++++++--------------------- 5 files changed, 50 insertions(+), 35 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8bdd680b..f83f9261 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.0-semantic-release", "license": "MIT", "dependencies": { - "bundle-require": "^2.1.3", + "bundle-require": "^2.1.7", "cac": "^6.7.2", "chokidar": "^3.5.1", "debug": "^4.3.1", @@ -1669,9 +1669,9 @@ "dev": true }, "node_modules/bundle-require": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/bundle-require/-/bundle-require-2.1.3.tgz", - "integrity": "sha512-aBLb/RHIZ9l3jAAGCTl/32m6bOQc3bmMbf6hfw+e6kcZnJL1E5i+MhmgBgBWK90CN9XeOBa/1bh6TiSLgk8New==", + "version": "2.1.7", + "resolved": "https://registry.npmjs.org/bundle-require/-/bundle-require-2.1.7.tgz", + "integrity": "sha512-IuQY+IgT/ZxaFhUKOzj69Bs6r3vviliCUINzgodqHYPgUW58AbBxS9ApiOxafa5N+L3Du7bu6Jt6gKEMN6Yuew==", "peerDependencies": { "esbuild": ">=0.13" } @@ -8711,9 +8711,9 @@ "dev": true }, "bundle-require": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/bundle-require/-/bundle-require-2.1.3.tgz", - "integrity": "sha512-aBLb/RHIZ9l3jAAGCTl/32m6bOQc3bmMbf6hfw+e6kcZnJL1E5i+MhmgBgBWK90CN9XeOBa/1bh6TiSLgk8New==", + "version": "2.1.7", + "resolved": "https://registry.npmjs.org/bundle-require/-/bundle-require-2.1.7.tgz", + "integrity": "sha512-IuQY+IgT/ZxaFhUKOzj69Bs6r3vviliCUINzgodqHYPgUW58AbBxS9ApiOxafa5N+L3Du7bu6Jt6gKEMN6Yuew==", "requires": {} }, "cac": { diff --git a/package.json b/package.json index caec42da..e4a66687 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "build:simple": "tsup src/cli-*.ts src/index.ts src/rollup.ts --clean --splitting" }, "dependencies": { - "bundle-require": "^2.1.3", + "bundle-require": "^2.1.7", "cac": "^6.7.2", "chokidar": "^3.5.1", "debug": "^4.3.1", diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index 5a58d873..955ea97a 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -1,4 +1,5 @@ import { Plugin } from 'esbuild' +import { tsconfigPathsToRegExp, match } from 'bundle-require' // Must not start with "/" or "./" or "../" const NON_NODE_MODULE_RE = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ @@ -6,19 +7,32 @@ const NON_NODE_MODULE_RE = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ export const externalPlugin = ({ patterns, skipNodeModulesBundle, + tsconfigResolvePaths, }: { patterns?: (string | RegExp)[] skipNodeModulesBundle?: boolean + tsconfigResolvePaths?: Record }): Plugin => { return { name: `external`, setup(build) { if (skipNodeModulesBundle) { - build.onResolve({ filter: NON_NODE_MODULE_RE }, (args) => ({ - path: args.path, - external: true, - })) + const resolvePatterns = tsconfigPathsToRegExp( + tsconfigResolvePaths || {} + ) + build.onResolve({ filter: /.*/ }, (args) => { + // Resolve `paths` from tsconfig + if (match(args.path, resolvePatterns)) { + return + } + if (NON_NODE_MODULE_RE.test(args.path)) { + return { + path: args.path, + external: true, + } + } + }) } if (!patterns || patterns.length === 0) return diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 962b3f3e..72f17a6a 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -138,6 +138,7 @@ export async function runEsbuild( externalPlugin({ patterns: external, skipNodeModulesBundle: options.skipNodeModulesBundle, + tsconfigResolvePaths: options.tsconfigResolvePaths, }), ] : []), diff --git a/src/index.ts b/src/index.ts index 25ee00a4..ff72c0df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,11 @@ import path from 'path' import fs from 'fs' import { Worker } from 'worker_threads' -import type { MarkRequired } from 'ts-essentials' +import type { Buildable, DeepPartial, MarkRequired } from 'ts-essentials' import { removeFiles, debouncePromise, slash } from './utils' import { loadTsupConfig, resolveTsConfig } from './load' import glob from 'globby' +import { loadTsConfig } from 'bundle-require' import { handleError, PrettyError } from './errors' import resolveFrom from 'resolve-from' import { parseArgsStringToArgv } from 'string-argv' @@ -23,6 +24,7 @@ export type NormalizedOptions = Omit< 'dts' > & { dts?: DtsConfig + tsconfigResolvePaths: Record } export const defineConfig = ( @@ -51,10 +53,24 @@ const normalizeOptions = async ( optionsFromConfigFile: Options | undefined, optionsOverride: Options ) => { - const options: Options = { + const _options = { ...optionsFromConfigFile, ...optionsOverride, } + const options: Buildable = { + target: 'node12', + format: ['cjs'], + outDir: 'dist', + ..._options, + dts: + typeof _options.dts === 'boolean' + ? _options.dts + ? {} + : undefined + : typeof _options.dts === 'string' + ? { entry: _options.dts } + : _options.dts, + } setSilent(options.silent) @@ -82,34 +98,18 @@ const normalizeOptions = async ( logger.info('CLI', `Building entry: ${JSON.stringify(input)}`) } - options.outDir = options.outDir || 'dist' - - // Build in cjs format by default - if (!options.format) { - options.format = ['cjs'] - } - - const tsconfig = await resolveTsConfig(process.cwd(), options.tsconfig) - if (tsconfig) { + const tsconfig = loadTsConfig(process.cwd(), options.tsconfig) + if (tsconfig.path) { logger.info( 'CLI', - `Using tsconfig: ${path.relative(process.cwd(), tsconfig)}` + `Using tsconfig: ${path.relative(process.cwd(), tsconfig.path)}` ) - options.tsconfig = tsconfig + options.tsconfig = tsconfig.path + options.tsconfigResolvePaths = tsconfig.data?.compilerOptions?.paths || {} } else if (options.tsconfig) { throw new PrettyError(`Cannot find tsconfig: ${options.tsconfig}`) } - if (!options.target) { - options.target = 'node12' - } - - if (options.dts === true) { - options.dts = {} - } else if (typeof options.dts === 'string') { - options.dts = { entry: options.dts } - } - return options as NormalizedOptions }