klei / gulp-inject

A javascript, stylesheet and webcomponent injection plugin for Gulp
MIT License
811 stars 93 forks source link

If `file.path` is not absolute, `inject` adds many `../`? #210

Open leewz opened 7 years ago

leewz commented 7 years ago

Seems that inject doesn't like when fileToInject.path isn't absolute. It should be absolute, but some libraries don't always set it properly (such as gulp-rename and gulp-concat).

Should gulp-inject check for and handle the case where .path is not absolute?

joakimbeng commented 7 years ago

The short answer is: no.

You have three option to get rid of those ../../

leewz commented 7 years ago

It is a bug, or at least the propagation of a bug.

Those options won't work, because inject sees the wrong path.

leewz commented 7 years ago

To be more explicit:

The path being injected is not correct in any combination of starting/ending locations. I think it's because I used a relative path for gulp.src's base option, but the gulp docs did the same.

joakimbeng commented 7 years ago

IMO I think the plugins not setting a correct path is the problem and not inject.

But please show me an excerpt of your code and I think we can solve this without changing any of the plugins.

leewz commented 7 years ago

I made a testcase here: https://github.com/hparra/gulp-rename/issues/71#issuecomment-257401362

The line in gulp-rename that makes path relative: https://github.com/hparra/gulp-rename/blob/master/index.js#L54

file.path = Path.join(file.base, path);

There are other plugins that turn relative .base into relative .path, so I figured inject could just resolve .path before it uses it. Currently, inject adds a slash to a relative path instead of resolving .path (and ignores targetFile.base):

var base = opt.relative ? path.dirname(addRootSlash(unixify(targetFile.path))) : addRootSlash(unixify(sourceFile.cwd));

Using an absolute path for .base solves the issue, but relative .base is in an example in gulp's docs. Perhaps Vinyl.js should automatically turn base and path absolute?