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

Consolidate Jetpack initial state #38430

Merged
merged 48 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
18026f2
WIP
manzoorwanijk Jul 19, 2024
a4e0b93
Make the whole class static
manzoorwanijk Jul 22, 2024
2c677a3
Use `in-footer`
manzoorwanijk Jul 22, 2024
bb90514
Reset my-jetpack from trunk
manzoorwanijk Jul 22, 2024
59dc46c
Remove unnecessary checks for asset registration
manzoorwanijk Jul 22, 2024
590d56f
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 22, 2024
5b0ac8a
Add changelog
manzoorwanijk Jul 22, 2024
a8f41a8
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 22, 2024
713c909
Delete .eslintrc.js
manzoorwanijk Jul 22, 2024
ac4b37b
Set type to module
manzoorwanijk Jul 22, 2024
d32dc2d
Fix case
manzoorwanijk Jul 23, 2024
16a9564
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 23, 2024
1ba5b7d
Update pnpm-lock.yaml
manzoorwanijk Jul 23, 2024
f927d65
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 24, 2024
436c436
Silence filemtime warning
manzoorwanijk Jul 25, 2024
7acde2b
Use print_scripts hook instead of enqueue_scripts to render initial s…
manzoorwanijk Jul 26, 2024
d4fd7a9
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 26, 2024
14a1cd6
Reset changes in connection package
manzoorwanijk Jul 26, 2024
745918a
Have separate filters for admin and public scripts
manzoorwanijk Jul 26, 2024
066270e
Add more common fields and update types
manzoorwanijk Jul 29, 2024
7e2adfa
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 30, 2024
aadfeb3
Fix up versions
manzoorwanijk Jul 30, 2024
948e8ff
Create .gitkeep
manzoorwanijk Jul 30, 2024
d45437a
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 30, 2024
29daeaf
Fix up versions
manzoorwanijk Jul 30, 2024
7a0834f
Reset messed up versions
manzoorwanijk Jul 30, 2024
acaef37
Add the unnecessary changelog
manzoorwanijk Jul 30, 2024
d5abc90
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Jul 31, 2024
d0fc2d6
Remove unnecessary noise of changelogs
manzoorwanijk Jul 31, 2024
bdf049a
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
79ec614
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
78271ff
Use JetpackInitialState
manzoorwanijk Aug 1, 2024
c625bfe
Update doc blocks
manzoorwanijk Aug 1, 2024
3813e96
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 1, 2024
2276331
Fix up versions
manzoorwanijk Aug 1, 2024
76022bf
Rename the package to `script-data`
manzoorwanijk Aug 2, 2024
5fb4553
Update build config
manzoorwanijk Aug 2, 2024
df778a8
Update js-package name
manzoorwanijk Aug 2, 2024
659bea0
Use separate global variables for data and module
manzoorwanijk Aug 2, 2024
7d6ccf4
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 2, 2024
84d5a5a
Update lockfile
manzoorwanijk Aug 2, 2024
0cd03bc
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 5, 2024
ea91938
Fix up versions
manzoorwanijk Aug 5, 2024
8cc9b45
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 6, 2024
5ac15db
Fix up versions
manzoorwanijk Aug 6, 2024
528fc56
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 6, 2024
d7aa3d7
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 7, 2024
6af1d86
Merge branch 'trunk' into add/consolidate-initial-state
manzoorwanijk Aug 7, 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
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 7 additions & 0 deletions projects/js-packages/script-data/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Files not needed to be distributed in the package.
.gitattributes export-ignore
node_modules export-ignore

