UNPKG

@roots/bud-build

Version:

bud.js core module

135 lines (134 loc) 3.34 kB
import { __decorate } from "tslib"; import { basename } from 'node:path'; import Registrable from '@roots/bud-build/helpers/registrable'; import Loader from '@roots/bud-build/loader'; import { bind } from '@roots/bud-support/decorators/bind'; import isString from '@roots/bud-support/isString'; import logger from '@roots/bud-support/logger'; /** * Item class */ class Item extends Registrable { _app; /** * Identifier */ ident; /** * Loader */ loader; /** * Loader options */ options; /** * Class constructor */ constructor(_app, constructorParams) { super(_app); this._app = _app; constructorParams?.ident && this.setIdent(constructorParams.ident); constructorParams?.loader && this.setLoader(constructorParams.loader); !constructorParams?.ident && constructorParams?.loader && this.setIdent(isString(constructorParams.loader) ? constructorParams.loader : basename(constructorParams.loader.getSrc())); constructorParams?.options && this.setOptions(constructorParams.options); } getIdent() { return this.ident; } /** * Get rule set item loader */ getLoader() { return this.loader instanceof Loader ? this.loader : this.app.build.loaders[this.loader]; } /** * Get rule set item options */ getOptions() { return this.unwrap(this.options); } /** * Merge rule set item options */ mergeOptions(options) { this.setOptions({ ...(this.getOptions() ?? {}), ...options, }); return this; } setIdent(ident) { this.ident = ident; return this; } /** * Set rule set item loader */ setLoader(loader) { this.loader = loader; if (!this.ident) this.setIdent(basename(isString(loader) ? loader : loader.getSrc())); return this; } /** * Set rule set item options */ setOptions(options) { this.options = options; return this; } /** * Produce rule set item object for Webpack */ toWebpack() { const output = { loader: this.getLoader()?.getSrc(), }; if (this.options) { output.options = this.getOptions(); } if (this.ident) { output.ident = this.getIdent(); } if (!output.loader) { logger.error(`error in ${this.ident}: no loader registered`); } return Object.entries(output).reduce((output, [key, value]) => ({ ...(output ?? {}), ...(value ? { [key]: value } : {}), }), {}); } } __decorate([ bind ], Item.prototype, "getIdent", null); __decorate([ bind ], Item.prototype, "getLoader", null); __decorate([ bind ], Item.prototype, "getOptions", null); __decorate([ bind ], Item.prototype, "mergeOptions", null); __decorate([ bind ], Item.prototype, "setIdent", null); __decorate([ bind ], Item.prototype, "setLoader", null); __decorate([ bind ], Item.prototype, "setOptions", null); __decorate([ bind ], Item.prototype, "toWebpack", null); export { Item as default };