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

@liradb2000/babel-plugin-transform-globals

v1.0.6

Published

Transform global variables in JavaScript

Downloads

9

Readme

babel-plugin-transform-globals

NPM Version Build Status Support Chat

babel-plugin-transform-globals is a Babel plugin that lets you transform global variables in JavaScript. This can be helpful when working with scripts that make assumptions about what is in the global variable space.

window.addEventListener('click', {
  document.body.appendChild(
    document.createElement('div')
  )
})

/* becomes (with replace: 'browser') */

window.addEventListener('click', {
  window.document.body.appendChild(
    window.document.createElement('div')
  )
})

/* becomes (with import: { './my-dom': { window: 'default', document: 'document' }) */

import window, { document } from './my-dom'

window.addEventListener('click', {
  document.body.appendChild(
    document.createElement('div')
  )
})

Usage

Add babel-plugin-transform-globals to your project:

npm install babel-plugin-transform-globals --save-dev

Add babel-plugin-transform-globals to your Babel configuration:

// babel.config.js
module.exports = {
  plugins: [
    'transform-globals'
  ]
}

Alternative, configure transformations within your Babel configuration:

module.exports = {
  plugins: [
    ['transform-globals', {

      /* replace global variables */

      replace: {
        // transform global `varA` into `replaceVarA`
        varA: 'replaceVarA',
        // transform global `varB` into `nest.replaceVarB`
        varB: 'nest.replaceVarB'
      },

      /* import global variables (ES Modules) */

      import: {
        'module-import-a': {
          // when `importVarA` is global,
          // write `import importVarA = from 'module-import-a'`
          importVarA: 'default'
        },
        'module-import-b': {
          // when `importVarB` is global,
          // write `import { altName as importVarB } from 'module-import-b'`
          importVarB: 'altName'
        },
        'module-import-c': {
          // when `importVarC` and `importVarD` are global,
          // write `import importVarC, { altName as importVarD } from 'module-import-c'`
          importVarC: 'default',
          importVarD: 'altName'
        }
      },

      /* require global variables (CommonJS) */

      require: {
        'module-require-a': {
          // when `requireVarA` is global,
          // write `const requireVarA = require('module-require-a')`
          requireVarA: 'default'
        },
        'module-require-b': {
          // when `requireVarB` is global,
          // write `const { altName: requireVarB } = require('module-require-b')`
          requireVarB: 'altName'
        },
        'module-require-c': {
          // when `requireVarC` and `requireVarD` are global,
          // write `const requireVarC = require('module-require-c')` and
          // write `const { altName: requireVarB } = requireVarC`
          requireVarC: 'default',
          requireVarD: 'altName'
        }
      }
    }]
  ]
}

Options

replace

The replace option defines an object of global variable names and the variables that will replace them.

/* would transform `window.addEventListener` into `__window.addEventListener` */
/* would transform `document.createElement` into `__window.document` */
{
  replace: {
    'document': 'window.document',
    'window': '__window'
  }
}

The replace option accepts the keyword browsers to automatically prefix global browser variables with window.

/* would transform `document` into `window.document` */
/* would transform `HTMLElement` into `window.HTMLElement` */
{
  replace: 'browser'
}

import

The import option defines an object of modules conditionally imported when one of their global variable names are referenced.

/* on `window.addEventListener` prefix `import window from './dom'` */
/* on `document.createElement` prefix `import { document } from './dom'` */
/* on `Node.prototype` prefix `import { __Node as Node } from './dom'` */
/* on all prefix `import window, { document, __Node as Node } from './dom'` */
{
  import: {
    './dom': {
      'document': 'document',
      'Node': '__Node',
      'window': 'default'
    }
  }
}

require

The require option defines an object of modules conditionally required when one of their global variable names are referenced.

/* on `window.addEventListener` prefix `const window = require('./dom')` */
/* on `document.createElement` prefix `const { document } = require('./dom')` */
/* on `Node.prototype` prefix `const { __Node: Node } = require('./dom')` */
/* on all prefix `const window = require('./dom'); const { document, __Node: Node } = window` */
{
  require: {
    './dom': {
      'document': 'document',
      'Node': '__Node',
      'window': 'default'
    }
  }
}