austinpray / asset-builder

assembles and orchestrates your dependencies so you can run them through your asset pipeline.
http://use-asset-builder.austinpray.com
MIT License
180 stars 30 forks source link

Does asset-builder respect Gulp's cwd option? #51

Open cibulka opened 9 years ago

cibulka commented 9 years ago

Hello!

I use Asset-builder with gulp-hub plugin, that allows to run tasks from multiple Gulpfiles specified at project's root.

This is a simplified version of my setup:

|-- project
|-- |-- gulpfile.js                       // Contains gulp-hub setup
|-- |-- /plugin-1
|-- |--  |-- /assets
|-- |--  |-- |-- manifest.json  // Sass task, containing asset-manifest
|-- |--  |-- /dist
|-- |--  |-- gulpfile.js
|-- |-- /plugin-2
|-- |--  |-- /assets
|-- |--  |-- |-- manifest.json  // Sass task, containing asset-manifest
|-- |--  |-- /dist
|-- |--  |-- gulpfile.js

This way, I can run ...

cd project
gulp sass

... to avoid manually CDing to each plugin folder and all the relevant files are watched by Gulp as they supposed to.

This setup works with asset manifest only if I hardcode absolute paths to manifest.json.

// This works
paths: {
  source: "/absolute/path/to/project/plugin-1/assets/",
  dist: "/absolute/path/to/project/plugin-1/dist/"
}

// I would prefer this though
paths: {
  source: "assets/",
  dist: "dist/"
}

I tried to make it work by specifying cwd option to my (e.g.) sass task like this ...

var merged = require('merge-stream'),
      manifest = require('assets-dev/manifest.json'),
      cwd = '/abs/path/to/plugin-1',
      dest = '/abs/path/to/plugin-dest';

     manifest.forEachDependency('css', function(dep) {
          $.gulp.src(dep.globs, { cwd: cwd })
                 .pipe(mySassTask())
                 .pipe(gulp.dest(dest));
     });

... but it doesn't seem to have any effect. Do I do something wrong or is there some another recommended setup for such case?

Thank you very much in advance!

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/26806181-does-asset-builder-respect-gulp-s-cwd-option?utm_campaign=plugin&utm_content=tracker%2F8853550&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F8853550&utm_medium=issues&utm_source=github).
cibulka commented 9 years ago

OK, I have absolute path to plugin-1 etc. stored in another config variable, so for now, I prepend it manually to each dep.globs like this:

manifest.forEachDependency('css', function(dep) {
  for (var i=0; i < dep.globs.length; i++) {
    dep.globs[i] = config.base + dep.globs[i];
  }
  gulp.src(dep.globs).pipe() ... 
}

Still wondering if there is a better way though ...

cibulka commented 9 years ago

Weirdly enough, I've just noticed, that the absolute path to my plugin-1 get prepended by Asset Builder for my Bower files, but not my main files.

console.log(dep.name)
// outputs
[
  /abs-path/to/plugin-1//abs-path/to/plugin-1/bower_components/object-fit/dist/polyfill.object-fit.css,
  /abs-path/to-plugin-1/assets/my-style.scss
]

So I ended up with:

for (var i=0; i < dep.globs.length; i++) {
    if (dep.globs[i].substring(0, config.base.length) != config.base) {
        dep.globs[i] = config.base + dep.globs[i];
    }
}
austinpray commented 9 years ago

Weirdly enough, I've just noticed, that the absolute path to my plugin-1 get prepended by Asset Builder for my Bower files, but not my main files.

So this behavior comes from https://github.com/ck86/main-bower-files. I definitely want to expose the option to either make all of the globs absolute or all of the globs relative.

You are bringing up an interesting point and I definitely want to support your usecase. Perhaps I should add a "cwd" option to the main entry function and the "make relative"/"make absolute" respect it? The default CWD could be whatever directory your gulpfile lives in.

cibulka commented 9 years ago

Sounds cool! I will stay tuned - however my current "fix" works, so no pressure. :)