Skip to content

Configuration

Eduardo Bouças edited this page Feb 6, 2020 · 1 revision

Sourcebit is driven by a configuration object that typically lives in a file named sourcebit.js. It contains information about all the plugins to be executed, and any options they may receive.

💡 If you're using the Sourcebit npm module in your JavaScript application, you can also supply the configuration object as a parameter to the fetch() method.

Each plugin is defined as an object with two properties:

  • module: The reference to the plugin's Node module (typically the return value of a require() call).
  • options: An optional object with any options that the plugin may expect.
✏️ Example
module.exports = {
  plugins: [
    {
      module: require("sourcebit-source-contentful"),
      options: {
        accessToken: process.env["CONTENTFUL_ACCESS_TOKEN"],
        environment: "master",
        spaceId: "1q2w3e4r"
      }
    },
    {
      module: require("sourcebit-target-jekyll"),
      options: {
        writeFile: function (entry, utils) {
          const { __metadata: meta, ...fields } = entry;

          if (!meta) return;

          const { createdAt = "", modelName, projectId, source } = meta;

          if (
            modelName === "post" &&
            projectId === "1q2w3e4r" &&
            source === "sourcebit-source-contentful"
          ) {
            const {
              __metadata,
              content: content,
              layout,
              ...frontmatterFields
            } = entry;

            return {
              content: {
                body: fields["content"],
                frontmatter: { ...frontmatterFields, layout: fields["layout"] }
              },
              format: "frontmatter-md",
              path:
                "_posts/" +
                createdAt.substring(0, 10) +
                "-" +
                utils.slugify(fields["title"]) +
                ".md"
            };
          }
        }
      }
    }
  ]
};

Inline plugins

The main principle behind Sourcebit is to compose different plugins to achieve the functionality you're after, but sometimes you want to perform simple operations, specific to your project, without going through the process of creating an abstraction and publishing it as a plugin. In such cases, you can use an inline plugin.

If you look at the example configuration above, you'll see that the module property of a plugin object is a reference to a module that results from a require() call. If we create a new plugin object where the value of module is an object with the right properties and methods, we've got ourselves an inline plugin.

✏️ Example of inline plugin
module.exports = {
  plugins: [
    {
      module: require("sourcebit-source-contentful"),
      options: {
        accessToken: process.env["CONTENTFUL_ACCESS_TOKEN"],
        environment: "master",
        spaceId: "1q2w3e4r"
      }
    },
    {
      module: {
        // Our inline plugin is adding {"generatedBy": "Sourcebit"} to all objects.
        transform: ({ data }) => {
          const objects = data.objects.map(object => ({
            ...object,
            generatedBy: 'Sourcebit'
          })):

          return {
            ...data,
            objects
          };
        }
      }
    }
  ]
};

Furthermore, if the inline plugin just makes use of the transform method, there's an alternative syntax you can use. Instead of declaring a plugin object with a module property, you can specify a transform method directly in the plugins array.

✏️ Example of inline plugin (shorthand syntax)
module.exports = {
  plugins: [
    {
      module: require("sourcebit-source-contentful"),
      options: {
        accessToken: process.env["CONTENTFUL_ACCESS_TOKEN"],
        environment: "master",
        spaceId: "1q2w3e4r"
      }
    },

    // Our inline plugin is adding { "generatedBy": "Sourcebit" } to all objects.
    ({ data }) => {
      const objects = data.objects.map(object => ({
        ...object,
        generatedBy: 'Sourcebit'
      })):

      return {
        ...data,
        objects
      };
    }
  ]
};
Clone this wiki locally