Skip to content

Commit

Permalink
#3239 load clients from webpack server if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed May 29, 2019
1 parent dd63aed commit cc4b7e4
Show file tree
Hide file tree
Showing 2,785 changed files with 407,045 additions and 2,138 deletions.
6 changes: 4 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"presets": [ "@wordpress/default" ]
}
"presets": [
"@calderajs/babel-preset-calderajs"
]
}
2 changes: 0 additions & 2 deletions caldera-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ function caldera_forms_load()
//@see https://github.com/CalderaWP/Caldera-Forms/issues/2855
add_filter( 'caldera_forms_pro_log_mode', '__return_false' );
add_filter( 'caldera_forms_pro_mail_debug', '__return_false' );


}

/**
Expand Down
115 changes: 114 additions & 1 deletion cf2/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function caldera_forms_get_v2_container()
}

/**
<<<<<<< HEAD
* Setup Cf2 container
*
* @since 1.8.0
Expand Down Expand Up @@ -69,3 +68,117 @@ function caldera_forms_schedule_job(\calderawp\calderaforms\cf2\Jobs\Job $job, $
->schedule($job, $delay);
}




/**
* Attempt to load a file at the specified path and parse its contents as JSON.
*
* @param string $path The path to the JSON file to load.
* @return array|null;
*/
function caldera_forms_load_asset_file( $path ) {
if ( ! file_exists( $path ) ) {
return null;
}
$contents = file_get_contents( $path );
if ( empty( $contents ) ) {
return null;
}
return json_decode( $contents, true );
}

/**
* Check a directory for a root or build asset manifest file, and attempt to
* decode and return the asset list JSON if found.
*
* @param string $directory Root directory containing `src` and `build` directory.
* @return array|null;
*/
function caldera_forms_get_assets_list( $manifest_path ) {
$dev_assets = caldera_forms_load_asset_file($manifest_path);
var_dump($dev_assets);exit;
if ( ! empty( $dev_assets ) ) {
return array_values( $dev_assets );
}

return null;
}


function caldera_forms_enqueue_assets( $manifest_path, $opts = [] ) {
$defaults = [
'handle' => basename( plugin_dir_path( $manifest_path ) ),
'filter' => '__return_true',
'scripts' => [],
'styles' => [],
];

$opts = wp_parse_args( $opts, $defaults );

$assets =caldera_forms_get_assets_list($manifest_path);
var_dump($assets);exit;
if ( empty( $assets ) ) {
// Trust the theme or pluign to handle its own asset loading.
return false;
}

// Keep track of whether a CSS file has been encountered.
$has_css = false;

// There should only be one JS and one CSS file emitted per plugin or theme.
foreach ( $assets as $asset_uri ) {
if ( $opts['filter'] && ! $opts['filter']( $asset_uri ) ) {
// Ignore file paths which do not pass the provided filter test.
continue;
}

$is_js = preg_match( '/\.js$/', $asset_uri );
$is_css = preg_match( '/\.css$/', $asset_uri );
$is_chunk = preg_match( '/\.chunk\./', $asset_uri );

if ( ( ! $is_js && ! $is_css ) || $is_chunk ) {
// Assets such as source maps and images are also listed; ignore these.
continue;
}

//Upgrade to HTTPS
if( is_ssl() ){
$asset_uri = preg_replace("/^http:/i", "https:", $asset_uri);
}

$split = explode( '/', $asset_uri);
$name = end($split);
if ( $is_js && 'editor.js' === $name) {
wp_enqueue_script(
$opts['handle'],
$asset_uri,
$opts['scripts'],
filemtime( $manifest_path ),
true
);
} elseif ( $is_css ) {
$has_css = true;
wp_enqueue_style(
$opts['handle'],
$asset_uri,
$opts['styles'],
filemtime( $manifest_path )
);
}
}

// Ensure CSS dependencies are always loaded, even when using CSS-in-JS in
// development.
if ( ! $has_css && ! empty( $opts['styles'] ) ) {
wp_register_style(
$opts['handle'],
null,
$opts['styles']
);
wp_enqueue_style( $opts['handle'] );
}

// Signal that auto-loading occurred.
return true;
}
94 changes: 72 additions & 22 deletions classes/render/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class Caldera_Forms_Render_Assets
*/
protected static $client_modified_time;


