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 1 commit
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
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions projects/js-packages/initial-state/.eslintrc.js
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: [ require.resolve( 'jetpack-js-tools/eslintrc/base' ) ],
parserOptions: {
requireConfigFile: false,
},
};
7 changes: 7 additions & 0 deletions projects/js-packages/initial-state/.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/initial-state/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
node_modules/
15 changes: 15 additions & 0 deletions projects/js-packages/initial-state/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Initial State

A library to provide initial state 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)
34 changes: 34 additions & 0 deletions projects/js-packages/initial-state/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "automattic/jetpack-initial-state",
"description": "A library to provide initial state 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-initial-state",
"changelogger": {
"link-template": "https://github.com/Automattic/jetpack-initial-state/compare/v${old}...v${new}"
},
"autotagger": true
}
}
23 changes: 23 additions & 0 deletions projects/js-packages/initial-state/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"private": true,
"name": "@automattic/jetpack-initial-state",
"version": "0.0.1",
"description": "A library to provide initial state and the corresponding utility functions for Jetpack.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/initial-state/#readme",
"bugs": {
"url": "https://github.com/Automattic/jetpack/labels/[JS Package] Initial State"
},
"repository": {
"type": "git",
"url": "https://github.com/Automattic/jetpack.git",
"directory": "projects/js-packages/initial-state"
},
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {},
"dependencies": {},
"devDependencies": {},
"exports": {
".": "./src/index.ts"
}
}
2 changes: 2 additions & 0 deletions projects/js-packages/initial-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './utils';
27 changes: 27 additions & 0 deletions projects/js-packages/initial-state/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface SiteData {
admin_url: string;
blog_id: number;
rest_nonce: string;
rest_root: string;
title: string;
}

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

export interface UserData {
current_user: CurrentUserData;
}

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

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

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

/**
* Get the admin URL from the initial state.
*
* @param {string} [path] - The path to append to the admin URL.
*
* @returns {string} The admin URL.
*/
export function getAdminUrl( path = '' ) {
return `${ getInitialState().site.admin_url }${ path }`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get to actually test this because it's not implemented by other packages, but won't this cause URLs to miss a forward slash after the admin URL? like https://yoursite.comadmin.php?page...?

Copy link
Member Author

@manzoorwanijk manzoorwanijk Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

admin_url is always with a trailing slash. What we might want to do here is ltrim() as done by get_admin_url. Do we need that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see its use just below there in getMyJetpackUrl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

admin_url is always with a trailing slash. What we might want to do here is ltrim() as done by get_admin_url. Do we need that?

No, if we have a trailing slash, I think that's enough.

}

/**
* Get the url for the My Jetpack page.
*
* @param {string} [section] - The section to append to the My Jetpack URL.
*
* @returns {string} The My Jetpack URL.
*/
export function getMyJetpackUrl( section = '' ) {
return getAdminUrl( `admin.php?page=my-jetpack${ section }` );
}
5 changes: 5 additions & 0 deletions projects/js-packages/initial-state/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" ]
}
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-initial-state': {
external: 'JetpackInitialState',
handle: 'jetpack-initial-state',
},
};

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\Initial_State::instance(), '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\Initial_State::instance(), 'configure' ),
);
}
3 changes: 3 additions & 0 deletions projects/packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"test": "jest tests",
"validate": "pnpm exec validate-es build/"
},
"dependencies": {
"@automattic/jetpack-initial-state": "workspace:*"
},
"devDependencies": {
"@automattic/jetpack-webpack-config": "workspace:*",
"@wordpress/browserslist-config": "6.2.0",
Expand Down
Loading
Loading