Created
October 31, 2019 19:13
-
-
Save deavy/79bbbe9e437862e774fbb44d9fc4ee34 to your computer and use it in GitHub Desktop.
Replace old jQuery in Wordpress with the latest version from node_modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$vendor_version = '%jquery_version%'; | |
$replace_jquery = ! is_admin() && ! is_customize_preview(); | |
$header_scripts_handler = 'vendor-header-scripts'; | |
if ( $replace_jquery ) { | |
wp_deregister_script( 'jquery' ); // Deregister WP core jQuery | |
wp_deregister_script( 'jquery-core' ); // Deregister WP jQuery | |
wp_deregister_script( 'jquery-migrate' ); // Deregister WP jQuery Migrate | |
// Replace built-in jQuery with the latest version from node_modules | |
$header_scripts_handler = 'jquery'; | |
} | |
wp_enqueue_script( $header_scripts_handler, get_stylesheet_directory_uri() . '/scripts/vendor-header.js', array(), $vendor_version, false ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { dest, src } from 'gulp'; | |
import fs from 'fs'; | |
import concat from 'gulp-concat'; | |
import gulpif from 'gulp-if'; | |
import plumber from 'gulp-plumber'; | |
import terser from 'gulp-terser'; | |
// Setup your project paths, settings, etc: | |
const paths = { ... }; | |
// Important header scripts | |
const headerJS = [ | |
`${paths.node}/jquery/dist/jquery.js`, | |
// ... | |
// ... | |
]; | |
// Error handler | |
const onError = err => { | |
log(`Error: ${err.toString()}`); | |
}; | |
export const processHeaderScripts = () => { | |
return src(headerJS) | |
.pipe(plumber({ errorHandler: onError })) | |
.pipe(concat('vendor-header.js')) | |
.pipe(terser()) | |
.pipe(dest(paths.scripts.prod)); | |
}; | |
export const processThemeFiles = () => { | |
const pkg = JSON.parse(fs.readFileSync('./package.json')); | |
const version = pkg.dependencies.jquery.replace(/[^0-9.]/g, ''); | |
return src(paths.theme.src) | |
.pipe(gulpif('functions.php', replace('%jquery_version%', version))) | |
.pipe(dest(paths.theme.prod)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment