Skip to content

Commit cee0e15

Browse files
authored
Merge pull request #9985 from google/enhancement/9957-js-entry-point-for-block-editor
Add JS entry point for block editor.
2 parents c839573 + 089868f commit cee0e15

6 files changed

+149
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Reader Revenue Manager module's block editor entrypoint.
3+
*
4+
* Site Kit by Google, Copyright 2024 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/

includes/Modules/Reader_Revenue_Manager.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Google\Site_Kit\Modules;
1212

1313
use Exception;
14+
use Google\Site_Kit\Core\Assets\Asset;
1415
use Google\Site_Kit\Core\Assets\Script;
1516
use Google\Site_Kit\Core\Authentication\Clients\Google_Site_Kit_Client;
1617
use Google\Site_Kit\Core\Modules\Module;
@@ -32,6 +33,7 @@
3233
use Google\Site_Kit\Core\Site_Health\Debug_Data;
3334
use Google\Site_Kit\Core\Tags\Guards\Tag_Environment_Type_Guard;
3435
use Google\Site_Kit\Core\Tags\Guards\Tag_Verify_Guard;
36+
use Google\Site_Kit\Core\Util\Feature_Flags;
3537
use Google\Site_Kit\Core\Util\URL;
3638
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Settings;
3739
use Google\Site_Kit\Modules\Reader_Revenue_Manager\Synchronize_OnboardingState;
@@ -383,7 +385,7 @@ function ( $url ) {
383385
protected function setup_assets() {
384386
$base_url = $this->context->url( 'dist/assets/' );
385387

386-
return array(
388+
$assets = array(
387389
new Script(
388390
'googlesitekit-modules-reader-revenue-manager',
389391
array(
@@ -400,6 +402,19 @@ protected function setup_assets() {
400402
)
401403
),
402404
);
405+
406+
if ( Feature_Flags::enabled( 'rrmModuleV2' ) ) {
407+
$assets[] = new Script(
408+
'googlesitekit-reader-revenue-manager-block-editor',
409+
array(
410+
'src' => $base_url . 'js/googlesitekit-reader-revenue-manager-block-editor.js',
411+
'dependencies' => array(),
412+
'load_contexts' => array( Asset::CONTEXT_ADMIN_POST_EDITOR ),
413+
)
414+
);
415+
}
416+
417+
return $assets;
403418
}
404419

405420
/**

tests/phpunit/integration/Modules/Reader_Revenue_ManagerTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,36 @@ public function test_check_service_entity_access_no_access_unavailable_publicati
486486
$this->assertEquals( false, $access );
487487
}
488488

489+
public function test_block_editor_script_enqueued() {
490+
$this->enable_feature( 'rrmModuleV2' );
491+
492+
$registerable_asset_handles = array_map(
493+
function ( $asset ) {
494+
return $asset->get_handle();
495+
},
496+
$this->reader_revenue_manager->get_assets()
497+
);
498+
499+
$this->assertContains(
500+
'googlesitekit-reader-revenue-manager-block-editor',
501+
$registerable_asset_handles
502+
);
503+
}
504+
505+
public function test_block_editor_script_not_enqueued() {
506+
$registerable_asset_handles = array_map(
507+
function ( $asset ) {
508+
return $asset->get_handle();
509+
},
510+
$this->reader_revenue_manager->get_assets()
511+
);
512+
513+
$this->assertNotContains(
514+
'googlesitekit-reader-revenue-manager-block-editor',
515+
$registerable_asset_handles
516+
);
517+
}
518+
489519
/**
490520
* @return Module_With_Scopes
491521
*/

webpack.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
const { createRules } = require( './webpack/common' );
2323
const adminCssConfig = require( './webpack/adminCss.config' );
2424
const basicModulesConfig = require( './webpack/basicModules.config' );
25+
const blockEditorConfig = require( './webpack/blockEditor.config' );
2526
const frontendModules = require( './webpack/frontendModules.config' );
2627
const modulesConfig = require( './webpack/modules.config' );
2728
const testBundleConfig = require( './webpack/testBundle.config' );
@@ -45,6 +46,9 @@ function* webpackConfig( env, argv ) {
4546
// Build modules that will be used on the frontend, that require wider browser support.
4647
yield frontendModules( mode );
4748

49+
// Build the block editor js.
50+
yield blockEditorConfig( mode );
51+
4852
// Build the main plugin admin css.
4953
yield adminCssConfig( mode );
5054
}

webpack/blockEditor.config.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Block editor config webpack partial.
3+
*
4+
* Site Kit by Google, Copyright 2024 Google LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* External dependencies
21+
*/
22+
const CircularDependencyPlugin = require( 'circular-dependency-plugin' );
23+
const ESLintPlugin = require( 'eslint-webpack-plugin' );
24+
const ManifestPlugin = require( 'webpack-manifest-plugin' );
25+
const WebpackBar = require( 'webpackbar' );
26+
27+
/**
28+
* Internal dependencies
29+
*/
30+
const {
31+
rootDir,
32+
manifestArgs,
33+
resolve,
34+
gutenbergExternals,
35+
createRules,
36+
createMinimizerRules,
37+
} = require( './common' );
38+
39+
module.exports = ( mode ) => ( {
40+
entry: {
41+
'googlesitekit-reader-revenue-manager-block-editor':
42+
'./assets/js/googlesitekit-reader-revenue-manager-block-editor.js',
43+
},
44+
externals: gutenbergExternals,
45+
output: {
46+
filename:
47+
mode === 'production' ? '[name]-[contenthash].js' : '[name].js',
48+
path: rootDir + '/dist/assets/js',
49+
publicPath: '',
50+
},
51+
module: {
52+
rules: createRules( mode ),
53+
},
54+
plugins: [
55+
new WebpackBar( {
56+
name: 'Block Editor Entry Points',
57+
color: '#deff13',
58+
} ),
59+
new CircularDependencyPlugin( {
60+
exclude: /node_modules/,
61+
failOnError: true,
62+
allowAsyncCycles: false,
63+
cwd: process.cwd(),
64+
} ),
65+
new ManifestPlugin( {
66+
...manifestArgs( mode ),
67+
filter( file ) {
68+
return ( file.name || '' ).match( /\.js$/ );
69+
},
70+
} ),
71+
new ESLintPlugin( {
72+
emitError: true,
73+
emitWarning: true,
74+
failOnError: true,
75+
} ),
76+
],
77+
optimization: {
78+
minimizer: createMinimizerRules( mode ),
79+
},
80+
resolve,
81+
} );

webpack/modules.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ module.exports = ( mode, rules, ANALYZE ) => {
117117
// If multiple webpack runtimes (from different compilations) are used on the
118118
// same webpage, there is a risk of conflicts of on-demand chunks in the global
119119
// namespace.
120-
// See: https://webpack.js.org/configuration/output/#outputjsonpfunction.
120+
// See: https://v4.webpack.js.org/configuration/output/#outputjsonpfunction.
121121
jsonpFunction: '__googlesitekit_webpackJsonp',
122122
},
123123
performance: {

0 commit comments

Comments
 (0)