How to make options for a gulp plugin?

75 views Asked by At

Context:

I'm trying to make a gulp plugin that uses phantomJS and possibly cheerio to crawl a website, grab sources (images & font files for example) and put them into a folder. The plugin is is based off Stylify-Me.

I've gone over the "documentation" on gulpjs.com but I still don't understand how to go about making basic gulp options and console.log them.

Here is what the plugin should look like:

   .pipe(plugin({
         url: 'http://stylifyme.com/',
         sitemap: true,
         content: all, 
         out: md,
         output: './dist/'
   }))

All I'm asking right now is how to take an option (such as url: 'google.com') and console.log the website url.

I'm using a yeoman generator to create the plugin and here is the code i have for the plugin:

var gutil = require('gulp-util');
var through = require('through2');
var cheerio = require('cheerio'),
var phantom = require('phantom');

var plugin = require('./lib');

module.exports = function (opts) {
    opts = opts || {};

    return through.obj(function (file, enc, cb) {
        if (file.isNull()) {
            cb(null, file);
            return;
        }

        if (file.isStream()) {
            cb(new gutil.PluginError('gulp-stylify', 'Streaming not supported'));
            return;
        }

        try {
            file.contents = new Buffer(someModule(file.contents.toString(), opts));
            this.push(file);
        } catch (err) {
            this.emit('error', new gutil.PluginError('gulp-stylify', err));
        }

        cb();
    });
};
1

There are 1 answers

0
andorx On

Notice this opts = opts || {};. This variable contains your options that were passed when you invoked your plugin. After that statement, you could access your options:

console.log(opts.url);
console.log(opts.sitemap);
...