Skip to content

Commit

Permalink
Generate and register block manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
oakesjosh committed Nov 22, 2024
1 parent 006054a commit 7aafa14
Show file tree
Hide file tree
Showing 5 changed files with 2,006 additions and 2,263 deletions.
105 changes: 51 additions & 54 deletions gulp/tasks/js.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
// js.js
const { src, dest, parallel } = require('gulp');
const { src, dest, parallel, series } = require('gulp');
const rename = require('gulp-rename');
const minify = require('gulp-babel-minify');
const sass = require('sass');
const through2 = require('through2');
const config = require('../config');

// Process JavaScript files
// Process JS files
function processFiles() {
return src(['src/assets/js/*.js'])
.pipe(
rename((path) => {
path.extname = '.min.js';
})
)
.pipe(minify(config.minify))
.pipe(dest(config.dirs.dist + '/js'));
}
return new Promise((resolve, reject) => {
const stream = src(['src/assets/js/*.js'])
.pipe(
rename(function (path) {
return {
dirname: path.dirname,
basename: path.basename,
extname: '.min.js',
};
})
)
.pipe(minify(config.minify))
.pipe(dest(config.dirs.dist + '/js'));

// Process vendor JavaScript files
function copyVendorFiles() {
return src(['src/assets/js/vendor/*.js'])
.pipe(
rename((path) => {
if (!path.basename.endsWith('.min')) {
path.extname = '.min.js';
}
})
)
.pipe(minify(config.minify))
.pipe(dest(config.dirs.dist + '/js'));
stream.on('end', resolve);
stream.on('error', reject);
});
}

// Process SCSS files with modern Sass API
function styles() {
return src('src/assets/scss/**/*.scss')
.pipe(
through2.obj(function (file, _, callback) {
if (file.isBuffer()) {
try {
// Using the modern Sass API
const result = sass.compile(file.path, {
style: 'compressed',
});
file.contents = Buffer.from(result.css);
file.extname = '.css';
this.push(file);
} catch (err) {
this.emit('error', err);
// Copy vendor files
function copyVendorFiles() {
return new Promise((resolve, reject) => {
const stream = src(['src/assets/js/vendor/*.js'])
.pipe(
rename(function (path) {
if (path.basename.endsWith('.min')) {
return path;
}
}
callback();
})
)
.pipe(dest(config.dirs.dist + '/css'));

return {
dirname: path.dirname,
basename: path.basename,
extname: '.min.js',
};
})
)
.pipe(minify(config.minify))
.pipe(dest(config.dirs.dist + '/js'));

stream.on('end', resolve);
stream.on('error', reject);
});
}

// Parallel tasks for JavaScript
function miscJs() {
return parallel(processFiles, copyVendorFiles)();
// Combined JS task
function miscJs(cb) {
return parallel(processFiles, copyVendorFiles)(cb);
}

// Export tasks
exports.processFiles = processFiles;
exports.copyVendorFiles = copyVendorFiles;
exports.miscJs = miscJs;
exports.styles = styles;
exports.buildJs = parallel(miscJs);
exports.js = parallel(miscJs);
exports.default = parallel(styles, miscJs);
exports.buildJs = miscJs;
exports.js = miscJs;

// Default task
exports.default = series(miscJs);
14 changes: 14 additions & 0 deletions kadence-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ function kadence_blocks_init(): void {
*/
require_once KADENCE_BLOCKS_PATH . 'includes/settings/class-kadence-blocks-site-health.php';

/**
* Register the block metadata manifest.
* Introduced in WP 6.7 to improve performance of block registration.
*/
if (
function_exists('wp_register_block_metadata_collection')
&& file_exists(KADENCE_BLOCKS_PATH . 'dist/blocks/blocks-manifest.php')
) {
wp_register_block_metadata_collection(
KADENCE_BLOCKS_PATH . 'dist/blocks/',
KADENCE_BLOCKS_PATH . 'dist/blocks/manifest.php'
);
}

/**
* Telemetry.
*/
Expand Down
Loading

0 comments on commit 7aafa14

Please sign in to comment.