1.0.1 • Published 1 year ago

babel-plugin-transform-inline-helpers v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

babel-plugin-transform-inline-helpers

Babel plugin to inline simple static const variables or TypeScript type guard functions that are only one line when a leading comment contains @inline.

This plugin was written to make working on Preact easier. It is not intended to be a full blown inlining solution or anything. There will likely be no support outside of the use case we have for Preact.

Usage

npm install --save-dev babel-plugin-transform-inline-helpers

Configuration in your babel.config.js:

const fs = require("fs");
const path = require("path");

// Path to file with helpers that should be inlined
const inlineFile = path.join(__dirname, "path/to/my-helpers.js");
const inlineCode = fs.readFileSync(inlineFile, "utf-8");

module.exports = function () {
  return {
    plugins: [
      "babel-plugin-transform-inline-helpers",
      {
        inlineCode,
      },
    ],
  };
};

Example

helper.js:

/** @inline */
export const FOO = 2;

/** @inline */
export const addTwo = (x) => x + 2;

/** @inline */
export function addTwo2(x) {
  return x + 2;
}
import { addTwo } from "./helper.js";

const num = 42;
console.log(addTwo(num));

...becomes:

const num = 42;
console.log(num + 2);

License

MIT, see the LICENSE file.