npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-unifyer

v1.0.7

Published

Babel joiner to one executable script file for NodeJS or web browser

Downloads

17

Readme

Babel-Unifyer

Helps to translate files with Babel.js and unifyer them in a single script file.

The required files are added in the single script, the non-js files are transformed (if the transformation is registered) or read as strings for then execution of Javascript.

Table of Contents

  1. Installation
  2. Simple usage
  3. Configure
    1. Babel
    2. Added configuration
    3. Prepare non-js files
  4. Documentation

Installation

npm install --save-dev babel-unifyer

Simple usage

const vm = require("vm")
const babelUnifyer = require("babel-unifyer")

;(async function () {
  const buffer = await babelUnifyer(__dirname + "/a")
  const script = new vm.Script(buffer)
  const ctx = vm.createContext({ console })
  await script.runInContext(ctx)
})()

Configure

Babel

Second parameter is the Babel.js configurator (below default configuration).

const babelUnifyer = require("babel-unifyer")
babelUnifyer(filename, {
  presets: ["@babel/env"],
  ast: true,
  sourceMap: true,
  comments: false
})

Warn: sourceMap parameter here should be false for disabling, or anything else for enabling it.

Added configuration

In order to help translation of unified scripts like TypeScript, you can set allowedExtensions as an array of filename extensions allowed. Default value is [".js"].

const babelUnifyer = require("babel-unifyer")
babelUnifyer(filename, {
  allowedExtensions: [".js", ".jsx", ".mjs"]
})

Prepare non-js files

With this module, you can load files that are not written in JavaScript. The translated contents are then sent to Babel.js for integration in the unified script.

const {setTranslator} = require("babel-unifyer")
setTranslator(".xml", function (contents) {
  // your translation
  return contents
})

For every extensions not listed, you can configure translator "" for default translation.

Documentation

Load module

const { unifyScript, setTranslator } = require("babel-unifyer")
// or
const unifyScript = require("babel-unifyer")
const { setTranslator } = unifyScript

function unifyScript(src, opts)

Function unifyScript translate a script entry src to an only one executable script with its required files.

Parameters:

  • {object} opts the options for the babel translation ;
  • {string} src the entry script path.

Returns: {Promise.<{vm.Script} script>.<{Error} err>} reject an error if :

  • a file required is not reachable ;
  • there is an error at compilation.

function setTranslator(extname, fn)

Set a translator function fn for every files with extension name extname. Default extension name is "".

Parameters:

  • {string} extname with dot ;
  • {function} fn({string} contents, {string} filename, {object} opts) : {string|Buffer|Promise.<{string|Buffer} contents>} translator.