@roots/bud-api
Version:
bud.js core module
53 lines (52 loc) • 1.48 kB
JavaScript
import { join, sep } from 'node:path';
import isRegExp from '@roots/bud-support/isRegExp';
import isString from '@roots/bud-support/isString';
/**
* Create a module chunk
*
* @example
* Create an `alpine` chunk
*
* ```js
* bud.bundle('alpine')
* ```
*/
export const bundle = function (name, matcher) {
const test = normalize(matcher ?? name);
this.hooks.on(`build.optimization.splitChunks`, options => {
const entry = {
[name]: {
chunks: `all`,
enforce: true,
filename: join(`js`, `bundle`, `[name].js`),
idHint: name,
name,
priority: -10,
test,
},
};
if (options === false || options === undefined) {
return {
automaticNameDelimiter: sep,
cacheGroups: { default: false, ...entry },
};
}
return {
...options,
cacheGroups: {
...(options.cacheGroups ?? {}),
...entry,
},
};
});
this.api.logger.log(`bud.bundle: chunk settings registered`);
return this;
};
const normalize = (matcher) => {
return isRegExp(matcher)
? matcher
: isString(matcher)
? getTestRegExp([matcher])
: getTestRegExp(matcher);
};
const getTestRegExp = (matcher) => new RegExp(`[\\/](${matcher.reduce((a, c) => `${a}|${c}`)})[\\/]`);