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

Rewrite of JavaScript URL's #106

Open
niftylettuce opened this issue Aug 17, 2015 · 9 comments
Open

Rewrite of JavaScript URL's #106

niftylettuce opened this issue Aug 17, 2015 · 9 comments

Comments

@niftylettuce
Copy link

e.g. AJAX request, this URL gets rewritten by a file with the same name as the path of the request

before:

    $.ajax({
      type: 'PUT',
      url: '/checkout',
      dataType: 'json',
      data: $('#checkout-form').serialize()
    }).done(checkoutRefresh).fail(checkoutFailure)

after:

    $.ajax({
      type: 'PUT',
      url: '/checkout.f80cb6c7',
      dataType: 'json',
      data: $('#checkout-form').serialize()
    }).done(checkoutRefresh).fail(checkoutFailure)

(and ofc /checkout.js exists so that's why it conflicts)

@niftylettuce
Copy link
Author

@smysnk any advice here?

@circlingthesun
Copy link
Contributor

@niftylettuce, the API leaves a bit to be desired but you can use the annotator and replacer options. The first lets you split up your sources into annotated fragments. In the replacer function you can choose to not replace a reference for a particular fragment. See https://github.com/smysnk/gulp-rev-all#annotater--replacer

@niftylettuce
Copy link
Author

@circlingthesun Can you provide an example of how I'd prevent a file from replacing references to itself? Shouldn't this be standard? Why do JavaScript URL references get replaced that don't even have extensions?

@niftylettuce
Copy link
Author

(e.g. I resolved this issue by appending a trailing / forward slash to all my URL's in JavaScript files)

@circlingthesun
Copy link
Contributor

CommonJS and AMD allow one to reference modules without an extension. So that's why extensionless references are targeted. You should be able to avoid self references with something like this:

options.annotator = function(contents, path) {
    var fragments = [{'contents': contents, 'path': path}];
    return fragments;
};

options.replacer = function(fragment, replaceRegExp, newReference, referencedFile) {
    if(fragment.path === referencedFile.revPathOriginal){
        return;
    }
    fragment.contents = fragment.contents.replace(replaceRegExp, '$1' + newReference + '$3$4');
};

or avoid extensionless replacements like so:

options.annotator = function(contents, path) {
    var fragments = [{'contents': contents}];
    return fragments;
};

options.replacer = function(fragment, replaceRegExp, newReference, referencedFile) {
    if(referencedFile.revFilenameExtOriginal === '.js' && !newReference.match(/\.js$/)){
        return;
    }
    fragment.contents = fragment.contents.replace(replaceRegExp, '$1' + newReference + '$3$4');
};

@sixinli
Copy link

sixinli commented Nov 3, 2015

hi @circlingthesun,

thanks a lot for posting the options! I have been trying to avoid extensionless replacements, and it seems like newReference.match(/\.js$/) is always true (newReference never has an extension). I am wondering am I doing something wrong in here?

Thanks,
Si

@niftylettuce
Copy link
Author

@circlingthesun I think you should prevent a file from replacing instances of references to itself as a base for this project, otherwise you need to make it clear in the documentation to append trailing forward slashes. This is a very common scenario and it is a definite bug. Please consider my request to publish a patch, thank you!

@LionelMarbot
Copy link

+1

@IvinWu
Copy link

IvinWu commented Jul 29, 2016

@sixinli try this:

options.replacer = function(fragment, replaceRegExp, newReference, referencedFile) {
    if(referencedFile.revFilenameExtOriginal === '.js' && !replaceRegExp.toString().match(/\.js/)){
        return;
    }
    fragment.contents = fragment.contents.replace(replaceRegExp, '$1' + newReference + '$3$4');
};

because !newReference.match(/\.js$/) is always true, i try to test the replaceRegExp to see if .js is exisit and it works.

@circlingthesun can you post a officail code to solve this problem?

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

5 participants