@require-transpile/babel
For on-the-fly transpiling similar to @babel/register, but without hooking into your global require
;
If you want to bring your own transpiler logic, look at @require-transpile/core.
Word of warning:
This module seems to work for me, but as of writing this, I do not have a thorough understanding of nodejs internals. So, here be dragons.
License, donations
GPL-3.0. If you want to support my work, you can:
Example
see example.js
in the repo:
"use strict";
const requireBabel = require("@require-transpile/babel");
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const config = {
presets: [
"@babel/preset-react",
[
"@babel/preset-env",
{
useBuiltIns: "entry",
corejs: "3.0.0"
}
]
],
extensions: [".jsx"]
};
const component = requireBabel("./component", config);
let rendered = ReactDOMServer.renderToStaticMarkup(React.createElement(component));
console.log(rendered); // <div>Hello World</div>
API
requireBabel(filepath, [options])
- filepath: the path to the module you want to load.
-
options: optional object with options for require-transpile, merged with your babel configuration
-
includeNodeModules: default:
false
, apply transpiler to modules loaded fromnode_modules
as well. -
extensions: default:
[]
, additional extensions to search for, as require.resolve defaults to just.js
. -
cache: default:
{}
, object used for module caching, can be reused across babelTranspile calls as long as files don't change. -
entryFile: default:
module.parent.filename
, used as basepath to resolvefilepath
from. -
withDependencies: default:
false
, if true returns{mod, dependencies}
where dependencies is an array of all transpiled files -
overrideScope: default:
{}
, object with variables to inject/override in module scopes -
overrideHook:
function(path) {}
to short-circuit require calls, if it returns a non-null value it is returned instead of normal require logic - Any other keys will be passed to babel, and need to be valid babel options
-
includeNodeModules: default:
requireBabel.resolve(path, [options])
Resolve similarly to require.resolve
, but with custome extensions
- path: the path to resolve
-
options: optional object with:
-
exts: non-default extensions to look for, like
".jsx"
, or an array of strings for more options -
paths: paths to look in, defaults as relative from
__filename
where you are calling this
-
exts: non-default extensions to look for, like
requireBabel.invalidate(cache, fullpath)
Invalidate a cache entry, for livereloading dependencies with surgical precision.
It will delete fullpath
from the cache, and mark all it's parents as invalid
, which will re-execute their code upon require
returns Set of invalidated entries
-
cache: cache Map that contains an entry for the key
fullpath
- fullpath: entry to invalidate
Changelog
v0.3.2 (August 25, 2022)
- add option to inject/override variables into module scope
- update @require-transpile/core to 0.4.2
v0.3.1 (July 1, 2020)
- bump
@require-transpile/core
, invalidate now returns Set of affected paths
🏳️🌈 June 29, 2020)
v0.3.0 (- bump
@require-transpile/core
, exposingresolve
and cacheinvalidate
functions
🏳️🌈 June 16, 2020)
v0.2.3 (- add support for
@require-transpile/core
overrideHook
🏳️🌈 June 2, 2020)
v0.2.1 (- bump
@require-transpile/core
, correctly returns dependencies for cache-hits
v0.2.0 (May 31, 2020)
- pass new entryFile and withDependency options