UNPKG

@jenssimon/webpack-config-sfcc

Version:

A shareable Webpack configuration for SFCC projects

81 lines 2.64 kB
/* eslint-disable unicorn/prevent-abbreviations */ import fs from 'node:fs'; import { parse } from 'yaml'; /** * Process options, add defaults and validate. */ const getOptions = (options) => { if (!options.dirname) throw new Error('"dirname" option is mandatory. Please use "__dirname" as value.'); if (!options.resolver) throw new Error('"resolver" option is mandatory. Please use "require.resolve" as value.'); const defaults = { sourceMap: false, devServer: false, production: false, entryPoint: 'index.js', entryName: 'app', cssEntryName: 'core', preCSSExtractLoaders: [], additionalPlugins: [], additionalPostCSSPlugins: [], additionalDefine: {}, noLint: false, projectSpecificRules: [], alias: {}, transformNodeModules: [], swcTarget: 'es2019', allowCircularDependendies: false, }; const devServer = options?.env?.WEBPACK_SERVE === true; let hostname; let site; let locale; try { const dwJson = fs.readFileSync('dw.json', 'utf8'); ({ hostname } = JSON.parse(dwJson)); const devserverYml = fs.readFileSync('devserver.yml', 'utf8'); ({ site, locale } = parse(devserverYml)); } catch { hostname = undefined; site = undefined; locale = undefined; } return { ...defaults, ...options, devServer, site, locale, hostname, }; }; /** * Generates a Webpack configuration by using an array of configuration functions which create webpack config sections. * * @param sections configuration functions which create a webpack config section * @param cartridge cartridge name * @param opts config options * @returns a Webpack config containing the generated config sections */ export const generateWebpackConfiguration = (sections, cartridge, options) => { const intOptions = getOptions(options); return Object .entries(sections) // eslint-disable-next-line unicorn/no-array-reduce .reduce((previous, [key, value]) => { previous[key] = value(cartridge, intOptions); return previous; }, {}); }; /** * Change the name of the chunks. There are several problems with the default naming (like too long names). */ export const normalizeWebpack5ChunkName = (name) => name .replaceAll('node_modules', 'nodemodules') .replaceAll(/[._|-]+/g, ' ') .replaceAll(/\b(vendors|nodemodules|js|modules|es)\b/g, '') .trim() .replaceAll(/ +/g, '-'); //# sourceMappingURL=utils.js.map