Skip to content

Commit

Permalink
jetpack-mu-wpcom: Improve Verbum build (#35323)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Grullon <[email protected]>
  • Loading branch information
anomiex and agrullon95 authored Jan 30, 2024
1 parent 1f61063 commit 7fad963
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 98 deletions.
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions projects/packages/jetpack-mu-wpcom/changelog/fix-verbum-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Verbum: Avoid copying PHP files into `src/build/verbum-comments/`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Verbum: Minify dynamic-loader script.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Verbum: Use jetpack-assets package to register scripts using `.asset.php` file data.
3 changes: 2 additions & 1 deletion projects/packages/jetpack-mu-wpcom/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "jetpack-library",
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0"
"php": ">=7.0",
"automattic/jetpack-assets": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down
1 change: 0 additions & 1 deletion projects/packages/jetpack-mu-wpcom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@types/react": "^18.2.28",
"@types/react-dom": "18.2.0",
"babel-plugin-transform-rename-properties": "0.1.0",
"copy-webpack-plugin": "11.0.0",
"sass": "1.64.1",
"sass-loader": "12.4.0",
"typescript": "^5.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function load_verbum_comments() {
if ( self::should_disable_comment_experience( $blog_id ) ) {
return false;
}
require_once __DIR__ . '/build/verbum-comments/class-verbum-comments.php';
require_once __DIR__ . '/features/verbum-comments/class-verbum-comments.php';
new \Automattic\Jetpack\Verbum_Comments();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,25 @@ public function enqueue_assets() {
return;
}

$version_js = filemtime( __DIR__ . '/verbum-comments.js' );
$version_css = filemtime( __DIR__ . '/verbum-comments.css' );
$connect_url = site_url( '/public.api/connect/?action=request' );
$primary_redirect = get_primary_redirect();

if ( strpos( $primary_redirect, '.wordpress.com' ) === false ) {
$connect_url = add_query_arg( 'domain', $primary_redirect, $connect_url );
}

// Enqueue styles
wp_enqueue_style( 'verbum', plugins_url( '/verbum-comments.css', __FILE__ ), array(), $version_css );

// Enqueue scripts
wp_register_script(
// Enqueue styles and scripts
Assets::register_script(
'verbum',
plugins_url( 'verbum-comments.js', __FILE__ ),
array(),
$version_js,
'../../build/verbum-comments/verbum-comments.js',
__FILE__,
array(
'strategy' => 'defer',
'in_footer' => true,
)
);

wp_enqueue_style( 'verbum' );
\WP_Enqueue_Dynamic_Script::enqueue_script( 'verbum' );

// Enqueue settings separately since the main script is dynamic.
Expand All @@ -139,7 +135,7 @@ public function enqueue_assets() {
'verbum-settings',
false,
array(),
$version_js,
null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- No script, so no version needed.
array(
'strategy' => 'defer',
'in_footer' => true,
Expand Down Expand Up @@ -250,14 +246,14 @@ public function enqueue_assets() {

wp_enqueue_script( 'verbum-settings' );

wp_enqueue_script(
Assets::register_script(
'verbum-dynamic-loader',
plugins_url( 'assets/dynamic-loader.js', __FILE__ ),
array(),
$version_js,
'../../build/verbum-comments/assets/dynamic-loader.js',
__FILE__,
array(
'strategy' => 'defer',
'in_footer' => true,
'enqueue' => true,
)
);
}
Expand Down
172 changes: 100 additions & 72 deletions projects/packages/jetpack-mu-wpcom/verbum.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const path = require( 'path' );
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jetpackConfig = require( '@automattic/jetpack-webpack-config/webpack' );
const CopyPlugin = require( 'copy-webpack-plugin' );
const webpack = require( 'webpack' );

const babelOpts = {
Expand All @@ -18,82 +17,111 @@ const babelOpts = {
presets: [ [ '@automattic/jetpack-webpack-config/babel/preset' ] ],
};

module.exports = {
entry: {
'verbum-comments': './src/features/verbum-comments/src/index.tsx',
},
mode: jetpackConfig.mode,
devtool: jetpackConfig.devtool,
output: {
...jetpackConfig.output,
filename: '[name]/[name].js',
path: path.resolve( __dirname, 'src/build' ),
environment: {
module: true,
dynamicImport: true,
module.exports = [
{
entry: {
'verbum-comments': './src/features/verbum-comments/src/index.tsx',
},
},
optimization: {
...jetpackConfig.optimization,
},
resolve: {
...jetpackConfig.resolve,
},
node: false,
plugins: [
...jetpackConfig.StandardPlugins( {
DependencyExtractionPlugin: { injectPolyfill: false },
MiniCssExtractPlugin: { filename: '[name]/[name].css' },
} ),
new webpack.ProvidePlugin( {
h: [ 'preact', 'h' ],
Fragment: [ 'preact', 'Fragment' ],
} ),
new CopyPlugin( {
patterns: [
{
from: './src/features/verbum-comments/class-verbum-comments.php',
to: './verbum-comments',
},
{
from: './src/features/verbum-comments/assets',
to: './verbum-comments/assets',
},
],
} ),
],
module: {
strictExportPresence: true,
rules: [
// Transpile JavaScript.
jetpackConfig.TranspileRule( {
exclude: /node_modules\//,
babelOpts,
mode: jetpackConfig.mode,
devtool: jetpackConfig.devtool,
output: {
...jetpackConfig.output,
filename: '[name]/[name].js',
path: path.resolve( __dirname, 'src/build' ),
environment: {
module: true,
dynamicImport: true,
},
},
optimization: {
...jetpackConfig.optimization,
},
resolve: {
...jetpackConfig.resolve,
},
node: false,
plugins: [
...jetpackConfig.StandardPlugins( {
DependencyExtractionPlugin: { injectPolyfill: false },
MiniCssExtractPlugin: { filename: '[name]/[name].css' },
} ),

// Transpile @automattic/jetpack-* in node_modules too.
jetpackConfig.TranspileRule( {
includeNodeModules: [ '@automattic/jetpack-' ],
new webpack.ProvidePlugin( {
h: [ 'preact', 'h' ],
Fragment: [ 'preact', 'Fragment' ],
} ),
],
module: {
strictExportPresence: true,
rules: [
// Transpile JavaScript.
jetpackConfig.TranspileRule( {
exclude: /node_modules\//,
babelOpts,
} ),

// preact has some `__` internal methods, which confuse i18n-check-webpack-plugin. Hack around that.
jetpackConfig.TranspileRule( {
includeNodeModules: [ 'preact' ],
babelOpts: {
configFile: false,
plugins: [ [ 'babel-plugin-transform-rename-properties', { rename: { __: '__ǃ' } } ] ],
presets: [],
},
} ),
// Transpile @automattic/jetpack-* in node_modules too.
jetpackConfig.TranspileRule( {
includeNodeModules: [ '@automattic/jetpack-' ],
} ),

// Handle CSS.
jetpackConfig.CssRule( {
extensions: [ 'css', 'scss' ],
extraLoaders: [ 'sass-loader' ],
} ),
// preact has some `__` internal methods, which confuse i18n-check-webpack-plugin. Hack around that.
jetpackConfig.TranspileRule( {
includeNodeModules: [ 'preact' ],
babelOpts: {
configFile: false,
plugins: [ [ 'babel-plugin-transform-rename-properties', { rename: { __: '__ǃ' } } ] ],
presets: [],
},
} ),

// Handle CSS.
jetpackConfig.CssRule( {
extensions: [ 'css', 'scss' ],
extraLoaders: [ 'sass-loader' ],
} ),

// Handle images.
jetpackConfig.FileRule(),
// Handle images.
jetpackConfig.FileRule(),
],
},
},
{
entry: {
'verbum-comments/assets/dynamic-loader':
'./src/features/verbum-comments/assets/dynamic-loader.js',
},
mode: jetpackConfig.mode,
devtool: jetpackConfig.devtool,
output: {
...jetpackConfig.output,
path: path.resolve( __dirname, 'src/build' ),
},
optimization: {
...jetpackConfig.optimization,
},
resolve: {
...jetpackConfig.resolve,
},
node: false,
plugins: [
...jetpackConfig.StandardPlugins( {
DependencyExtractionPlugin: { injectPolyfill: false },
} ),
],
module: {
strictExportPresence: true,
rules: [
// Transpile JavaScript.
jetpackConfig.TranspileRule( {
exclude: /node_modules\//,
babelOpts,
} ),

// Transpile @automattic/jetpack-* in node_modules too.
jetpackConfig.TranspileRule( {
includeNodeModules: [ '@automattic/jetpack-' ],
} ),
],
},
},
};
];
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const jetpackConfig = require( '@automattic/jetpack-webpack-config/webpack' );
const verbumConfig = require( './verbum.webpack.config.js' );

module.exports = [
verbumConfig,
...verbumConfig,
{
entry: {
'error-reporting': './src/features/error-reporting/index.js',
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/mu-wpcom-plugin/changelog/fix-verbum-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
]
},
"config": {
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_0_17"
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_0_18_alpha"
}
}
Loading

0 comments on commit 7fad963

Please sign in to comment.