klei/gulp-inject

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

leewz opened this issue · 5 comments

leewz commented

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?

leewz commented

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

To be more explicit:

  • Source: ./src/css/main.less, ./src/index.html
  • Dest: ./build/dev/css/main.css, ./build/dev/index.html
  • Injected: ../../../../../../../src/css/main.css (six or seven levels)

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.

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

I made a testcase here: hparra/gulp-rename#71 (comment)

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?