What
Every Polylang project repeats the same block at the top of configureWebpack():
const mode = options.mode;
const isProduction = mode === 'production' || false;
const workingDirectory = path.resolve( __dirname );
const jsBuildDirectory = path.join( workingDirectory, 'js/build' );
const cssBuildDirectory = path.join( workingDirectory, 'css/build' );
console.log( 'Webpack mode:', mode );
console.log( 'Working directory:', workingDirectory );
Same code in polylang, polylang-pro, polylang-wc, updater and polylang-for-elementor.
Why
- Duplicated in every repo; any change needs 4+ PRs
- Consumer configs should only define project-specific patterns/entry points
- A shared helper works for both CJS (
__dirname) and ESM (import.meta.url)
How
Add a getBuildContext() export:
const { getBuildContext, getVanillaConfig } = require( '@wpsyntex/polylang-build-scripts' );
function configureWebpack( options ) {
const {
isProduction,
workingDirectory,
jsBuildDirectory,
cssBuildDirectory,
} = getBuildContext( options, __dirname, {}, true );
return getVanillaConfig( {
workingDirectory,
jsBuildDirectory,
cssBuildDirectory,
isProduction,
// project-specific options…
} );
}
Helper sketch:
function getBuildContext(
options,
workingDirectory,
{ jsBuildSubdir = 'js/build', cssBuildSubdir = 'css/build' } = {},
log = false
) {
const mode = options?.mode ?? 'development';
const isProduction = mode === 'production';
const resolvedWorkingDirectory = path.resolve( workingDirectory );
if ( log ) {
console.log( 'Webpack mode:', mode );
console.log( 'Working directory:', resolvedWorkingDirectory );
}
return {
mode,
isProduction,
workingDirectory: resolvedWorkingDirectory,
jsBuildDirectory: path.join( resolvedWorkingDirectory, jsBuildSubdir ),
cssBuildDirectory: path.join( resolvedWorkingDirectory, cssBuildSubdir ),
};
}
Done when: helper exported + documented, unit tests added, all consumer webpack.config.js files
What
Every Polylang project repeats the same block at the top of
configureWebpack():Same code in
polylang,polylang-pro,polylang-wc,updaterandpolylang-for-elementor.Why
__dirname) and ESM (import.meta.url)How
Add a
getBuildContext()export:Helper sketch:
Done when: helper exported + documented, unit tests added, all consumer
webpack.config.jsfiles