shonny-ua / gulp-rev-collector

Static asset revision data collector from manifests, with was generated from different streams and replace they's links in html template.
MIT License
150 stars 41 forks source link

the replacement with a variable #45

Closed tuhongwei closed 6 years ago

tuhongwei commented 6 years ago

Now, I have a demand in my js files. I need to change the img dynamically according to the "status". So the code is "$(element).attr('src', 'img-btn' + status + '.png');". The status is a variable and is varying. In this case, the replacement can't work properly, because can't the img src.

shonny-ua commented 6 years ago

Your manifest file looks like:

{
    "img-btn-ok.png": "img-btn-ok-88553322.png",
    "img-btn-false.png": "img-btn-false-aa5588ff.png",
    "img-btn-read.png": "img-btn-read-bb001199.png"
}

? You need js code like next:

switch(status) {
    case 'ok':
        imgSrc = 'img-btn-ok.png';
        break;
    case 'false':
        imgSrc = 'img-btn-false.png';
        break;
    case 'read':
        imgSrc = 'img-btn-read.png';
        break;
}
$(element).attr('src', imgSrc);

Then gulp-rev-collector replaces names in switch - case block corectly.

tuhongwei commented 6 years ago

yes, you are right,At present, I solve the problem like this. But when the "status" is quite a lot, it is troublesome to list ony by one. So are there other solutions?

tuhongwei commented 6 years ago

Now there is a problem, I use "require.ensure" to generate a script dynamically,but this script can't be replaced,how this problem be solved?

shonny-ua commented 6 years ago

show me an exsamle of require.ensure usage. I don't undersand the problem.

shonny-ua commented 6 years ago

I close the issue.