Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minifier - use the absolute path instead of the relative one for the filepath variable #14

Open
mathieuhays opened this issue Apr 18, 2018 · 0 comments

Comments

@mathieuhays
Copy link

Configuration:

{
    gzip: true,
    minifier: function ( contents, filepath ) {
        if ( filepath.match( /\.min\./g ) ) {
            return contents
        }

        var file_extension = '';

        var minified_filename = filepath.replace( /\.(js|css)$/, function(match, extension ) {
            file_extension = extension;
            return '.min.' + extension;
        } );

        // re-construct a path like: /path/to/assets/js/main.min.js
        var minified_path = path.join( '/path/to/assets/', file_extension, minified_filename );

        if ( fs.existsSync( minified_path ) ) {
            return fs.readFileSync( minified_path );
        }

        return '';
    }
}

Since the filepath only contains the filename I had to hardcode the full path which is not ideal.

To the best of my knowledge I don't think there are other ways to get the full path rather than hardcoding it in this case.

Having access to the full path will prevent us having to hardcode the path on every project.

Optionally we could add the full path as a third argument for backward-compatibility.

Thanks ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant