@require-transpile/babel
For on-the-fly transpiling similar to [@babel/register](https://babeljs.io/docs/en/babel-register/), but without hooking into your global `require`;
Last updated 4 months ago by f0x52 .
GPL-3.0 · Repository · Original npm · Tarball · package.json
$ cnpm install @require-transpile/babel 
SYNC missed versions from official npm registry.

@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:
Donate using Liberapay

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 from node_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 resolve filepath from.
    • withDependencies: default: false, if true returns {mod, dependencies} where dependencies is an array of all transpiled files
    • 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

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

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.1 (July 1, 2020)

  • bump @require-transpile/core, invalidate now returns Set of affected paths

v0.3.0 (????️‍???? June 29, 2020)

  • bump @require-transpile/core, exposing resolve and cache invalidate functions

v0.2.3 (????️‍???? June 16, 2020)

  • add support for @require-transpile/core overrideHook

v0.2.1 (????️‍???? June 2, 2020)

  • bump @require-transpile/core, correctly returns dependencies for cache-hits

v0.2.0 (May 31, 2020)

  • pass new entryFile and withDependency options

Current Tags

  • 0.3.1                                ...           latest (3 months ago)

9 Versions

  • 0.3.1                                ...           3 months ago
  • 0.3.0                                ...           3 months ago
  • 0.2.3                                ...           3 months ago
  • 0.2.2                                ...           4 months ago
  • 0.2.1                                ...           4 months ago
  • 0.2.0                                ...           4 months ago
  • 0.0.3                                ...           4 months ago
  • 0.0.2                                ...           4 months ago
  • 0.0.1                                ...           4 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (6)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |