Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection: Load connection JS bundle via connection PHP package #38877

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
49d1e64
Mark connection js package being external to extract it as a dependency
manzoorwanijk Aug 14, 2024
be42ad9
Bundle connection js package inside php connection package
manzoorwanijk Aug 14, 2024
74c2580
Register connect bundle as a script handle for dependents to use
manzoorwanijk Aug 14, 2024
cae13e2
Add changelog
manzoorwanijk Aug 14, 2024
486619c
Fix E2E tests
manzoorwanijk Aug 14, 2024
f24e89b
Fix more e2e tests
manzoorwanijk Aug 14, 2024
c959457
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 16, 2024
7d1551e
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 19, 2024
c81c3f6
Fix jetpack-admin css dependency because it does not use Assets::regi…
manzoorwanijk Aug 19, 2024
e7821d7
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 20, 2024
610afc3
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 21, 2024
4e038ae
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 21, 2024
e7ad4bc
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 27, 2024
cf0b066
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Aug 28, 2024
16b16b1
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Sep 2, 2024
2b14392
Merge branch 'trunk' into update/load-connection-js-bundle-via-connec…
manzoorwanijk Sep 2, 2024
aa47f5e
Update webpack.config.js
manzoorwanijk Sep 2, 2024
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
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Updated connection js to load its bundle via connection package
4 changes: 4 additions & 0 deletions projects/js-packages/webpack-config/src/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ const defaultRequestMap = {
external: 'JetpackScriptDataModule',
handle: 'jetpack-script-data',
},
'@automattic/jetpack-connection': {
external: 'JetpackConnection',
handle: 'jetpack-connection',
},
};

