jsoverson / preprocess

Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration
Other
366 stars 80 forks source link

File not found with @extend throws error, instead of calling the callback #119

Open Ishmael-Turner opened 7 years ago

Ishmael-Turner commented 7 years ago

I have an HTML file with:

<!-- @extend non-existant-file.html -->

I am using Promises to chain various build steps together and calling preprocessFile with a callback inside the Promise constructor. When the code is run an exception is raised for the missing file and I cannot catch it with try...catch or Promise.catch(). This causes the entire script to halt.

As this build task is ran inside a file watcher (BrowserSync/chokidar) it halts the server, rather than giving me a chance to edit and save with automatic re-build. Here's a simplified version of the code, omitting the BrowserSync bit.

function html() {
    return globby( 'app/*.html')
        .then( (htmlFiles) => {
            var ppHtmlPs = htmlFiles.map( (file) => {
                return new Promise( (resolve, reject) => {
                    var destFileName = path.join( 'temp', path.basename(file) );

                    pp.preprocessFile( file, destFileName, {}, (err) => {
                        if (err) {
                            reject(err);
                        } else {
                            resolve();
                        }
                    }, {
                        type: 'html',
                        srcEol: '\r\n',
                        fileNotFoundSilentFail: false
                    } );
                } );
            } );
            return Promise.all( ppHtmlPs );
        } )
        .catch( (reason) => {
            console.error(reason.message);
        } );
}

I could resort to fileNotFoundSilentFail: true and add a Promise.then() handler to check for the error message written into the output. It's not a big issue but a callback for this error would make for more natural code.