Skip to content

Commit

Permalink
feat: better typing for optional options
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredo.salzillo committed May 19, 2021
1 parent d2756f3 commit 95f9e64
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,49 @@ const outputFileName = (filePath) => filePath
* structure in the dist folder
*
* @param {?Object} options
* @param {?FastGlob.Options} options.glob - the fast-glob configuration object
* @param {?string} options.relative - the base path to remove in the dist folder
* @param {?TransformOutputPathFn} options.transformOutputPath - callback function to
* @param {?FastGlob?.Options} options.glob - the fast-glob configuration object
* @param {?string?} options.relative - the base path to remove in the dist folder
* @param {?TransformOutputPathFn?} options.transformOutputPath - callback function to
* transform the destination file name before generation
* @return {Plugin} - the rollup plugin config for enable support of multi-entry glob inputs
* */
export default ({
glob: globOptions,
relative = defaultOptions.relative,
transformOutputPath,
} = defaultOptions) => ({
name: pluginName,
options(conf) {
// flat to enable input to be a string or an array
// separate globs inputs string from others to enable input to be a mixed array too
const [globs, others] = partition([conf.input].flat(), isString);
// get files from the globs strings and return as a Rollup entries Object
const input = Object
.assign(
{},
Object.fromEntries(fastGlob
.sync(globs, globOptions)
.map((name) => {
const filePath = path.relative(relative, name);
const isRelative = !filePath.startsWith('../');
const relativeFilePath = (isRelative
? filePath
: path.relative('./', name));
if (transformOutputPath) {
return [outputFileName(transformOutputPath(relativeFilePath, name)), name];
}
return [outputFileName(relativeFilePath), name];
})),
// add no globs input to the result
...others,
);
export default (options = defaultOptions) => {
const {
glob: globOptions,
relative = defaultOptions.relative,
transformOutputPath,
} = options;
return ({
name: pluginName,
options(conf) {
// flat to enable input to be a string or an array
// separate globs inputs string from others to enable input to be a mixed array too
const [globs, others] = partition([conf.input].flat(), isString);
// get files from the globs strings and return as a Rollup entries Object
const input = Object
.assign(
{},
Object.fromEntries(fastGlob
.sync(globs, globOptions)
.map((name) => {
const filePath = path.relative(relative, name);
const isRelative = !filePath.startsWith('../');
const relativeFilePath = (isRelative
? filePath
: path.relative('./', name));
if (transformOutputPath) {
return [outputFileName(transformOutputPath(relativeFilePath, name)), name];
}
return [outputFileName(relativeFilePath), name];
})),
// add no globs input to the result
...others,
);
// return the new configuration with the glob input and the non string inputs
return {
...conf,
input,
};
},
});
return {
...conf,
input,
};
},
});
};

0 comments on commit 95f9e64

Please sign in to comment.