Revert "Revert "fix: export router as a separated package""

This reverts commit 90abc56862.
This commit is contained in:
Tung Du 2024-09-18 20:02:11 +07:00
parent 0a0b436828
commit 08050b38c0
7 changed files with 49 additions and 25 deletions

View File

@ -1,8 +1,12 @@
/**
* External dependencies
*/
import { store, privateApis, getConfig } from '@woocommerce/interactivity';
/**
* Internal dependencies
*/
import { fetchHeadAssets, updateHead } from './head';
import { store, privateApis, getConfig } from './index';
const {
directivePrefix,
@ -205,7 +209,7 @@ const isValidEvent = ( event: MouseEvent ) =>
// Variable to store the current navigation.
let navigatingTo = '';
export const { state, actions } = store( 'core/router', {
export const { state, actions } = store( 'woocommerce/router', {
state: {
url: window.location.href,
navigation: {

View File

@ -18,7 +18,6 @@ import { getNamespace } from './namespaces';
import { parseServerData, populateServerData } from './store';
import { proxifyState } from './proxies';
export { navigate } from './router';
export { store, getConfig } from './store';
export { getContext, getElement } from './scopes';
export {

View File

@ -952,12 +952,13 @@ const getInteractivityAPIConfig = ( options = {} ) => {
const { alias, resolvePlugins = [] } = options;
return {
entry: {
'wc-interactivity': './assets/js/interactivity',
interactivity: './assets/js/interactivity',
'interactivity-router': './assets/js/interactivity-router',
},
output: {
filename: '[name].js',
filename: 'wc-[name].js',
path: path.resolve( __dirname, '../build/' ),
library: [ 'wc', '__experimentalInteractivity' ],
library: [ 'wc', '__experimentalInteractivity', '[name]' ],
libraryTarget: 'this',
chunkLoadingGlobal: 'webpackWcBlocksJsonp',
},

View File

@ -18,7 +18,16 @@ const wcDepMap = {
'@woocommerce/price-format': [ 'wc', 'priceFormat' ],
'@woocommerce/blocks-checkout': [ 'wc', 'blocksCheckout' ],
'@woocommerce/blocks-components': [ 'wc', 'blocksComponents' ],
'@woocommerce/interactivity': [ 'wc', '__experimentalInteractivity' ],
'@woocommerce/interactivity': [
'wc',
'__experimentalInteractivity',
'interactivity',
],
'@woocommerce/interactivity-router': [
'wc',
'__experimentalInteractivity',
'interactivity-router',
],
'@woocommerce/types': [ 'wc', 'wcTypes' ],
};
@ -33,6 +42,7 @@ const wcHandleMap = {
'@woocommerce/blocks-checkout': 'wc-blocks-checkout',
'@woocommerce/blocks-components': 'wc-blocks-components',
'@woocommerce/interactivity': 'wc-interactivity',
'@woocommerce/interactivity-router': 'wc-interactivity-router',
'@woocommerce/types': 'wc-types',
};
@ -68,6 +78,10 @@ const getAlias = ( options = {} ) => {
__dirname,
`../assets/js/${ pathPart }interactivity/`
),
'@woocommerce/interactivity-router': path.resolve(
__dirname,
`../assets/js/${ pathPart }interactivity-router/`
),
'@woocommerce/base-utils': path.resolve(
__dirname,
`../assets/js/${ pathPart }base/utils/`

View File

@ -42,6 +42,7 @@
"@woocommerce/base-hooks": [ "assets/js/base/hooks" ],
"@woocommerce/interactivity": [ "assets/js/interactivity" ],
"@woocommerce/interactivity/*": [ "assets/js/interactivity/*" ],
"@woocommerce/interactivity-router": [ "assets/js/interactivity-router" ],
"@woocommerce/base-utils": [ "assets/js/base/utils" ],
"@woocommerce/base-utils/*": [ "assets/js/base/utils/*" ],
"@woocommerce/blocks/*": [ "assets/js/blocks/*" ],

View File

@ -30,24 +30,29 @@ function woocommerce_interactivity_register_runtime() {
$plugin_path = \Automattic\WooCommerce\Blocks\Package::get_path();
$plugin_url = plugin_dir_url( $plugin_path . '/index.php' );
$file = 'assets/client/blocks/wc-interactivity.js';
$file_path = $plugin_path . $file;
$file_url = $plugin_url . $file;
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file_path ) ) {
$version = filemtime( $file_path );
} else {
// Use wc- prefix here to prevent collisions when WC Core version catches up to a version previously used by the WC Blocks feature plugin.
$version = 'wc-' . Constants::get_constant( 'WC_VERSION' );
}
wp_register_script(
'wc-interactivity',
$file_url,
array(),
$version,
true
$files = array(
'wc-interactivity' => 'assets/client/blocks/wc-interactivity.js',
'wc-interactivity-router' => 'assets/client/blocks/wc-interactivity-router.js',
);
foreach ( $files as $handle => $file ) {
$file_path = $plugin_path . $file;
$file_url = $plugin_url . $file;
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file_path ) ) {
$version = filemtime( $file_path );
} else {
// Use wc- prefix here to prevent collisions when WC Core version catches up to a version previously used by the WC Blocks feature plugin.
$version = 'wc-' . Constants::get_constant( 'WC_VERSION' );
}
wp_register_script(
$handle,
$file_url,
array(),
$version,
true
);
}
}
add_action( 'wp_enqueue_scripts', 'woocommerce_interactivity_register_runtime' );