Skip to content

Commit

Permalink
fix: localization build step
Browse files Browse the repository at this point in the history
  • Loading branch information
lgersman committed Nov 4, 2024
1 parent e7dca04 commit 46ddc63
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GIT_CLEAN_OPTS='-Xfd --interactive'

# directory to store wp-env data
# (only set when NOT running inside a dev container)
if [[ "$HOME" == "/home/vscode" ]]; then
if [[ "$HOME" != "/home/vscode" ]]; then
WP_ENV_HOME=./wp-env-home
fi

Expand Down
4 changes: 2 additions & 2 deletions packages/wp-plugin/essentials/languages/essentials.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-10-29T12:28:40+00:00\n"
"POT-Creation-Date: 2024-11-04T13:08:17+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Generator: WP-CLI 2.10.0\n"
"X-Domain: essentials\n"

#. Plugin Name of the plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-10-29T12:28:45+00:00\n"
"POT-Creation-Date: 2024-11-04T13:08:21+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Generator: WP-CLI 2.10.0\n"
"X-Domain: test-auto-load-i18n\n"

#. Plugin Name of the plugin
Expand Down
8 changes: 4 additions & 4 deletions packages/wp-plugin/test-plugin/languages/test-plugin-de_DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ msgstr ""
msgid "a test plugin"
msgstr ""

#: src/feature-1/blocks/block-1/edit.js:35
#: build/feature-1/blocks/block-1/index.js:1
msgid "Example Static – hello from the editor!"
msgstr ""

#: src/feature-1/blocks/block-2/edit.js:35
#: build/feature-1/blocks/block-2/index.js:1
msgid "Dynamic Block 2 – hello from the editor!"
msgstr ""

#: src/index.js:9
#: build/index.js:1
msgid "hello from packages/wp-plugin/test-plugin/src/index.js"
msgstr ""
msgstr "hello from germany"

#. Author of the plugin
#: test-plugin.php
Expand Down
8 changes: 4 additions & 4 deletions packages/wp-plugin/test-plugin/languages/test-plugin-en_US.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ msgstr "ionos-wordpress/test-plugin"
msgid "a test plugin"
msgstr "a test plugin"

#: src/feature-1/blocks/block-1/edit.js:35
#: build/feature-1/blocks/block-1/index.js:1
msgid "Example Static – hello from the editor!"
msgstr ""

#: src/feature-1/blocks/block-2/edit.js:35
#: build/feature-1/blocks/block-2/index.js:1
msgid "Dynamic Block 2 – hello from the editor!"
msgstr ""

#: src/index.js:9
#: build/index.js:1
msgid "hello from packages/wp-plugin/test-plugin/src/index.js"
msgstr ""
msgstr "hello from usa"

#. Author of the plugin
#: test-plugin.php
Expand Down
10 changes: 5 additions & 5 deletions packages/wp-plugin/test-plugin/languages/test-plugin.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-10-29T12:28:48+00:00\n"
"POT-Creation-Date: 2024-11-04T13:08:26+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Generator: WP-CLI 2.10.0\n"
"X-Domain: test-plugin\n"

#. Plugin Name of the plugin
Expand All @@ -34,14 +34,14 @@ msgstr ""
msgid "https://www.ionos-group.com/brands.html"
msgstr ""

#: src/feature-1/blocks/block-1/edit.js:35
#: build/feature-1/blocks/block-1/index.js:1
msgid "Example Static – hello from the editor!"
msgstr ""

#: src/feature-1/blocks/block-2/edit.js:35
#: build/feature-1/blocks/block-2/index.js:1
msgid "Dynamic Block 2 – hello from the editor!"
msgstr ""

#: src/index.js:9
#: build/index.js:1
msgid "hello from packages/wp-plugin/test-plugin/src/index.js"
msgstr ""
13 changes: 13 additions & 0 deletions packages/wp-plugin/test-plugin/test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@

error_log("foo=$foo");
});

