Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options spread to multiple gulp-assemble tasks due to shared instance #15

Closed
nicluo opened this issue Dec 10, 2014 · 5 comments
Closed

Comments

@nicluo
Copy link

nicluo commented Dec 10, 2014

// Common Config
var assembleCommonOptions = {
  layouts: ['templates/layouts/*.hbs'],
  partials: ['templates/partials/*.hbs'],
};

// Specific Config
var assembleOptions = _.extend({
  layout: 'default',
}, assembleCommonOptions);

var assembleOptionsAdmin = _.extend({
  layout: 'admin',
}, assembleCommonOptions);

// Assemble Task 1
gulp.task('assemble-main', function () {
  return gulp.src('templates/pages/*.hbs')
    .pipe(assemble(assembleOptions))
    .pipe(htmlmin())
    .pipe(gulp.dest('dist/'));
});

// Assemble Task 2
gulp.task('assemble-admin', function () {
  return gulp.src('templates/pages/admin/*.hbs')
    .pipe(assemble(assembleOptionsAdmin))
    .pipe(htmlmin())
    .pipe(gulp.dest('dist/admin/'));
});

I have a site which has multiple assemble tasks.

Running both tasks simultaneously creates problems on the options. In destination directories, files overlap as well (e.g. files from assemble-main may end up in dist/admin depending on the running order).

Previously (0.1.7), I could workaround by using run-sequence, which keeps the options separated long enough for both tasks to work fine.

gulp.task('assemble', function(callback){
  runSequence(
    'assemble-main',
    'assemble-admin',
    callback);
});

This does not work anymore in 0.1.8. I'm essentially trying to make the gulp task idempotent.

While exposing the instance in module.exports helps with configuring, I can't really reload the instance between tasks.

/**
 * Expose instance of assemble for further modification
 */

plugin.instance = assemble;

In the end, I did this as a quick fix:

var plugin = module.exports = function (options) {

  assemble = assemble.createInst();

  var opts = _.extend({}, options);
...

1 - Is there a better way to do this that I missed?
2 - If you need more information/ minimum code to reproduce I'm more than happy to help. Thanks

@doowb
Copy link
Member

doowb commented Dec 10, 2014

@nicluo thanks for pointing this out and showing a good use case. gulp-assemble is still in alpha, so we're still working on how it should interact with assemble itself.

I think creating a new instance of assemble inside each task is a good idea as long as we don't want to expose any other assemble features outside of the plugin. Meaning the only way to interact with the underlying assemble instance is through an options object. Then using more advanced features would require using assemble directly instead of using gulp-assemble.

Another option is exposing more methods to allow using the parent instance or a new instance.

Any suggestions and feedback would be helpful.

@nicluo
Copy link
Author

nicluo commented Dec 12, 2014

I agree that the instance options should be handled within the plugin. We should also work with new instances (at least currently), to support use cases such as mine.

However, with grunt assemble we are able to define multiple src and destination locations within assemble config.

assemble: {
    options: {...},
    site: {...},
    admin: {...},
    docs: {...}
}

Gulp is more involved with the filesystem because it has the src and dest functions, which was why I took advantage by dividing into individual directories. A decision will eventually have to be made as to whether the ideal use case should be a single task for the whole site, or multiple fine-grained tasks.

@jonschlinkert
Copy link
Member

@nicluo have you considered using assemble v0.6.0 directly? I'm not sure if it will make sense for your use case, but if you're using assemble for that kind of configuration, IMHO it's probably worth a look.

@nicluo
Copy link
Author

nicluo commented Dec 13, 2014

@jonschlinkert Thanks for the suggestion, I'll definitely revisit the way I configure assemble.

However, at the moment I'm unable to build my site properly, or rollback to 0.1.7. I had the same issue as assemble/grunt-assemble#9 on gulp with 0.1.7.

Fortunately, I have a laptop with a working version at the moment. Thought you could use a heads up while settling the transition to 0.6.0.

Error: Cannot find module '/dev/test/node_modules/gulp-assemble/node_modules/assemble/node_modules/handlebars-helpers/lib/helpers/dev/test/node_modules/gulp-assemble/node_modules/assemble/node_modules/handlebars-helpers/lib/helpers/code.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Function.addHelpers (/dev/test/node_modules/gulp-assemble/node_modules/assemble/node_modules/handlebars-helpers/lib/index.js:59:20)
    at Array.forEach (native)
    at new Helpers (/dev/test/node_modules/gulp-assemble/node_modules/assemble/node_modules/handlebars-helpers/lib/index.js:46:15)
    at Helpers (/dev/test/node_modules/gulp-assemble/node_modules/assemble/node_modules/handlebars-helpers/lib/index.js:34:12)
    at HandlebarsEngine.init (/dev/test/node_modules/gulp-assemble/node_modules/assemble/lib/engines/handlebars/handlebars.js:57:17)
    at Engine.init (/dev/test/node_modules/gulp-assemble/node_modules/assemble/lib/render.js:33:26)
Macintosh:dev user$

@doowb
Copy link
Member

doowb commented Feb 20, 2015

gulp-assemble 0.2.0 now requires passing in an instance of assemble so you can create new instances for each task. Check out assemble 0.6.0 docs for more information.

@doowb doowb closed this as completed Feb 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants