Skip to content

Commit

Permalink
fix(rollup): use loadConfigFile from rollup/dist/shared to load confi…
Browse files Browse the repository at this point in the history
…gs (#41)
  • Loading branch information
olehan committed Apr 8, 2021
1 parent b7ce4c2 commit babfa25
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/rollup/src/collect.ts
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());
};
25 changes: 25 additions & 0 deletions packages/rollup/src/typings/loadConfigFile.d.ts
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;
}

0 comments on commit babfa25

Please # to comment.