#64563 closed defect (bug) (fixed)
Grunt uglify:core and copy:files tasks hang when wp-content has many files
| Reported by: | westonruter | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.0 |
| Component: | Build/Test Tools | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: | javascript |
Description
For a long time, I've struggled with npm run build:dev (see Slack thread). When I run the command, it would always seem to hang at:
Running "uglify:core" (uglify) task
I got so fed up with having to wait so long at that task, that I started resorting to using symlinks to avoid having to do a build altogether.
A note on my development environment setup: I clone the repos for the plugins and themes I work on into the wp-content of my wordpress-develop clone which I use for core development.
I got fed up and decided to put Gemini CLI on the problem and it came up with this fix:
-
Gruntfile.js
a b module.exports = function(grunt) { 926 926 'wp-includes/js/tinymce/plugins/wp*/plugin.js', 927 927 928 928 // Exceptions. 929 '! **/*.min.js',929 '!{wp-admin,wp-includes}/**/*.min.js', 930 930 '!wp-admin/js/custom-header.js', // Why? We should minify this. 931 931 '!wp-admin/js/farbtastic.js', 932 932 '!wp-includes/js/wp-emoji-loader.js', // This is a module. See the emoji-loader task below.
The hang was caused by a broad exclusion pattern in the uglify:core task within Gruntfile.js. Its explanation makes perfect sense:
When you run
npm run build:dev, thecwd(current working directory) for the build tasks is set tosrc/.
The original pattern
!**/*.min.jstold Grunt to exclude any minified files found anywhere inside thesrc/directory. To fulfill this request, the globbing engine had to scan every single folder and file withinsrc/to see if they matched.
Because your
src/wp-content/directory contains several plugins (like thejetpack-monorepo) with massivenode_modulesfolders containing hundreds of thousands of files, Grunt would hang for a long time just trying to find all those files to ensure they were excluded.
By changing the pattern to
!{wp-admin,wp-includes}/**/*.min.js, I restricted that exclusion search to only the corewp-adminandwp-includesdirectories. This allows Grunt to completely skip scanning thewp-contentdirectory, which resolves the hang.
Impact
When running npm run build:dev:
Before: 43.032s
After: 9.393s
This is now >4.5 times faster, a >78% reduction in execution time!
Change History (8)
This ticket was mentioned in PR #10809 on WordPress/wordpress-develop by @westonruter.
7 months ago
#1
- Keywords has-patch added
This ticket was mentioned in Slack in #core by westonruter. View the logs.
7 months ago
#4
in reply to: ↑ description
@
7 months ago
Replying to westonruter:
Impact
When running
npm run build:dev:
Before: 43.032s
After: 9.393s
This is now >4.5 times faster, a >78% reduction in execution time!
Go for it 🚀
@westonruter commented on PR #10809:
7 months ago
#5
I asked Gemini if there were any other opportunities to improve performance similarly in Gruntfile.js, and it identified another glob pattern in copy:files. Indeed, when I run npm run build I find it hangs for a long time at:
Running "copy:files" (copy) task
However, with c413206 the time to build is dramatically improved:
Before: 51.102s
After: 13.805s
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
The
uglify:coretask utilizes a negative glob pattern!**/*.min.jsto prevent re-minifying already minified files. In development builds (npm run build:dev), this pattern operates relative to thesrc/directory. Consequently, the glob expansion scans the entiresrc/directory tree, includingwp-content.For environments where
wp-contentcontains deep directory structures—such as plugins withnode_modulesdependencies—this traversal becomes prohibitively slow, causing the build process to hang.This change scopes the exclusion pattern to
!{wp-admin,wp-includes}/**/*.min.js, limiting the file scan to the relevant core directories and preventing unnecessary recursion intowp-content.Trac ticket: https://core.trac.wordpress.org/ticket/64563