jgranstrom/sass-extract

Plugin options

Closed this issue · 4 comments

Is it possible to pass options to plugins?

@adamgruber unfortunately there is no way to pass any plugin options through the render or extract api. However you can pass a plugin instance directly in the plugin array instead of a string with the module name. This way you can have your plugin export a factory function with options, and return a plugin interface with the run function.

I'm currently working on the 2.0 release, and I can add an option to pass plugin options through the render/extract api that would be exposed to the plugin as an argument in the run function.

How about the ability to pass an object instead of a string in the plugin array like so:

{
  ...
  plugins: [ { plugin: 'your-plugin-name', options: {...} } ]
  ...
}

And in your plugin

export function run(options) {
  // do stuff with options
}

Being able to pass an object in the plugin array would be perfect. In the meantime I'll try out your other suggestion of passing a plugin instance directly. Thanks!

Creating a plugin factory that accepts options worked perfectly. It would still be nice to allow passing an object with options right in the plugins array but this is an acceptable workaround.

@adamgruber glad to hear it. I just released 2.0.0 which includes an option to pass options to the plugins through the usual methods.

The plugin API is backward compatible so there's no breaking change from before, but you can now also pass an object in order to pass additional options to the plugin like so:

const rendered = sassExtract.renderSync({
  file: './path/to/vars.scss'
}, {
  plugins: [ { plugin: 'sass-extract-js', options: { a: 123 } } ]
  // plugins: [ 'sass-extract-js' ] // still works when not passing options
});

These are passed to the run function in your plugin:

const sassExtractJs = {
  run: (opts) => ({
    postExtract: extractedVariables => transformVars(extractedVariables.global, opts),
  }),
};

Thanks for the feedback!