const DependencyExtractionPlugin = ( { requestMap, ...options } = {} ) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Updated connection js to load its bundle via connection package
1 change: 1 addition & 0 deletions projects/packages/connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"watch": "pnpm run build && pnpm webpack watch"
},
"dependencies": {
"@automattic/jetpack-connection": "workspace:*",
"@automattic/jetpack-idc": "workspace:*",
"@wordpress/data": "10.5.0",
"@wordpress/element": "6.5.0"
Expand Down
41 changes: 41 additions & 0 deletions projects/packages/connection/src/class-connection-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Connection_Assets.
*
* @package automattic/jetpack-connection
*/

namespace Automattic\Jetpack\Connection;

use Automattic\Jetpack\Assets;

/**
* Connection_Assets class.
*/
class Connection_Assets {

/**
* Initialize the class.
*/
public static function configure() {
add_action( 'wp_loaded', array( __CLASS__, 'register_assets' ) );

add_filter( 'jetpack_admin_js_script_data', array( Initial_State::class, 'set_connection_script_data' ), 10, 1 );
}

/**
* Register assets.
*/
public static function register_assets() {

Assets::register_script(
'jetpack-connection',
'../dist/jetpack-connection.js',
__FILE__,
array(
'in_footer' => true,
'textdomain' => 'jetpack-connection',
)
);
}
}
2 changes: 1 addition & 1 deletion projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function configure() {
// Initial Partner management.
Partner::init();

add_filter( 'jetpack_admin_js_script_data', array( Initial_State::class, 'set_connection_script_data' ), 10, 1 );
Connection_Assets::configure();
}

/**
Expand Down
1 change: 1 addition & 0 deletions projects/packages/connection/src/js/jetpack-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@automattic/jetpack-connection';
105 changes: 70 additions & 35 deletions projects/packages/connection/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,57 @@ for ( const file of glob.sync( './src/sso/*.css' ) ) {
ssoEntries[ name ].push( path.resolve( file ) );
}

/**
* @type {import('webpack').Configuration[]} Webpack configuration.
*/
const sharedConfig = {
mode: jetpackWebpackConfig.mode,
devtool: jetpackWebpackConfig.devtool,
output: {
...jetpackWebpackConfig.output,
path: path.resolve( './dist' ),
},
optimization: {
...jetpackWebpackConfig.optimization,
},
resolve: {
...jetpackWebpackConfig.resolve,
},
node: false,
module: {
strictExportPresence: true,
rules: [
// Transpile JavaScript, including node_modules.
jetpackWebpackConfig.TranspileRule(),

// Transpile @automattic/jetpack-* in node_modules too.
jetpackWebpackConfig.TranspileRule( {
includeNodeModules: [ '@automattic/jetpack-' ],
} ),

// Handle CSS.
jetpackWebpackConfig.CssRule( {
extensions: [ 'css', 'sass', 'scss' ],
extraLoaders: [ 'sass-loader' ],
} ),
// Handle images.
jetpackWebpackConfig.FileRule(),
],
},
externals: {
...jetpackWebpackConfig.externals,
jetpackConfig: JSON.stringify( {
consumer_slug: 'connection_package',
} ),
},
};

/**
* @type {import('webpack').Configuration[]} Webpack configuration.
*/
module.exports = [
{
...sharedConfig,
entry: {
'tracks-ajax': './src/js/tracks-ajax.js',
'tracks-callables': {
Expand All @@ -30,47 +79,33 @@ module.exports = [
'identity-crisis': './src/identity-crisis/_inc/admin.jsx',
...ssoEntries,
},
mode: jetpackWebpackConfig.mode,
devtool: jetpackWebpackConfig.devtool,
output: {
...jetpackWebpackConfig.output,
path: path.resolve( './dist' ),
},
optimization: {
...jetpackWebpackConfig.optimization,
},
resolve: {
...jetpackWebpackConfig.resolve,
},
node: false,
plugins: [
...jetpackWebpackConfig.StandardPlugins( {
MiniCssExtractPlugin: { filename: '[name].css' },
} ),
],
module: {
strictExportPresence: true,
rules: [
// Transpile JavaScript, including node_modules.
jetpackWebpackConfig.TranspileRule(),

// Transpile @automattic/jetpack-* in node_modules too.
jetpackWebpackConfig.TranspileRule( {
includeNodeModules: [ '@automattic/jetpack-' ],
} ),

// Handle CSS.
jetpackWebpackConfig.CssRule( {
extensions: [ 'css', 'sass', 'scss' ],
extraLoaders: [ 'sass-loader' ],
} ),
],
},
{
...sharedConfig,
entry: {
'jetpack-connection': {
import: './src/js/jetpack-connection.js',
library: {
name: 'JetpackConnection',
type: 'umd',
},
},
},
externals: {
...jetpackWebpackConfig.externals,
jetpackConfig: JSON.stringify( {
consumer_slug: 'identity_crisis',
plugins: [
...jetpackWebpackConfig.StandardPlugins( {
MiniCssExtractPlugin: { filename: '[name].css' },
DependencyExtractionPlugin: {
requestMap: {
// We don't want to externalize this package, we rather want to bundle it.
'@automattic/jetpack-connection': {},
},
},
} ),
},
],
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function admin_scripts() {
public function admin_styles() {
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' );
wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons', 'jetpack-connection' ), JETPACK__VERSION . '-20121016' );
wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
wp_style_add_data( 'jetpack-admin', 'suffix', $min );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Updated connection js to load its bundle via connection package
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"build": "pnpm jetpack build packages/forms packages/connection packages/blaze plugins/jetpack -v --no-pnpm-install --production",
"build": "pnpm jetpack build packages/forms packages/assets packages/connection packages/blaze plugins/jetpack -v --no-pnpm-install --production",
"clean": "rm -rf output",
"config:decrypt": "pnpm test-decrypt-default-config && pnpm test-decrypt-config",
"distclean": "rm -rf node_modules",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Updated connection js to load its bundle via connection package
2 changes: 1 addition & 1 deletion projects/plugins/search/tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"build": "pnpm jetpack build packages/search plugins/jetpack -v --no-pnpm-install --production",
"build": "pnpm jetpack build packages/assets packages/search packages/connection plugins/jetpack -v --no-pnpm-install --production",
"clean": "rm -rf output",
"config:decrypt": "openssl enc -md sha1 -aes-256-cbc -d -pass env:CONFIG_KEY -in ./node_modules/jetpack-e2e-commons/config/encrypted.enc -out ./config/local.cjs",
"distclean": "rm -rf node_modules",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Updated connection js to load its bundle via connection package
2 changes: 1 addition & 1 deletion projects/plugins/social/tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"build": "pnpm jetpack build packages/assets plugins/social plugins/jetpack -v --no-pnpm-install --production",
"build": "pnpm jetpack build packages/assets packages/connection plugins/social plugins/jetpack -v --no-pnpm-install --production",
"clean": "rm -rf output",
"config:decrypt": "openssl enc -md sha1 -aes-256-cbc -d -pass env:CONFIG_KEY -in ./node_modules/jetpack-e2e-commons/config/encrypted.enc -out ./config/local.cjs",
"distclean": "rm -rf node_modules",
Expand Down
Loading