|
| 1 | +import type FastGlob from 'fast-glob' |
| 2 | +import fastGlob from 'fast-glob' |
| 3 | +import path from 'path' |
| 4 | +import type { Plugin } from 'rollup' |
| 5 | + |
| 6 | +// This was taken from https://github.com/alfredosalzillo/rollup-plugin-multi-input |
| 7 | +// We maintain our copy here because rollup-plugin-multi-input has issues with exporting types |
| 8 | + |
| 9 | +const pluginName = 'rollup-plugin-multi-input' |
| 10 | + |
| 11 | +const isString = (value: unknown): value is string => typeof value === 'string' |
| 12 | + |
| 13 | +/** |
| 14 | + * default multi-input Options |
| 15 | + * */ |
| 16 | +const defaultOptions = { |
| 17 | + // `path.sep` is used for windows support |
| 18 | + relative: `src${path.sep}`, |
| 19 | +} |
| 20 | + |
| 21 | +// extract the output file name from a file name |
| 22 | +const outputFileName = (filePath: string) => filePath.replace(/\.[^/.]+$/, '').replace(/\\/g, '/') |
| 23 | + |
| 24 | +export type MultiInputOptions = { |
| 25 | + glob?: FastGlob.Options |
| 26 | + relative?: string |
| 27 | + transformOutputPath?: (path: string, fileName: string) => string |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * multiInput is a rollup plugin to use multiple entry point and preserve the directory |
| 32 | + * structure in the dist folder |
| 33 | + * */ |
| 34 | +export const multiInput = (options: MultiInputOptions = defaultOptions): Plugin => { |
| 35 | + const { glob: globOptions, relative = defaultOptions.relative, transformOutputPath } = options |
| 36 | + return { |
| 37 | + name: pluginName, |
| 38 | + options(conf) { |
| 39 | + // flat to enable input to be a string or an array |
| 40 | + const inputs = [conf.input].flat() |
| 41 | + // separate globs inputs string from others to enable input to be a mixed array too |
| 42 | + const globs = inputs.filter(isString) |
| 43 | + const others = inputs.filter((value) => !isString(value)) |
| 44 | + const normalizedGlobs = globs.map((glob) => glob.replace(/\\/g, '/')) |
| 45 | + // get files from the globs strings and return as a Rollup entries Object |
| 46 | + const entries = fastGlob.sync(normalizedGlobs, globOptions).map((name) => { |
| 47 | + const filePath = path.relative(relative, name) |
| 48 | + const isRelative = !filePath.startsWith(`..${path.sep}`) |
| 49 | + const relativeFilePath = isRelative ? filePath : path.relative(`.${path.sep}`, name) |
| 50 | + if (transformOutputPath) { |
| 51 | + return [outputFileName(transformOutputPath(relativeFilePath, name)), name] |
| 52 | + } |
| 53 | + return [outputFileName(relativeFilePath), name] |
| 54 | + }) |
| 55 | + const input = Object.assign( |
| 56 | + {}, |
| 57 | + Object.fromEntries(entries), |
| 58 | + // add no globs input to the result |
| 59 | + ...others, |
| 60 | + ) |
| 61 | + // return the new configuration with the glob input and the non string inputs |
| 62 | + return { |
| 63 | + ...conf, |
| 64 | + input, |
| 65 | + } |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
0 commit comments