OverZealous / cdnizer

Node module for replacing local links with CDN links, includes fallbacks and customization
MIT License
52 stars 24 forks source link

relativeRoot doesn't resolve the path, just prepends it? #13

Closed snoepkast closed 9 years ago

snoepkast commented 9 years ago

File tree:

frontend/
    app/
    vendor/
        some/
            folder/
                my.css
                images/
                    image.png

My.css:

.some-class {
    background-image: url('./images/image.png');
}

Task:

gulp.task('vendor-css', [], function() {
    return gulp.src(config.vendor.css.src)
        .pipe(cdnizer({
            defaultCDNBase: config.cdn.defaultBase,
            files: ['**/*.{gif,png,jpg,jpeg,svg,ico}'],
            relativeRoot: 'frontend/'  // Also tried it with 'vendor/'
        }))
        .pipe(gulp.dest(config.vendor.buildDir))
});

Result:

.some-class {
    background-image: url('myCDN/frontend/images/image.png');
}

Expected result:

.some-class {
    background-image: url('myCDN/frontend/vendor/some/folder/images/image.png');
}

Reading the docs I would expect the path to be absolute after being cdnized, but perhaps the fault lies with me again. See anything wrong with my setup or is this really a bug?

PS. I was referred here by the maintainer of gulp-cdnizer.

OverZealous commented 9 years ago

I wrote you an answer on the other one. I didn't say copy it here, I said to make future issues for cdnizer here. I maintain both projects.

Please take the time to read the answers people provide on FOSS projects. I don't have time to provide more detail. The code is working as expected, as documented.

snoepkast commented 9 years ago

No it doesn't, the docs say:

This relative path will be appended to the file path **and the path resolved** to remove relative URLs. 

Which it doesn't do, it resolves nothing it just prepends the string I give to it instead of using it as a root.

Considering external css etc. etc. you just cannot assume all your css will be in the same folder and so just prepending the root doesn't solve anything. Plus on a production environment you want to minimize the number of requests by bundling your static, which means you can't keep the same folder structure as the original source.

I read your answer, it just didn't answer my question and said to go here for this type of issue.

But I guess this answers it then, works as intended, it just doesn't work how I want it to work (and how other css url rebasers work when given a root folder). I was just hoping I could prevent using multiple packages for the same thing since those packages are mostly css only.

OverZealous commented 9 years ago

What do you think resolving means? Your path is ./images/image.png. If you prepend anything to this path, then resolve it, you end up with simply prepending the path:

'/styles/' + './images/image.png' -> '/styles/./images/image.png` -> '/styles/images/image.png'

If you had a relative path that didn't point to the same directory, it would make more sense:

'/styles/' + '../images/image.png' -> '/styles/../images/image.png` -> '/images/image.png'

That's what the relative root means. It's a way to calculate the relative root between the CSS or HTML file and the referenced items.

If you want to slap a prefix path on your file references, then you can specify that within the cdn property, rather than relying on the utility to guess what you mean.

snoepkast commented 9 years ago

I think resolving means that the lib resolves the relative path from the given root folder. Because when I minify and bundle my css every relative reference inside the css is wrong. Basically this use case.

When /my/app/some/folder/thingy.png gets referenced inside some css as ./thingy.png and I bundle that css along with some other css in /styles.css the whole path sould be mycdn.com/my/app/some/folder/thingy.png.

I couldn't think of a single use case where my file stream would not be divided over folders or in the end not be bundled, so combined with the same keywords (root) I think I mixed things up. Sorry if I offended you in any way, but is there a way cdnize can do what I want?