Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Try/add dependency extraction plugin webpack #109

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions config/webpack.config.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const WebpackBar = require('webpackbar');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const DependencyExtractionWebpackPlugin = require('@wordpress/dependency-extraction-webpack-plugin');

const isProduction = process.env.NODE_ENV === 'production';

Expand Down Expand Up @@ -144,5 +145,8 @@ module.exports = {

// Fancy WebpackBar.
new WebpackBar(),

// Extract depenencies
new DependencyExtractionWebpackPlugin({ injectPolyfill: true, combineAssets: true }),
],
};
64 changes: 56 additions & 8 deletions includes/functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,54 @@ function script_url( $script, $context ) {

}

/**
* Retrieve the list of script dependencies for the script being loaded.
*
* @param string $script Script file name (no .js extension)
*
* @return array The array containing the dependencies and version
*/
function script_dependencies( $script ) {
// The file hasn't been created
if ( ! file_exists( TENUP_SCAFFOLD_PATH . '/dist/assets.php' ) ) {
return [];
}

// include the assets.
$assets = include TENUP_SCAFFOLD_PATH . '/dist/assets.php';

// Does this script have depenedencies
if ( ! isset( $assets[ "js/{$script}.js" ] ) ) {
return [];
}

return $assets[ "js/{$script}.js" ]['dependencies'];
}

/**
* Retrieve the version for the script
*
* @param string $script Script file name (no .js extension)
*
* @return string Version
*/
function script_version( $script ) {
// The file hasn't been created
if ( ! file_exists( TENUP_SCAFFOLD_PATH . '/dist/assets.php' ) ) {
return TENUP_SCAFFOLD_VERSION;
}

// include the assets.
$assets = include TENUP_SCAFFOLD_PATH . '/dist/assets.php';

// Does this script have depenedencies
if ( ! isset( $assets[ "js/{$script}.js" ] ) ) {
return TENUP_SCAFFOLD_VERSION;
}

return $assets[ "js/{$script}.js" ]['version'];
}

/**
* Generate an URL to a stylesheet, taking into account whether SCRIPT_DEBUG is enabled.
*
Expand Down Expand Up @@ -132,16 +180,16 @@ function scripts() {
wp_enqueue_script(
'tenup_scaffold_shared',
script_url( 'shared', 'shared' ),
[],
TENUP_SCAFFOLD_VERSION,
array_merge( script_dependencies( 'shared' ), [] ), // Add any manual enqeues to the second array.
script_version( 'shared' ),
true
);

wp_enqueue_script(
'tenup_scaffold_frontend',
script_url( 'frontend', 'frontend' ),
[],
TENUP_SCAFFOLD_VERSION,
array_merge( script_dependencies( 'frontend' ), [] ), // Add any manual enqeues to the second array.
script_version( 'frontend' ),
true
);

Expand All @@ -157,16 +205,16 @@ function admin_scripts() {
wp_enqueue_script(
'tenup_scaffold_shared',
script_url( 'shared', 'shared' ),
[],
TENUP_SCAFFOLD_VERSION,
array_merge( script_dependencies( 'shared' ), [] ), // Add any manual enqeues to the second array.
script_version( 'shared' ),
true
);

wp_enqueue_script(
'tenup_scaffold_admin',
script_url( 'admin', 'admin' ),
[],
TENUP_SCAFFOLD_VERSION,
array_merge( script_dependencies( 'admin' ), [] ), // Add any manual enqeues to the second array.
script_version( 'admin' ),
true
);

Expand Down
Loading