Skip to content
This repository has been archived by the owner on Oct 13, 2018. It is now read-only.

inline plugins not compatible with UI toggles #18

Closed
shakyShane opened this issue Apr 18, 2015 · 0 comments
Closed

inline plugins not compatible with UI toggles #18

shakyShane opened this issue Apr 18, 2015 · 0 comments

Comments

@shakyShane
Copy link
Contributor

For example, say I want to add an inline plugin to BrowserSync for some quick and dirty hacking - using a URL logger as an example, it would be cool if it was possible to create a toggle for each plugin, regardless of whether the plugin has UI code/markup.

var PLUGIN_NAME = "Asset Logger";
var logger = {
    // Name of Plugin
    "plugin:name": PLUGIN_NAME,
    // Interface for BrowserSync
    plugin: function (opts, bs) {

        // Middleware are given a name to be easily accessed/removed
        var mwname = "asset-logger";

        // Add the middleware at run time.
        addMiddleware();

        // Listen to plugins:configure event and add/remove middleware accordingly
        bs.events.on("plugins:configure", function (data) {

            // Don't act on any other plugins
            if (data.name !== PLUGIN_NAME) {
                return;
            }

            // If toggle switch = on
            if (data.active) {
                if (!bs.app.stack.some(function (item) {
                    return item.id === mwname;
                })) {
                    addMiddleware();
                }
            } else {
                // If toggle switch = off
                bs.removeMiddleware(mwname);
            }
        });
        function addMiddleware () {
            bs.addMiddleware("*", function (req, res, next) {
                // our pseudo middleware
                console.log(req.url);
                next();
            }, {override: true, id: mwname});
        }
    }
};

bs = require("browser-sync");
bs.init({
    server: "./app",
    plugins: [logger]
});
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant