I'm looking for a JavaScript preprocessor. Or maybe compiler. Or "transpiler". I don't know what the right word. It's for transforming JavaScript code in the same way that the C preprocessor transforms C code: substitution of macros/placeholders, configurable output, getting the names of functions that code is contained within, and so on.

These are the requirements (some are optional but preferred):

  • It must be able to stringify parameters like in C.
  • It should preferably be able to get the function name from inside something like this (the syntax is something I'm making up for this example):

    this.myFunc = function () { return @@FUNC_NAME@@; // Would substitute in "myFunc" };

  • It should preferably be able to substitute in the name of an object's constructor function inside its methods:

    function F (x) { this.constructorName = @@...@@; // The ellipsis is just a placeholder for some way of traversing up nested structures like these }

  • It should be run using JavaScript code. Babel and gulp do this, so compilation/preprocessing/whatever would be invoked by executing, say, babel.preprocess("path-to-file"). On top of that, build configuration files for these systems are also written in JavaScript. This needlessly complicates a project. I can't make sense of the JavaScript libraries that have files like package.json or bower.js, and I can't tell where the build system code ends and the application code begins.

  • Maybe the "transformers" feature of RollupJS, or a plugin can do this, but I have never used Rollup (but I remember it from "See Also" section of a Webpack tutorial). Rollup is a module bundler, built for a different purpose but maybe you can make it work for you in principle. – knb Jul 24 '19 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.