From caca9cb77b6c4cd3842125d729b8d135961294be Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 16 Dec 2018 23:27:36 +0000 Subject: [PATCH] Build Tools: Add non-minified `@wordpress` scripts to the build output. Props atimmer. Merges [43886] to trunk. See #45156. git-svn-id: https://develop.svn.wordpress.org/trunk@44245 602fd350-edb4-49c9-b593-d223f7449a82 --- Gruntfile.js | 150 +++++++++++++++++++++++++++++++++----- tools/webpack/packages.js | 4 +- webpack.config.js | 6 +- 3 files changed, 138 insertions(+), 22 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index def8ba9f65..57d0b92042 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -12,6 +12,10 @@ module.exports = function(grunt) { autoprefixer = require( 'autoprefixer' ), nodesass = require( 'node-sass' ), phpUnitWatchGroup = grunt.option( 'group' ), + themeFiles = [ + 'wp-content/themes/index.php', + 'wp-content/themes/twenty*/**' + ], buildFiles = [ '*.php', '*.txt', @@ -19,12 +23,10 @@ module.exports = function(grunt) { 'wp-includes/**', // Include everything in wp-includes. 'wp-admin/**', // Include everything in wp-admin. 'wp-content/index.php', - 'wp-content/themes/index.php', - 'wp-content/themes/twenty*/**', 'wp-content/plugins/index.php', 'wp-content/plugins/hello.php', 'wp-content/plugins/akismet/**' - ], + ].concat( themeFiles ), cleanFiles = [], changedFiles = { php: [] @@ -127,6 +129,42 @@ module.exports = function(grunt) { ] } }, + symlink: { + expanded: { + files: [ + { + expand: true, + overwrite: true, + cwd: SOURCE_DIR, + src: [ + 'wp-admin/*', + 'wp-content/uploads/', + 'wp-content/index.php', + 'wp-content/plugins/*', + 'wp-includes/*', + '*.php', + '*.txt', + '*.html', + '!wp-load.php', + '!wp-admin/css', + '!wp-content/themes', + '!wp-includes/css', + '!wp-includes/version.php', // Exclude version.php + '!wp-includes/formatting.php', // Exclude formatting.php + '!wp-includes/embed.php', // Exclude formatting.php + '!index.php', '!wp-admin/index.php', + '!_index.php', '!wp-admin/_index.php' + ], + dest: BUILD_DIR + }, + { + 'build/wp-config-sample.php': ['wp-config-sample.php'], + 'build/index.php': ['src/_index.php'], + 'build/wp-admin/index.php': ['src/wp-admin/_index.php'] + } + ] + } + }, copy: { files: { files: [ @@ -138,6 +176,8 @@ module.exports = function(grunt) { '!js/**', // JavaScript is extracted into separate copy tasks. '!.{svn,git}', // Exclude version control folders. '!wp-includes/version.php', // Exclude version.php + '!wp-admin/css/**/*', // Exclude the CSS + '!wp-includes/css/**/*', // Exclude the CSS '!index.php', '!wp-admin/index.php', '!_index.php', '!wp-admin/_index.php' ] ), @@ -153,6 +193,23 @@ module.exports = function(grunt) { } ] }, + css: { + dot: true, + expand: true, + cwd: SOURCE_DIR, + src: [ + 'wp-admin/**/*.css', + 'wp-includes/**/*.css' + ], + dest: BUILD_DIR + }, + themes: { + dot: true, + expand: true, + cwd: SOURCE_DIR, + src: themeFiles, + dest: BUILD_DIR + }, 'npm-packages': { files: { 'build/wp-includes/js/backbone.js': ['./node_modules/backbone/backbone.js'], @@ -341,6 +398,13 @@ module.exports = function(grunt) { src: SOURCE_DIR + 'wp-includes/version.php', dest: BUILD_DIR + 'wp-includes/version.php' }, + 'php-buildFiles': { + files: { + 'build/wp-includes/formatting.php': ['src/wp-includes/formatting.php'], + 'build/wp-includes/embed.php': ['src/wp-includes/embed.php'], + 'build/wp-load.php': ['src/wp-load.php'], + } + }, dynamic: { dot: true, expand: true, @@ -706,6 +770,7 @@ module.exports = function(grunt) { }, webpack: { prod: webpackConfig( { environment: 'production' } ), + devProdTarget: webpackConfig( { environment: 'development', forceBuildTarget: 'build/wp-includes' } ), dev: webpackConfig( { environment: 'development' } ), watch: webpackConfig( { environment: 'development', watch: true } ) }, @@ -1158,7 +1223,7 @@ module.exports = function(grunt) { grunt.registerTask( 'watch', function() { if ( ! this.args.length || this.args.indexOf( 'webpack' ) > -1 ) { - grunt.task.run( 'build' ); + grunt.task.run( 'build:dev' ); } if ( 'watch:phpunit' === grunt.cli.tasks[ 0 ] || 'undefined' !== typeof grunt.option( 'phpunit' ) ) { @@ -1307,6 +1372,13 @@ module.exports = function(grunt) { } } ); + grunt.registerTask( 'uglify:all', [ + 'uglify:core', + 'uglify:embed', + 'uglify:jqueryui', + 'uglify:imgareaselect' + ] ); + grunt.registerTask( 'copy:js', [ 'copy:npm-packages', 'copy:vendor-js', @@ -1314,11 +1386,20 @@ module.exports = function(grunt) { 'copy:includes-js' ] ); - grunt.registerTask( 'uglify:all', [ - 'uglify:core', - 'uglify:embed', - 'uglify:jqueryui', - 'uglify:imgareaselect' + grunt.registerTask( 'copyOrSymlink', function() { + var task = grunt.option( 'symlink' ) === true ? 'symlink:expanded' : 'copy:files'; + grunt.task.run( task ); + } ); + + grunt.registerTask( 'copy:all', [ + 'copyOrSymlink', + 'copy:php-buildFiles', + 'copy:css', + 'copy:themes', + 'copy:wp-admin-css-compat-rtl', + 'copy:wp-admin-css-compat-min', + 'copy:version', + 'copy:js' ] ); grunt.registerTask( 'build:tinymce', [ @@ -1338,16 +1419,26 @@ module.exports = function(grunt) { 'jsvalidate:build' ] ); - grunt.registerTask( 'copy:all', [ - 'copy:files', - 'copy:wp-admin-css-compat-rtl', - 'copy:wp-admin-css-compat-min', - 'copy:version', - 'copy:js' - ] ); + grunt.registerTask( 'clean-all', function() { + if ( grunt.option( 'symlink' ) === true ) { + // clean all symlinks + try { + var delSymlinks = require('del-symlinks'); - grunt.registerTask( 'build', [ - 'clean:all', + var result = delSymlinks.sync(['./build/**']); + grunt.log.writeln( '>> ' + result.length + ' symlinks cleaned.' ); + } catch ( e ) { + grunt.verbose.error( 'Error:', e.message ); + grunt.fail.warn( "Failed to delete symlinks. If you're on Windows, " + + "running as administrator could resolve this issue."); + } + } + + grunt.task.run( 'clean:all' ); + } ); + + grunt.registerTask( 'build:all', [ + 'clean-all', 'copy:all', 'file_append', 'cssmin:core', @@ -1362,10 +1453,31 @@ module.exports = function(grunt) { 'includes:embed', 'usebanner', 'webpack:prod', - 'webpack:dev', + 'webpack:devProdTarget', 'jsvalidate:build' ] ); + grunt.registerTask( 'build', function() { + grunt.task.run( 'build:all' ); + } ); + + grunt.registerTask( 'build:dev', function() { + try { + // Try creating a symlink. + fs.symlinkSync( './symlink', './symlinktest'); + grunt.option( 'symlink', true ); + // If succeeded, remove it again. + fs.unlinkSync( './symlinktest' ); + } catch( e ) { + grunt.verbose.error( 'Error:', e.message ); + grunt.log.error( "Failed to delete symlinks. Falling back to copying " + + "files instead. If you're on Windows, " + + "running as administrator could resolve this issue."); + } finally { + grunt.task.run( 'build:all' ); + } + } ); + grunt.registerTask( 'prerelease', [ 'format:php:error', 'precommit:php', diff --git a/tools/webpack/packages.js b/tools/webpack/packages.js index 3a9f67e33a..4292795f9f 100644 --- a/tools/webpack/packages.js +++ b/tools/webpack/packages.js @@ -49,10 +49,10 @@ function mapVendorCopies( vendors, buildTarget ) { } ) ); } -module.exports = function( env = { environment: 'production', watch: false } ) { +module.exports = function( env = { environment: 'production', watch: false, forceBuildTarget: false } ) { const mode = env.environment; const suffix = mode === 'production' ? '.min' : ''; - const buildTarget = ( mode === 'production' ? 'build' : 'src' ) + '/wp-includes'; + const buildTarget = env.forceBuildTarget ? env.forceBuildTarget : ( mode === 'production' ? 'build' : 'src' ) + '/wp-includes'; const packages = [ 'api-fetch', diff --git a/webpack.config.js b/webpack.config.js index d389a74afb..1d10eb8a4e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,11 +1,15 @@ const mediaConfig = require( './tools/webpack/media' ); const packagesConfig = require( './tools/webpack/packages' ); -module.exports = function( env = { environment: "production", watch: false } ) { +module.exports = function( env = { environment: "production", watch: false, forceBuildTarget: false } ) { if ( ! env.watch ) { env.watch = false; } + if ( ! env.forceBuildTarget ) { + env.forceBuildTarget = false; + } + const config = [ mediaConfig( env ), packagesConfig( env ),