Blame docs/plugins.md

Packit bfcc33
Plugins are shared object files (.so on *nix and .dll on win) that can be loaded by LibSass on runtime. Currently we only provide a way to load internal/custom functions from plugins. In the future we probably will also add a way to provide custom importers via plugins (needs more refactoring to [support multiple importers with some kind of priority system](https://github.com/sass/libsass/issues/962)).
Packit bfcc33
Packit bfcc33
## plugin.cpp
Packit bfcc33
Packit bfcc33
```C++
Packit bfcc33
#include <cstring>
Packit bfcc33
#include <iostream>
Packit bfcc33
#include <stdint.h>
Packit bfcc33
#include "sass_values.h"
Packit bfcc33
Packit bfcc33
union Sass_Value* ADDCALL call_fn_foo(const union Sass_Value* s_args, void* cookie)
Packit bfcc33
{
Packit bfcc33
  // we actually abuse the void* to store an "int"
Packit bfcc33
  return sass_make_number((intptr_t)cookie, "px");
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
extern "C" const char* ADDCALL libsass_get_version() {
Packit bfcc33
  return libsass_version();
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
extern "C" Sass_C_Function_List ADDCALL libsass_load_functions()
Packit bfcc33
{
Packit bfcc33
  // allocate a custom function caller
Packit bfcc33
  Sass_C_Function_Callback fn_foo =
Packit bfcc33
    sass_make_function("foo()", call_fn_foo, (void*)42);
Packit bfcc33
  // create list of all custom functions
Packit bfcc33
  Sass_C_Function_List fn_list = sass_make_function_list(1);
Packit bfcc33
  // put the only function in this plugin to the list
Packit bfcc33
  sass_function_set_list_entry(fn_list, 0, fn_foo);
Packit bfcc33
  // return the list
Packit bfcc33
  return fn_list;
Packit bfcc33
}
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
To compile the plugin you need to have LibSass already built as a shared library (to link against it). The commands below expect the shared library in the `lib` sub-directory (`-Llib`). The plugin and the main LibSass process should "consume" the same shared LibSass library on runtime. It will propably also work if they use different LibSass versions. In this case we check if the major versions are compatible (i.e. 3.1.3 and 3.1.1 would be considered compatible).
Packit bfcc33
Packit bfcc33
## Compile with gcc on linux
Packit bfcc33
Packit bfcc33
```bash
Packit bfcc33
g++ -O2 -shared plugin.cpp -o plugin.so -fPIC -Llib -lsass
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
## Compile with mingw on windows
Packit bfcc33
Packit bfcc33
```bash
Packit bfcc33
g++ -O2 -shared plugin.cpp -o plugin.dll -Llib -lsass
Packit bfcc33
```