async function middleware(createBundle, options, request, response, next) {
	try {

		// If the request path does not match one of our
		// bundle paths then we exit early
		const sourcePath = options.bundles[request.path];
		if (!sourcePath) {
			return next();
		}

		// Get the full save path and source paths
		const fullSourcePath = path.join(options.basePath, sourcePath);
		const fullSavePath = (options.savePath ? path.join(options.savePath, request.path) : null);

		// Get the full bundle path and create the bundle
		let bundleContent;
		try {
			bundleContent = await createBundle({
				options,
				requestPath: request.path,
				sourcePath: fullSourcePath,
				savePath: fullSavePath
			});
			options.log.info(`Bundle "${request.path}" compiled`);
		} catch (bundleError) {
			options.log.error(`Bundle "${request.path}" failed to compile: ${bundleError.stack}`);
			throw bundleError;
		}

		// If there's a save path, we save the bundle content to the file system
		if (fullSavePath) {
			try {
				await writeFile(fullSavePath, bundleContent);
				options.log.info(`Bundle "${request.path}" saved`);
			} catch (saveError) {
				options.log.error(`Bundle "${request.path}" failed to save: ${saveError.stack}`);
				throw saveError;
			}
		}

		// Serve the bundle
		options.log.info(`Bundle "${request.path}" served`);
		return serveBundle(response, request.path, bundleContent);

	} catch (error) {
		return next(error);
	}
}
/**
 * Actual resave middleware
 *
 * @access private
 * @param {Function} createBundle
 *     An function used to generate a bundle.
 * @param {Object} options
 *     Options used to configure the middleware.
 * @param {Object} request
 *     An Express Request object.
 * @param {Object} response
 *     An Express Response object.
 * @param {ExpressMiddlewareCallback} next
 *     A callback function.
 * @returns {Promise<undefined>}
 *     Returns a promise that resolves when the bundling is complete.
 */
Actual resave middleware
@access private
@param {Function} createBundle
An function used to generate a bundle.
@param {Object} options
Options used to configure the middleware.
@param {Object} request
An Express Request object.
@param {Object} response
An Express Response object.
@param {ExpressMiddlewareCallback} next
A callback function.
@returns {Promise}
Returns a promise that resolves when the bundling is complete.
Actual resave middleware @access private @param { Function } createBundle An function used to generate a bundle . @param { Object } options Options used to configure the middleware . @param { Object } request An Express Request object . @param { Object } response An Express Response object . @param { ExpressMiddlewareCallback } next A callback function . @returns { Promise } Returns a promise that resolves when the bundling is complete .
async function middleware(createBundle, options, request, response, next) {
	try {
		const sourcePath = options.bundles[request.path];
		if (!sourcePath) {
			return next();
		}
		const fullSourcePath = path.join(options.basePath, sourcePath);
		const fullSavePath = (options.savePath ? path.join(options.savePath, request.path) : null);
		let bundleContent;
		try {
			bundleContent = await createBundle({
				options,
				requestPath: request.path,
				sourcePath: fullSourcePath,
				savePath: fullSavePath
			});
			options.log.info(`Bundle "${request.path}" compiled`);
		} catch (bundleError) {
			options.log.error(`Bundle "${request.path}" failed to compile: ${bundleError.stack}`);
			throw bundleError;
		}
		if (fullSavePath) {
			try {
				await writeFile(fullSavePath, bundleContent);
				options.log.info(`Bundle "${request.path}" saved`);
			} catch (saveError) {
				options.log.error(`Bundle "${request.path}" failed to save: ${saveError.stack}`);
				throw saveError;
			}
		}
		options.log.info(`Bundle "${request.path}" served`);
		return serveBundle(response, request.path, bundleContent);
	} catch (error) {
		return next(error);
	}
}
async function middleware ( createBundle , options , request , response , next ) { try { const sourcePath = options . bundles [ request . path ] ; if ( ! sourcePath ) { return next ( ) ; } const fullSourcePath = path . join ( options . basePath , sourcePath ) ; const fullSavePath = ( options . savePath ? path . join ( options . savePath , request . path ) : null ) ; let bundleContent ; try { bundleContent = await createBundle ( { options , requestPath : request . path , sourcePath : fullSourcePath , savePath : fullSavePath } ) ; options . log . info ( ` ${ request . path } ` ) ; } catch ( bundleError ) { options . log . error ( ` ${ request . path } ${ bundleError . stack } ` ) ; throw bundleError ; } if ( fullSavePath ) { try { await writeFile ( fullSavePath , bundleContent ) ; options . log . info ( ` ${ request . path } ` ) ; } catch ( saveError ) { options . log . error ( ` ${ request . path } ${ saveError . stack } ` ) ; throw saveError ; } } options . log . info ( ` ${ request . path } ` ) ; return serveBundle ( response , request . path , bundleContent ) ; } catch ( error ) { return next ( error ) ; } }
Actual resave middleware
 @access private
 @param {Function} createBundle
     An function used to generate a bundle.
Actual resave middleware @access private @param { Function } createBundle An function used to generate a bundle .
// If the request path does not match one of our // bundle paths then we exit early // Get the full save path and source paths // Get the full bundle path and create the bundle // If there's a save path, we save the bundle content to the file system // Serve the bundle