-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rollup): use loadConfigFile from rollup/dist/shared to load confi…
…gs (#41)
- Loading branch information
Showing
2 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import { sync } from 'glob'; | ||
import { resolve } from 'path'; | ||
import type { RollupOptions } from 'rollup'; | ||
import { extname } from 'path'; | ||
import type { InternalModuleFormat, RollupOptions } from 'rollup'; | ||
import loadConfigFile from 'rollup/dist/loadConfigFile'; | ||
|
||
const formatByExtname: Record<string, InternalModuleFormat> = { | ||
js: 'es', | ||
mjs: 'es', | ||
cjs: 'cjs', | ||
}; | ||
|
||
export const collect = async ( | ||
filePatterns: readonly string[], | ||
dirname = process.cwd() | ||
): Promise<readonly RollupOptions[]> => | ||
Promise.all( | ||
filePatterns | ||
.flatMap((pattern) => sync(pattern) as readonly string[]) | ||
.map(async (file) => import(resolve(dirname, file)).then((config) => config.default)) | ||
).then((res) => res.flat()); | ||
cwd = process.cwd() | ||
): Promise<readonly RollupOptions[]> => { | ||
const configPromises = filePatterns | ||
.flatMap((pattern) => sync(pattern, { cwd }) as readonly string[]) | ||
.map(async (file) => { | ||
const format = formatByExtname[extname(file)]; | ||
const { warnings, options } = await loadConfigFile(file, { format }); | ||
|
||
warnings.flush(); | ||
|
||
return options; | ||
}); | ||
|
||
return Promise.all(configPromises).then((configs) => configs.flat()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
declare module 'rollup/dist/loadConfigFile' { | ||
import type { OutputOptions, RollupOptions } from 'rollup'; | ||
|
||
export type BatchedWarnings = { | ||
readonly count: number; | ||
readonly flush: () => void; | ||
}; | ||
|
||
export type LoadConfigFileResult = { | ||
readonly warnings: BatchedWarnings; | ||
readonly options: readonly RollupOptions[]; | ||
}; | ||
|
||
/** | ||
* @param fileName - path to config. | ||
* @param commandOptions - should be command line options. OutputOption is an alternative. | ||
* @private | ||
*/ | ||
function loadConfigFile( | ||
fileName: string, | ||
commandOptions?: OutputOptions | ||
): Promise<LoadConfigFileResult>; | ||
|
||
export = loadConfigFile; | ||
} |