Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 19dd5d9

Browse files
committed
Build process streamlining
* Removing “min” suffix and associated structure; with sourcemaps there is no need for this anymore * `images-dist` now `images-optimize` to better explain what this function does * More and better comments, particularly in the assets-loading file
1 parent a1f7863 commit 19dd5d9

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

gulpconfig.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ module.exports = {
5353
, chunks: { // Chunks are arrays of paths or globs matching a set of source files; this way you can organize a bunch of scripts that go together into pieces that can then be bundled (above)
5454
// The core chunk is loaded no matter what; put essential scripts that you want loaded by your theme in here
5555
core: [
56-
modules+'timeago/jquery.timeago.js'
56+
modules+'timeago/jquery.timeago.js' // The modules directory contains packages downloaded via npm
5757
, src+'js/responsive-menu.js'
5858
, src+'js/core.js'
5959
]
6060
// The pageloader chunk provides an example of how you would add a user-configurable feature to your theme; you can delete this if you wish
6161
// Have a look at the `src/inc/assets.php` to see how script bundles could be conditionally loaded by a theme
6262
, pageloader: [
63-
modules+'html5-history-api/history.js' // The modules directory contains packages downloaded via npm
63+
modules+'html5-history-api/history.js'
6464
, modules+'spin.js/spin.js'
6565
, modules+'spin.js/jquery.spin.js'
6666
, modules+'wp-ajax-page-loader/wp-ajax-page-loader.js'
@@ -72,8 +72,7 @@ module.exports = {
7272
src: [src+'js/**/*.js'] // Linting checks the quality of the code; we only lint custom scripts, not those under the various modules, so we're relying on the original authors to ship quality code
7373
}
7474
, minify: {
75-
src: [build+'js/**/*.js', '!'+build+'js/**/*.min.js'] // Avoid recursive min.min.min.js
76-
, rename: { suffix: '.min' }
75+
src: build+'js/**/*.js'
7776
, uglify: {} // Default options
7877
, dest: build+'js/'
7978
}

gulpfile.js/tasks/images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ gulp.task('images', function() {
1313
});
1414

1515
// Optimize images in the `dist` folder (slow)
16-
gulp.task('images-dist', ['utils-dist'], function() {
16+
gulp.task('images-optimize', ['utils-dist'], function() {
1717
return gulp.src(config.dist.src)
1818
.pipe(plugins.imagemin(config.dist.imagemin))
1919
.pipe(gulp.dest(config.dist.dest));

gulpfile.js/tasks/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ gulp.task('build', ['images', 'scripts', 'styles', 'theme']);
1313

1414
// Dist task chain: wipe -> build -> clean -> copy -> compress images
1515
// NOTE: this is a resource-intensive task!
16-
gulp.task('dist', ['images-dist']);
16+
gulp.task('dist', ['images-optimize']);

gulpfile.js/tasks/scripts.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var gulp = require('gulp')
99
// Check core scripts for errors
1010
gulp.task('scripts-lint', function() {
1111
return gulp.src(config.lint.src)
12-
.pipe(plugins.jshint('.jshintrc'))
12+
.pipe(plugins.jshint())
1313
.pipe(plugins.jshint.reporter('default')); // No need to pipe this anywhere
1414
});
1515

@@ -47,8 +47,9 @@ gulp.task('scripts-bundle', ['scripts-lint'], function(){
4747
// Minify scripts in place
4848
gulp.task('scripts-minify', ['scripts-bundle'], function(){
4949
return gulp.src(config.minify.src)
50-
.pipe(plugins.rename(config.minify.rename))
50+
.pipe(plugins.sourcemaps.init())
5151
.pipe(plugins.uglify(config.minify.uglify))
52+
.pipe(plugins.sourcemaps.write('./'))
5253
.pipe(gulp.dest(config.minify.dest));
5354
});
5455

src/inc/assets.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
<?php // ==== ASSETS ==== //
22

33
// Now that you have efficiently generated scripts and stylesheets for your theme, how should they be integrated?
4-
// This file walks you through the approach I use but you are free to approach this any way you like
4+
// This file walks you through the approach I use but you are free to do this any way you like
55

66
// Enqueue front-end scripts and styles
77
function voidx_enqueue_scripts() {
88

9-
$script_name = ''; // Empty by default, may be populated by conditionals below
9+
$script_name = ''; // Empty by default, may be populated by conditionals below; this is used to generate the script filename
1010
$script_vars = array(); // An empty array that can be filled with variables to send to front-end scripts
11-
$script_handle = 'voidx'; // A generic script handle
12-
$suffix = '.min'; // The suffix for minified scripts
13-
$ns = 'wp'; // Namespace for scripts
14-
15-
// Load original scripts when debug mode is on; this allows for easier local development
16-
if ( WP_DEBUG === true )
17-
$suffix = '';
11+
$script_handle = 'voidx'; // A generic script handle used internally by WordPress
12+
$ns = 'wp'; // Namespace for scripts; this should match what is specified in your `gulpconfig.js` (and it's safe to leave alone)
1813

1914
// Figure out which script bundle to load based on various options set in `src/functions-config-defaults.php`
20-
// Note: bundles require less HTTP requests at the expense of addition caching hits when different scripts are requested
21-
// You could also load one main bundle on every page with supplementary scripts as needed (e.g. for commenting or a contact page)
15+
// Note: bundles require fewer HTTP requests at the expense of addition caching hits when different scripts are requested on different pages of your site
16+
// You could also load one main bundle on every page with supplementary scripts as needed (e.g. for commenting or a contact page); it's up to you!
17+
18+
19+
20+
// == EXAMPLE INTEGRATION == //
2221

2322
// An example integration using WP AJAX Page Loader; this script requires a bit more setup as outlined in the documentation: https://github.com/synapticism/wp-ajax-page-loader
2423
$script_vars_page_loader = '';
2524

2625
// This conditional establishes whether the page loader bundle is loaded or not; you can turn this off completely from the theme configuration file if you wish (or just remove the code)
2726
if ( VOIDX_SCRIPTS_PAGELOAD && ( is_archive() || is_home() || is_search() ) ) {
28-
$script_name .= '-pageloader'; // This is used to generate the filename that the theme will serve to the user; it is additive to allow for multiple features that can be toggled in the theme configuration
2927

30-
global $wp_query;
28+
// The script name is used to specify the file that the theme will serve to users
29+
// Script names are designed to be additive (meaning you can append more script names to the end in other conditional blocks using `.= '-anotherscript'` etc.) to allow for multiple feature toggles in the theme configuration
30+
$script_name .= '-pageloader';
3131

32-
// This chunk of code provisions the script with some important information it needs: What page are we on? And what is the page limit?
32+
// This chunk of code provisions the WP AJAX Page Loader script with some important information it needs: What page are we on? And what is the page limit?
33+
global $wp_query;
3334
$max = $wp_query->max_num_pages;
3435
$paged = ( get_query_var( 'paged' ) > 1 ) ? get_query_var( 'paged' ) : 1;
3536

36-
// Prepare script variables; note that these are separate from the rest of the script variables as this script requires everything in an object named `PG8Data`
37+
// Prepare script variables; note that these are separate from the rest of the script variables as the WP AJAX Page Loader script requires everything in a specific object named `PG8Data`
3738
$script_vars_page_loader = array(
3839
'startPage' => $paged,
3940
'maxPages' => $max,
4041
'nextLink' => next_posts( $max, false )
4142
);
4243
} // WP AJAX Page Loader configuration ends
4344

44-
// Default script name
45+
46+
47+
// Default script name; used when conditional blocks (above) aren't triggered
4548
if ( empty( $script_name ) )
4649
$script_name = '-core';
4750

4851
// Load theme-specific JavaScript bundles with versioning based on last modified time; http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/
4952
// The handle is the same for each bundle since we're only loading one script; if you load others be sure to provide a new handle
50-
wp_enqueue_script( $script_handle, get_stylesheet_directory_uri() . '/js/' . $ns . $script_name . $suffix . '.js', array( 'jquery' ), filemtime( get_template_directory() . '/js/' . $ns . $script_name . $suffix . '.js' ), true );
53+
wp_enqueue_script( $script_handle, get_stylesheet_directory_uri() . '/js/' . $ns . $script_name . '.js', array( 'jquery' ), filemtime( get_template_directory() . '/js/' . $ns . $script_name . '.js' ), true );
5154

5255
// Pass variables to JavaScript at runtime; see: http://codex.wordpress.org/Function_Reference/wp_localize_script
5356
$script_vars = apply_filters( 'voidx_script_vars', $script_vars );
5457
if ( !empty( $script_vars ) )
5558
wp_localize_script( $script_handle, 'voidxVars', $script_vars );
5659

57-
// Script variables for WP AJAX Page Loader (these are separate from the main theme script variables due to the naming requirement; the object must be `PG8Data`)
60+
// Script variables specific to WP AJAX Page Loader (these are separate from the main theme script variables due to the naming requirement; the object must be `PG8Data`)
61+
// This appears and NOT in the conditional block (above) because these variables will be attached to the main script handle (which may be modified after the page loader block)
5862
if ( !empty( $script_vars_page_loader ) )
5963
wp_localize_script( $script_handle, 'PG8Data', $script_vars_page_loader );
6064

@@ -70,7 +74,7 @@ function voidx_enqueue_scripts() {
7074
// Provision the front-end with the appropriate script variables
7175
function voidx_update_script_vars( $script_vars = array() ) {
7276

73-
// Non-destructively merge script variables if a particular condition is met (e.g. `is_archive()` or whatever)
77+
// Non-destructively merge script variables if a particular condition is met (e.g. `is_archive()` or whatever); useful for managing many different kinds of script variables
7478
if ( 1 == 1 ) {
7579
$script_vars = array_merge( $script_vars, array(
7680
'ajaxUrl' => admin_url( 'admin-ajax.php' ),

0 commit comments

Comments
 (0)