# Files to exclude from the mirror repo
/changelog/** production-exclude
/.eslintrc.cjs production-exclude
2 changes: 2 additions & 0 deletions projects/js-packages/script-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
node_modules/
6 changes: 6 additions & 0 deletions projects/js-packages/script-data/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
15 changes: 15 additions & 0 deletions projects/js-packages/script-data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Scrip Helpers

A library to provide data for script handles and the corresponding utility functions for Jetpack.

## Contribute

## Get Help

## Security

Need to report a security vulnerability? Go to [https://automattic.com/security/](https://automattic.com/security/) or directly to our security bug bounty site [https://hackerone.com/automattic](https://hackerone.com/automattic).

## License

publicize is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added jetpack-script-data package to consolidate the logic for Jetpack Initial state
34 changes: 34 additions & 0 deletions projects/js-packages/script-data/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "automattic/jetpack-script-data",
"description": "A library to provide data for script handles and the corresponding utility functions for Jetpack.",
"type": "library",
"license": "GPL-2.0-or-later",
"require": {},
"require-dev": {
"automattic/jetpack-changelogger": "@dev"
},
"autoload": {
"classmap": [
"src/"
]
},
"scripts": {},
"repositories": [
{
"type": "path",
"url": "../../packages/*",
"options": {
"monorepo": true
}
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"mirror-repo": "Automattic/jetpack-script-data",
"changelogger": {
"link-template": "https://github.com/Automattic/jetpack-script-data/compare/v${old}...v${new}"
},
"autotagger": true
}
}
24 changes: 24 additions & 0 deletions projects/js-packages/script-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"private": true,
"name": "@automattic/jetpack-script-data",
"version": "0.1.0-alpha",
"description": "A library to provide data for script handles and the corresponding utility functions for Jetpack.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/script-data/#readme",
"bugs": {
"url": "https://github.com/Automattic/jetpack/labels/[JS Package] Script Data"
},
"repository": {
"type": "git",
"url": "https://github.com/Automattic/jetpack.git",
"directory": "projects/js-packages/script-data"
},
"license": "GPL-2.0-or-later",
"author": "Automattic",
"type": "module",
"scripts": {},
"dependencies": {},
"devDependencies": {},
"exports": {
".": "./src/index.ts"
}
}
2 changes: 2 additions & 0 deletions projects/js-packages/script-data/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './utils';
49 changes: 49 additions & 0 deletions projects/js-packages/script-data/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export interface SitePlan {
product_slug: string;
features?: {
active: Array< string >;
available?: Record< string, Array< string > >;
};
}

export interface WPCOMSiteData {
blog_id: number;
}

export interface PublicSiteData {
icon: string;
title: string;
}

export interface AdminSiteData {
admin_url: string;
date_format: string;
is_multisite: boolean;
plan: SitePlan;
rest_nonce: string;
rest_root: string;
wp_version: string;
wpcom: WPCOMSiteData;
}

export interface SiteData extends PublicSiteData, Partial< AdminSiteData > {}

export interface CurrentUserData {
id: number;
display_name: string;
}

export interface UserData {
current_user: CurrentUserData;
}

export interface JetpackScriptData {
site: SiteData;
user: UserData;
}

declare global {
interface Window {
JetpackScriptData: JetpackScriptData;
}
}
59 changes: 59 additions & 0 deletions projects/js-packages/script-data/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Get the script data from the window object.
*
* @returns {import('./types').JetpackScriptData} The script data.
*/
export function getScriptData() {
return window.JetpackScriptData;
}

/**
* Get the site data from the script data.
*
* @returns {import('./types').SiteData} The site data.
*/
export function getSiteData() {
return getScriptData().site;
}

/**
* Get the admin URL from the script data.
*
* @param {string} [path] - The path to append to the admin URL. e.g. `admin.php?page=jetpack`.
*
* @returns {string} The admin URL.
*/
export function getAdminUrl( path = '' ) {
return `${ getScriptData().site.admin_url }${ path }`;
}

/**
* Get the url for the Jetpack admin page.
*
* @param {string} [section] - The section to append to the My Jetpack URL. e.g. `#/settings`.
*
* @returns {string} The Jetpack admin page URL.
*/
export function getJetpackAdminPageUrl( section = '' ) {
return getAdminUrl( `admin.php?page=jetpack${ section }` );
}

/**
* Get the url for the My Jetpack page.
*
* @param {string} [section] - The section to append to the My Jetpack URL. e.g. `#/connection`.
*
* @returns {string} The My Jetpack URL.
*/
export function getMyJetpackUrl( section = '' ) {
return getAdminUrl( `admin.php?page=my-jetpack${ section }` );
}

/**
* Get active features from the site plan.
*
* @returns {import('./types').SitePlan['features']['active']} The active features.
*/
export function getActiveFeatures() {
return getScriptData().site.plan?.features?.active ?? [];
}
5 changes: 5 additions & 0 deletions projects/js-packages/script-data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "jetpack-js-tools/tsconfig.base.json",
// List all sources and source-containing subdirs.
"include": [ "./src" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added jetpack-initial-state package to consolidate the logic for Initial state
2 changes: 1 addition & 1 deletion projects/js-packages/webpack-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-webpack-config",
"version": "3.2.11",
"version": "3.3.0-alpha",
"description": "Library of pieces for webpack config in Jetpack projects.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/webpack-config/#readme",
"bugs": {
Expand Down
27 changes: 26 additions & 1 deletion projects/js-packages/webpack-config/src/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,32 @@ const DefinePlugin = defines => [
} ),
];

const DependencyExtractionPlugin = options => [ new DependencyExtractionWebpackPlugin( options ) ];
const defaultRequestMap = {
'@automattic/jetpack-script-data': {
external: 'JetpackScriptDataModule',
handle: 'jetpack-script-data',
},
};

const DependencyExtractionPlugin = ( { requestMap, ...options } = {} ) => {
const finalRequestMap = { ...defaultRequestMap, ...requestMap };

const requestToExternal = request => {
return finalRequestMap[ request ]?.external;
};

const requestToHandle = request => {
return finalRequestMap[ request ]?.handle;
};

return [
new DependencyExtractionWebpackPlugin( {
requestToExternal,
requestToHandle,
...options,
} ),
];
};

const DuplicatePackageCheckerPlugin = options => [
new DuplicatePackageCheckerWebpackPlugin( options ),
Expand Down
6 changes: 6 additions & 0 deletions projects/packages/assets/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
// drop data into `$wp_filter` for `WP_Hook::build_preinitialized_hooks()`.
if ( function_exists( 'add_action' ) ) {
add_action( 'wp_default_scripts', array( Automattic\Jetpack\Assets::class, 'wp_default_scripts_hook' ) );
add_action( 'plugins_loaded', array( Automattic\Jetpack\Script_Data::class, 'configure' ), 1 );
} else {
global $wp_filter;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_filter['wp_default_scripts'][10][] = array(
'accepted_args' => 1,
'function' => array( Automattic\Jetpack\Assets::class, 'wp_default_scripts_hook' ),
);
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$wp_filter['plugins_loaded'][1][] = array(
'accepted_args' => 0,
'function' => array( Automattic\Jetpack\Script_Data::class, 'configure' ),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Added jetpack-initial-state package to consolidate the logic for Initial state
2 changes: 1 addition & 1 deletion projects/packages/assets/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"link-template": "https://github.com/Automattic/jetpack-assets/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "2.2.x-dev"
"dev-trunk": "2.3.x-dev"
}
}
}
1 change: 1 addition & 0 deletions projects/packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"validate": "pnpm exec validate-es build/"
},
"dependencies": {
"@automattic/jetpack-script-data": "workspace:*",
"react": "18.3.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/assets/src/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ function ( $d ) {
);
$ver = $options['version'] ?? $asset['version'];
} else {
$ver = $options['version'] ?? filemtime( "$dir/$path" );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$ver = $options['version'] ?? @filemtime( "$dir/$path" );
}

if ( $options['async'] && '' === $options['strategy'] ) { // Handle the deprecated `async` option
Expand Down
Loading
Loading