\add_action('admin_enqueue_scripts', function() : void {
$assets = include_once __DIR__ . '/build/index.asset.php';
\wp_enqueue_script(
handle: 'test-plugin-index',
src: \plugins_url( '/build/index.js', __FILE__ ),
deps: $assets['dependencies'],
ver : $assets['version'],
args: true,
);

\wp_set_script_translations( 'test-plugin-index', 'test-plugin', \plugin_dir_path( __FILE__ ) . 'languages' );
});
76 changes: 38 additions & 38 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,43 @@ function ionos.wordpress.build_workspace_package_wp_plugin() {

pnpm --filter "$PACKAGE_NAME" --if-present run prebuild

# transpile js/css scripts
if [[ -d $path/src ]]; then
# generate webpack.config.js (see https://wordpress.stackexchange.com/a/425349)
cat << EOF | tee $path/webpack.config.js
// DO NOT MODIFY THIS FILE DIRECTLY - IT'S MACHINE GENERATED BY ./scripts/build.sh
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
module.exports = {
...defaultConfig,
entry: {
...defaultConfig.entry(),
$(
# add recursively all {index,*-index}.js files in src directory to webpack entry
# ignore files with block.json in the same directory
for js_file in $(find $path/src -type f \( -name 'index.js' -o -name '*-index.js' \) ! -execdir test -f block.json \; -print | xargs -I {} realpath --relative-to $path/src {}); do
echo " '${js_file%.*}': './src/$js_file',"
done
)
},
};
EOF

# bundle js/css either in development or production mode depending on NODE_ENV
pnpm \
--filter "$PACKAGE_NAME" \
exec wp-scripts \
$([[ "${NODE_ENV}" == 'development' ]] && echo 'start --no-watch' || echo 'build') \
--webpack-copy-php

# @TODO: if wp 6.7 is out - enable manifest generation
# (see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#build-blocks-manifest)
# # if the plugin provides blocks => build also the blocks manifest
# # find $path/src -type f -name 'block.json' | grep -q . && echo pnpm --filter "$PACKAGE_NAME" exec wp-scripts build-blocks-manifest
else
ionos.wordpress.log_warn "transpiling js/css skipped : no ./src directory found"
fi

# ensure directory 'languages' exists if WP_CLI_I18N_LOCALES is not empty
[[ "${WP_CLI_I18N_LOCALES:-}" != '' ]] && mkdir -p $path/languages

Expand All @@ -120,7 +157,7 @@ function ionos.wordpress.build_workspace_package_wp_plugin() {
# generate/update pot file
ionos.wordpress.build_workspace_package_wp_plugin.wp_cli i18n make-pot \
--domain=$text_domain \
--exclude=tests/,vendor/,package.json,node_modules/,build/ \
--exclude=tests/,vendor/,package.json,node_modules/,src/ \
./ ./languages/$text_domain.pot

# generate po files if WP_CLI_I18N_LOCALES is set
Expand Down Expand Up @@ -156,43 +193,6 @@ function ionos.wordpress.build_workspace_package_wp_plugin() {
ionos.wordpress.log_warn "processing i18n skipped : no ./languages directory found nor env variable WP_CLI_I18N_LOCALES set"
fi

# transpile js/css scripts
if [[ -d $path/src ]]; then
# generate webpack.config.js (see https://wordpress.stackexchange.com/a/425349)
cat << EOF | tee $path/webpack.config.js
// DO NOT MODIFY THIS FILE DIRECTLY - IT'S MACHINE GENERATED BY ./scripts/build.sh
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
module.exports = {
...defaultConfig,
entry: {
...defaultConfig.entry(),
$(
# add recursively all {index,*-index}.js files in src directory to webpack entry
# ignore files with block.json in the same directory
for js_file in $(find $path/src -type f \( -name 'index.js' -o -name '*-index.js' \) ! -execdir test -f block.json \; -print | xargs -I {} realpath --relative-to $path/src {}); do
echo " '${js_file%.*}': './src/$js_file',"
done
)
},
};
EOF

# bundle js/css either in development or production mode depending on NODE_ENV
pnpm \
--filter "$PACKAGE_NAME" \
exec wp-scripts \
$([[ "${NODE_ENV}" == 'development' ]] && echo 'start --no-watch' || echo 'build') \
--webpack-copy-php

# @TODO: if wp 6.7 is out - enable manifest generation
# (see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#build-blocks-manifest)
# # if the plugin provides blocks => build also the blocks manifest
# # find $path/src -type f -name 'block.json' | grep -q . && echo pnpm --filter "$PACKAGE_NAME" exec wp-scripts build-blocks-manifest
else
ionos.wordpress.log_warn "transpiling js/css skipped : no ./src directory found"
fi

# update plugin version in plugin.php
PACKAGE_VERSION=$(jq -r '.version' $PACKAGE_JSON)

Expand Down

0 comments on commit 46ddc63

Please sign in to comment.