Fuse-box: Custom language plugin documentation

Created on 16 Mar 2020  路  3Comments  路  Source: fuse-box/fuse-box

The example for a custom language plugin
https://github.com/fuse-box/fuse-box/blob/master/playground/custom_lang/pluginCustomLang.ts
does not work anymore on 4.0.0-next.411

The documentation
https://github.com/fuse-box/fuse-box/blob/master/docs/development/plugins.md
refers to assemble_before_analysis which has been removed

Here is the code I was using:

// plugin_nearley.js
const nearley = require("nearley/lib/nearley.js");
const compile = require("nearley/lib/compile.js");
const generate = require("nearley/lib/generate.js");
const rawGrammar = require("nearley/lib/nearley-language-bootstrapped.js");
const { parsePluginOptions } = require("fuse-box/plugins/pluginUtils");

function pluginNearleyHandler(module, opts) {
    // Parse the grammar source into an AST
    const nearleyGrammar = nearley.Grammar.fromCompiled(rawGrammar);
    const grammarParser = new nearley.Parser(nearleyGrammar);
    grammarParser.feed(module.contents);
    const grammarAst = grammarParser.results[0];

    // Compile the AST into a set of rules
    const grammarInfoObject = compile(grammarAst, { ...opts, args: [module.props.absPath] });
    // Generate JavaScript code from the rules
    const grammarJs = generate(grammarInfoObject, "grammar");

    // Replace contents with compiled.
    module.contents = grammarJs;
}

function pluginNearley(a, b) {
    return ctx => {
        ctx.ict.on("assemble_module_init", props => {
            if (props.module.props.extension === ".ne") {
                // making module executable so fusebox will take it and parse all dependencies later on
                props.module.makeExecutable();
            }
            return props;
        });
        ctx.ict.on("assemble_before_analysis", props => {
            const { module } = props;
            if (module.props.extension === ".ne") {
                const [opts] = parsePluginOptions(a, b, ctx.config.json);
                pluginNearleyHandler(module, opts);
            }
            return props;
        });
    };
}

module.exports = { pluginNearleyHandler, pluginNearley };

Is this still supported? How can I implement my plugin properly?
Thank you

4.0.0 question

Most helpful comment

I managed to make my plugin work on 4.0.0-alpha.329.
However, it would be useful to update the documentation for this use case.
Here is the code:

// plugin_nearley.js
const nearley = require("nearley/lib/nearley.js");
const compile = require("nearley/lib/compile.js");
const generate = require("nearley/lib/generate.js");
const rawGrammar = require("nearley/lib/nearley-language-bootstrapped.js");
const { parsePluginOptions } = require("fuse-box/plugins/pluginUtils");

function pluginNearleyHandler(module, opts) {
    module.ctx.log.info("pluginNearley", "compiling $file", {
        file: module.absPath,
    });
    module.read();

    // Parse the grammar source into an AST
    const nearleyGrammar = nearley.Grammar.fromCompiled(rawGrammar);
    const grammarParser = new nearley.Parser(nearleyGrammar);
    grammarParser.feed(module.contents);
    const grammarAst = grammarParser.results[0];

    // Compile the AST into a set of rules
    const grammarInfoObject = compile(grammarAst, { ...opts, args: [module.absPath] });
    // Generate JavaScript code from the rules
    const grammarJs = generate(grammarInfoObject, "grammar");

    // Replace contents with compiled.
    module.contents = grammarJs;
    module.isExecutable = true;
    module.isJavaScript = true;
}

function pluginNearley(a, b) {
    return ctx => {
        const [opts, matcher] = parsePluginOptions(a, b, {});
        ctx.ict.on("module_init", props => {
            const { module } = props;
            if (!module.captured) {
                if (matcher && !matcher.test(module.absPath)) return;
                module.captured = true;
                pluginNearleyHandler(module, opts);
            }
            return props;
        });
    };
}

module.exports = { pluginNearleyHandler, pluginNearley };

All 3 comments

I managed to make my plugin work on 4.0.0-alpha.329.
However, it would be useful to update the documentation for this use case.
Here is the code:

// plugin_nearley.js
const nearley = require("nearley/lib/nearley.js");
const compile = require("nearley/lib/compile.js");
const generate = require("nearley/lib/generate.js");
const rawGrammar = require("nearley/lib/nearley-language-bootstrapped.js");
const { parsePluginOptions } = require("fuse-box/plugins/pluginUtils");

function pluginNearleyHandler(module, opts) {
    module.ctx.log.info("pluginNearley", "compiling $file", {
        file: module.absPath,
    });
    module.read();

    // Parse the grammar source into an AST
    const nearleyGrammar = nearley.Grammar.fromCompiled(rawGrammar);
    const grammarParser = new nearley.Parser(nearleyGrammar);
    grammarParser.feed(module.contents);
    const grammarAst = grammarParser.results[0];

    // Compile the AST into a set of rules
    const grammarInfoObject = compile(grammarAst, { ...opts, args: [module.absPath] });
    // Generate JavaScript code from the rules
    const grammarJs = generate(grammarInfoObject, "grammar");

    // Replace contents with compiled.
    module.contents = grammarJs;
    module.isExecutable = true;
    module.isJavaScript = true;
}

function pluginNearley(a, b) {
    return ctx => {
        const [opts, matcher] = parsePluginOptions(a, b, {});
        ctx.ict.on("module_init", props => {
            const { module } = props;
            if (!module.captured) {
                if (matcher && !matcher.test(module.absPath)) return;
                module.captured = true;
                pluginNearleyHandler(module, opts);
            }
            return props;
        });
    };
}

module.exports = { pluginNearleyHandler, pluginNearley };

@ldom66 please use fuse-box@next
The alpha has been merged to master ;-)

Closed due to inactivity.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

twop picture twop  路  4Comments

ma2ciek picture ma2ciek  路  3Comments

freehuntx picture freehuntx  路  3Comments

charles-mathieus-jomedia picture charles-mathieus-jomedia  路  4Comments

viankakrisna picture viankakrisna  路  5Comments