/**
* The array from webpack manifest
*
* @sicne 1.8.6
*
* @var array
*/
protected static $webpack_asset_manifest;

/**
* Enqueue styles for field type
*
Expand Down Expand Up @@ -579,11 +589,39 @@ public static function make_url($name, $script = true)
$name = 'admin';
}

$version = absint(self::get_client_modified_time());
$manifest = self::get_webpack_manifest();

if ($script) {
return "{$root_url}clients/{$name}/build/index.min.js?h={$version}";
} else {
return "{$root_url}clients/{$name}/build/style.min.css?h={$version}";
if (
in_array($name, [
'blocks',
'pro',
'privacy',
'legacy-bundle'
])
|| empty($manifest)
|| ! array_key_exists("{$name}.js",$manifest)
) {
return "{$root_url}clients/{$name}/build/index.min.js";
} else {
return $manifest["{$name}.js"];
}
} else {
if (
in_array($name, [
'blocks',
'pro',
'privacy',
'legacy-bundle'
])
|| empty($manifest)
|| ! array_key_exists("{$name}.css",$manifest)
) {
return "{$root_url}clients/{$name}/build/style.min.css";

}else{
return $manifest["{$name}.css"];
}
}
}

Expand All @@ -603,24 +641,6 @@ public static function make_url($name, $script = true)

}

/**
* Get the time the clients directory was modified at.
*
* - Used to hash the file URLs for client entry points.
* - This avoids running filetime or md5 hash on all 10 files.
*
* @since 1.8.6
*
* @return int
*/
protected static function get_client_modified_time(){
return rand();
if( ! self::$client_modified_time ){
$dir = dirname(__FILE__,3). '/clients/admin/build/index.min.js' ;
self::$client_modified_time = filemtime($dir);
}
return self::$client_modified_time;
}

/**
* Is this the slug of a webpack entry point
Expand Down Expand Up @@ -1112,4 +1132,34 @@ protected static function is_beaver_builder_editor(){
return isset($_GET, $_GET[ 'fl_builder']);
}


protected static function get_webpack_manifest()
{
if( ! self::$webpack_asset_manifest ){
$path = CFCORE_PATH .'/config/asset-manifest.json';
if (!file_exists($path)) {
return [];
}
$contents = file_get_contents($path);
if (empty($contents)) {
return [];
}
$assets = json_decode($contents, true);
$prepared = [];
if( $assets ){
foreach ($assets as $asset => $url ){
if( false === strpos($asset,'.map' ) ) {
$prepared[$asset] = $url;

}
}
}
self::$webpack_asset_manifest = $prepared;

}
return self::$webpack_asset_manifest;

}


}
18 changes: 18 additions & 0 deletions clients/deleteBuilds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const path = require( 'path' );
[
__dirname + '/admin/build',
__dirname + '/render/build',
__dirname + '/privacy/build',
//__dirname + '/blocks/build',
].forEach( directory => {
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
console.log(path.join(directory, file));
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
});
7 changes: 3 additions & 4 deletions clients/legacy-bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import isShallowEqual from '@wordpress/is-shallow-equal';
import reduxRoutine from '@wordpress/redux-routine';
import components from '@wordpress/components';
import blocks from '@wordpress/blocks';
import utils from '@wordpress/utils';
import date from '@wordpress/date';
import utils from '@wordpress/editor';
import date from '@wordpress/editor';
import editor from '@wordpress/editor';

function wpDependencies(){

const dependencies = wpBabel + browserlistConfig + data + deprecated +
Expand All @@ -20,4 +19,4 @@ function wpDependencies(){

return dependencies;
}
wpDependencies();
wpDependencies();
Loading

0 comments on commit cc4b7e4

Please sign in to comment.