Skip to content

Commit

Permalink
feat: custom input base
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 25, 2024
1 parent 2a41bcf commit e16ca8c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type Options = {
autoAddExts?: boolean
/** Patch `export default` in `.d.cts` to `export = ` */
patchCjsDefaultExport?: boolean
/** Base directory for input files. */
inputBase?: string
rewriteImports?: (
id: string,
importer: string,
Expand Down Expand Up @@ -42,7 +44,7 @@ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U

export type OptionsResolved = Overwrite<
Required<Options>,
Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports'>
Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports' | 'inputBase'>
>

export function resolveOptions(options: Options): OptionsResolved {
Expand All @@ -56,5 +58,6 @@ export function resolveOptions(options: Options): OptionsResolved {
autoAddExts: options.autoAddExts || false,
patchCjsDefaultExport: options.patchCjsDefaultExport || false,
rewriteImports: options.rewriteImports,
inputBase: options.inputBase,
}
}
3 changes: 2 additions & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function stripExt(filename: string): string {

export function resolveEntry(
input: string | string[] | Record<string, string>,
userInputBase?: string,
): {
entryMap: Record<string, string> | undefined
inputBase: string
Expand All @@ -50,7 +51,7 @@ export function resolveEntry(
)
: undefined
const arr = Array.isArray(input) && input ? input : Object.values(input)
const inputBase = lowestCommonAncestor(...arr)
const inputBase = userInputBase || lowestCommonAncestor(...arr)

return { entryMap, inputBase }
}
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
inputOptions: NormalizedInputOptions,
) {
const { input } = inputOptions
const { inputBase, entryMap } = resolveEntry(input)
const { inputBase, entryMap } = resolveEntry(input, options.inputBase)
debug('[rollup] input base:', inputBase)

let { entryFileNames = '[name].js' } = outputOptions
Expand Down Expand Up @@ -248,8 +248,9 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
const { input = {}, output = {} } = config as ResolvedCompilation
const { inputBase, entryMap } = resolveEntry(
input as Record<string, string>,
options.inputBase,
)
debug('[farm] out base:', inputBase)
debug('[farm] input base:', inputBase)

if (output && typeof output.entryFilename !== 'string') {
return console.error('entryFileName must be a string')
Expand Down Expand Up @@ -310,8 +311,8 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
)
throw new Error('unsupported entryPoints, must be an string[]')

const inputBase = lowestCommonAncestor(...entries)
debug('[esbuild] out base:', inputBase)
const inputBase = options.inputBase || lowestCommonAncestor(...entries)
debug('[esbuild] input base:', inputBase)

const jsExt = esbuildOptions.outExtension?.['.js']
let outExt: string
Expand Down

0 comments on commit e16ca8c

Please sign in to comment.