Skip to content

Commit

Permalink
fix(tsup-node): don't mark compilerOptions.paths as external, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 25, 2021
1 parent 6115254 commit 8eac146
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
22 changes: 18 additions & 4 deletions src/esbuild/external.ts
@@ -1,24 +1,38 @@
import { Plugin } from 'esbuild'
import { tsconfigPathsToRegExp, match } from 'bundle-require'

// Must not start with "/" or "./" or "../"
const NON_NODE_MODULE_RE = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/

export const externalPlugin = ({
patterns,
skipNodeModulesBundle,
tsconfigResolvePaths,
}: {
patterns?: (string | RegExp)[]
skipNodeModulesBundle?: boolean
tsconfigResolvePaths?: Record<string, any>
}): 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
Expand Down
1 change: 1 addition & 0 deletions src/esbuild/index.ts
Expand Up @@ -138,6 +138,7 @@ export async function runEsbuild(
externalPlugin({
patterns: external,
skipNodeModulesBundle: options.skipNodeModulesBundle,
tsconfigResolvePaths: options.tsconfigResolvePaths,
}),
]
: []),
Expand Down
46 changes: 23 additions & 23 deletions 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'
Expand All @@ -23,6 +24,7 @@ export type NormalizedOptions = Omit<
'dts'
> & {
dts?: DtsConfig
tsconfigResolvePaths: Record<string, any>
}

export const defineConfig = (
Expand Down Expand Up @@ -51,10 +53,24 @@ const normalizeOptions = async (
optionsFromConfigFile: Options | undefined,
optionsOverride: Options
) => {
const options: Options = {
const _options = {
...optionsFromConfigFile,
...optionsOverride,
}
const options: Buildable<NormalizedOptions> = {
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)

Expand Down Expand Up @@ -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
}

Expand Down

1 comment on commit 8eac146

@vercel
Copy link

@vercel vercel bot commented on 8eac146 Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.