Skip to content

Commit

Permalink
feat: add new option transformOutputPath
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyceBabu committed Feb 25, 2020
1 parent 36b9585 commit fabe65f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,55 @@ const defaultOptions = {
const outputFileName = (filePath) => filePath
.replace(/\.[^/.]+$/, '');

/**
* Callback for transforming output file path
*
* @callback TransformOutputPathFn
* @param {string} output target file name
* @param {string} input source file name
*/

/**
* multiInput is a rollup plugin to use multiple entry point and preserve the directory
* 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 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) => ({
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(
{},
fromPairs(fastGlob
.sync(globs, globOptions)
.map((name) => {
const filePath = path.relative(relative, name);
let filePath = path.relative(relative, name);
const isRelative = !filePath.startsWith('../');
const relativeToRoot = () => path.relative('./', name);
return [outputFileName(isRelative

filePath = isRelative
? filePath
: relativeToRoot()), name];
: relativeToRoot();

if (transformOutputPath) {
filePath = transformOutputPath(filePath, name);
}

return [outputFileName(filePath), name];
})),
// add no globs input to the result
...others,
Expand Down
14 changes: 14 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { rollup } from 'rollup';
import importJson from '@rollup/plugin-json';
import multiInput from '../src/plugin';
import path from 'path';

const expectedOutput = [
'fixtures/input1.js',
Expand Down Expand Up @@ -78,4 +79,17 @@ describe('rollup-plugin-multi-input', () => {
'test/fixtures/input2.js',
]);
});
it('should resolve output to "dist" directory', async () => {
const outputFiles = await generateOutputFileNames({
input: ['test/fixtures/**/*.js'],
plugins: [multiInput({
transformOutputPath: (output, input) => `dest/${path.basename(output)}`
})],
external: ['fast-glob', 'path'],
});
expect(outputFiles).toEqual([
'dest/input1.js',
'dest/input2.js',
]);
});
});

0 comments on commit fabe65f

Please sign in to comment.