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 3 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.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 = 'production' === process.env.NODE_ENV;

Expand Down Expand Up @@ -152,5 +153,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
58 changes: 47 additions & 11 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@babel/register": "^7.7.7",
"@wordpress/dependency-extraction-webpack-plugin": "^2.3.0",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"browser-sync": "^2.26.7",
Expand All @@ -60,10 +61,10 @@
"eslint": "^6.8.0",
"eslint-loader": "^3.0.3",
"husky": "^3.1.0",
"imagemin-webpack-plugin": "^2.4.2",
"lint-staged": "^9.5.0",
"mini-css-extract-plugin": "^0.9.0",
"postcss-import": "^12.0.1",
"imagemin-webpack-plugin": "^2.4.2",
"postcss-loader": "^3.0.0",
"postcss-preset-env": "^6.7.0",
"stylelint": "^9.10.1",
Expand Down