Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: ESLint

on:
push:
branches: master
pull_request:
branches: master
workflow_dispatch:

jobs:
js-and-style:
name: ESLint

runs-on: ubuntu-latest

steps:
- name: Check out the source code
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 22

- name: Install dependencies
run: npm install

- name: Run ESLint
run: npm run lint
21 changes: 21 additions & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Spell Check

on:
push:
branches: master
pull_request:
branches: master
workflow_dispatch:

jobs:
testing:
name: Spell Check with Typos

runs-on: ubuntu-latest

steps:
- name: Checkout Actions Repository
uses: actions/checkout@v4

- name: Check spelling
uses: crate-ci/typos@master
29 changes: 29 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Unit Tests

on:
push:
branches: master
pull_request:
branches: master
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

name: Jest Unit Tests

steps:
- name: Check out the source code
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 22

- name: Install dependencies
run: npm install

- name: Run Jest Unit Tests
run: npm run test
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,104 @@
# Polylang Build Scripts

These are the scripts needed to build Polylang. They are meant to be use as **dependencies** of both [Polylang](https://github.com/polylang/polylang) and [Polylang Pro](https://polylang.pro/products). Outside of this context, we do not guarantee that they provide you any value.
Webpack configuration helpers for building Polylang projects. Provides two main functions for generating webpack configs: `getVanillaConfig` for vanilla JS/CSS files and `getReactifiedConfig` for React-based builds.

## Installation

```bash
npm install @wpsyntex/polylang-build-scripts
```

## getVanillaConfig

Generates webpack configurations for vanilla JS and CSS files. Creates both minified and unminified versions.

**Required options:**
- `workingDirectory` - Working directory for glob resolution (typically `__dirname`)
- `jsBuildDirectory` - Output directory for JS files
- `cssBuildDirectory` - Output directory for CSS files
- `isProduction` - Enable production mode optimizations

**Optional options:**
- `jsPatterns` - Glob patterns for JS files (default: `['**/*.js']`)
- `jsIgnorePatterns` - Patterns to ignore for JS files (default: `[]`)
- `cssPatterns` - Glob patterns for CSS files (default: `['**/*.css']`)
- `cssIgnorePatterns` - Patterns to ignore for CSS files (default: `[]`)

**Example:**

```javascript
const { getVanillaConfig } = require( '@wpsyntex/polylang-build-scripts' );

const vanillaConfigs = getVanillaConfig( {
workingDirectory: __dirname,
jsPatterns: [ '**/*.js' ],
jsIgnorePatterns: [ 'node_modules/**', '**/build/**', '**/*.min.js' ],
cssPatterns: [ '**/*.css' ],
cssIgnorePatterns: [ 'node_modules/**', '**/build/**', '**/*.min.css' ],
jsBuildDirectory: path.resolve( __dirname, 'js/build' ),
cssBuildDirectory: path.resolve( __dirname, 'css/build' ),
isProduction: mode === 'production',
} );

module.exports = vanillaConfigs;
```

## getReactifiedConfig

Generates webpack configurations for React-based builds (blocks, editors). Creates minified and unminified builds with Babel transpilation.

**Required options:**
- `entryPoints` - Entry points for webpack (e.g., `{ blocks: './js/src/blocks/index.js' }`)
- `outputPath` - Base path for output files (typically `__dirname`)
- `libraryName` - Library name for the bundle (e.g., `'polylang'`)
- `isProduction` - Enable production mode optimizations
- `wpDependencies` - WordPress package dependencies to mark as externals (e.g., `['blocks', 'element', 'i18n']`)

**Optional options:**
- `additionalExternals` - Additional external dependencies (e.g., `{ jquery: 'jQuery' }`)

**Example:**

```javascript
const { getReactifiedConfig } = require( '@wpsyntex/polylang-build-scripts' );

const wpDependencies = [
'api-fetch',
'block-editor',
'blocks',
'components',
'data',
'element',
'i18n',
];

const reactConfigs = getReactifiedConfig( {
entryPoints: { blocks: './js/src/blocks/index.js' },
outputPath: __dirname,
libraryName: 'polylang',
isProduction: mode === 'production',
wpDependencies,
} );

module.exports = reactConfigs;
```

## Combined Usage

```javascript
const { getVanillaConfig, getReactifiedConfig } = require( '@wpsyntex/polylang-build-scripts' );

module.exports = ( env, argv ) => {
const mode = argv.mode || 'development';
const isProduction = mode === 'production';

return [
...getVanillaConfig( { /* options */ } ),
...getReactifiedConfig( { /* options */ } ),
];
};
```

---

These scripts are meant to be used as dependencies of Polylang-related projects but can easily be adapted for any WordPress project requiring JS or React build tools.
136 changes: 136 additions & 0 deletions configs/reactified.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Peer dependencies.
*/
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );

/**
* Given a string, returns a new string with dash separators converted to
* camelCase equivalent. This is not as aggressive as `_.camelCase` in
* converting to uppercase, where Lodash will also capitalize letters
* following numbers.
*
* @param {string} string Input dash-delimited string.
* @return {string} Camel-cased string.
*/
const camelCaseDash = ( string ) => {
return string.replace( /-([a-z])/g, ( match, letter ) =>
letter.toUpperCase()
);
};

/**
* Generate webpack configuration for React-based builds (blocks, editors).
*
* @param {Object} options Configuration options.
* @param {Object} options.entryPoints Entry points object (e.g., { blocks: './js/src/blocks/index.js' }).
* @param {string} options.outputPath Base path for output files (__dirname).
* @param {string} options.libraryName Library name for the bundle (e.g., 'polylang', 'polylang-pro').
* @param {boolean} options.isProduction Whether to enable production mode optimizations.
* @param {string[]} options.wpDependencies WordPress package dependencies to mark as externals.
* @param {Object} [options.additionalExternals] Additional external dependencies.
* @return {Object[]} Array of webpack configurations (minified and unminified).
*/
const getReactifiedConfig = ( {
entryPoints,
outputPath,
libraryName,
isProduction,
wpDependencies,
additionalExternals = {},
} ) => {
const externals = {
react: 'React',
...additionalExternals,
};

wpDependencies.forEach( ( name ) => {
externals[ `@wordpress/${ name }` ] = {
this: [ 'wp', camelCaseDash( name ) ],
};
} );

const devtool = ! isProduction ? 'source-map' : false;

const resolve = {
modules: [ outputPath, 'node_modules' ],
};

const outputConfig = {
path: outputPath,
library: [ libraryName ],
libraryTarget: 'this',
};

const minimizeOptimizationConfig = {
minimize: true,
minimizer: [
new TerserPlugin( {
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
} ),
],
};

const unMinimizeOptimizationConfig = {
minimize: false,
};

const transpilationRules = [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
];

const minifiedConfig = {
entry: entryPoints,
output: Object.assign(
{},
{ filename: './js/build/[name].min.js' },
outputConfig
),
externals,
resolve,
module: {
rules: transpilationRules,
},
plugins: [
new MiniCssExtractPlugin( {
filename: './css/build/style.min.css',
} ),
],
devtool,
optimization: minimizeOptimizationConfig,
};

const unminifiedConfig = {
entry: entryPoints,
output: Object.assign(
{},
{ filename: './js/build/[name].js' },
outputConfig
),
externals,
resolve,
module: {
rules: transpilationRules,
},
plugins: [
new MiniCssExtractPlugin( {
filename: './css/build/style.css',
} ),
],
devtool,
optimization: unMinimizeOptimizationConfig,
};

return [ minifiedConfig, unminifiedConfig ];
};

module.exports = { getReactifiedConfig };
Loading