UNPKG

@roots/bud-api

Version:

bud.js core module

63 lines (62 loc) 2.11 kB
import { isAbsolute, join, parse, relative } from 'node:path'; export const assets = async function assets(request, overrides = {}) { const app = this; const makePatternObjectFromString = fromStringFactory(app, overrides); const makePatternObjectFromTuple = fromTupleFactory(app, overrides); const arrayedRequest = !Array.isArray(request) ? [request] : request; const valueMapper = (item) => { if (typeof item === `string`) { return makePatternObjectFromString(item); } if (Array.isArray(item)) { return makePatternObjectFromTuple(...item); } return { ...item, ...overrides }; }; app.extensions .get(`@roots/bud-extensions/copy-webpack-plugin`) ?.set(`patterns`, (patterns = []) => [ ...patterns, ...arrayedRequest.map(valueMapper), ]); app.api.logger.log(`bud.copy: ${arrayedRequest.length} asset patterns added`); return app; }; /** * Take an input string and return a {@link CopyPlugin.ObjectPattern} */ export const fromStringFactory = (app, overrides) => (from) => { from = isAbsolute(from) ? relative(app.path(`@src`), from) : app.relPath(from); from = parse(from).ext?.length > 0 ? from : join(from, `**`, `*`); return { context: app.path(`@src`), from, globOptions: { dot: false }, noErrorOnMissing: true, to: app.relPath(`@file`), ...overrides, }; }; /** * Take an input [from,to] tuple and return a {@link CopyPlugin.ObjectPattern} */ export const fromTupleFactory = (app, overrides) => (from, to) => { from = isAbsolute(from) ? relative(app.path(`@src`), from) : app.relPath(from); to = isAbsolute(to) ? relative(app.path(`@dist`), to) : app.relPath(to); to = parse(from).ext?.length > 0 || to.includes(`[`) ? to : app.relPath(to, `@file`); return { context: app.path(`@src`), from, globOptions: { dot: false }, noErrorOnMissing: true, to, ...overrides, }; };