Sometimes it can be very useful to have magical constants like __FILENAME__ or __LINE__ available within your sourcecode – especially for debugging or in merged files.

Unfortunately, such feature is missing in javascript but it is possible to implement it by yourself using a file-postprocessing filter in your gulp build script.

Thanks to gulp-concat-util, it’s very simple to define such filter which will be applied to each file during the concat task.

The Filter Function#

// gulp-concat-util provides additional functionalities compared to gulp-concat!
var concat = require("gulp-concat-util");

// default release build
gulp.task('browser-js', ['jsx'], function () {
    return gulp.src(['Source/Lib/**/*.js'])

        // concat all files
        .pipe(concat('EnlighterJS.browser.js', {
            // replace filename constants
            process: function (src, filePath) {
                // magic __FILENAME__ constant
                src = src.replace('__FILENAME__', function (match, offset) {
                    return "'" + path.basename(filePath) + "'"

                // magic __LINE__ constant
                src = src.replace('__LINE__', function (match, offset) {
                    // count the number of line-breaks
                    return (src.substr(0, offset).match(/\n/g) || []).length + 1;

                return src;

Usage Example#

Within your sourcefiles, these magical constants will become available

// Throw an Internal Error with the current filename and linenumber
// These informations will also be available in clear-text after concat and minimize task!
throw new InternalError('Error Message', __FILENAME__, __LINE__);