JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q25208F
  • License MIT

DOM-like wrapper around the Javascript AST

Package Exports

  • eselement

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eselement) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

eselement

npm version Build Status Coverage Status Dependency Status License

DOM-like wrapper around the Javascript AST

Install

$ npm install eselement --save

Example

// amd2cjs.js
// convert AMD modules to CommonJS.
var fs = require("fs");

var eselement = require("eselement");

var content = fs.readFileSync(process.argv[2])
  , program = eselement.createElement(content);

var define = program.querySelector("CallExpression[callee.name='define']");

if (define) {
  var arr = define.arguments[0]
      , fn = define.arguments[1];

  var libs = arr.elements.map(function (e) { return e.value })
    , names = fn.params.map(function (p) { return p.name; })
    , body = fn.body.body;

  define.parentElement.removeChild(define);

  libs.forEach(function (lib, i) {
    var el = names[i] ? eselement.createElement("var " + names[i] + " = require('" + lib + "')")
                        : eselement.createElement("require('" + lib + "')");
    program.appendChild("body", el);
  });

  program.appendChild("body", body);

  console.log(program.outerCode());
}
$ cat amd_module.js
define(["crypto"], function (crypto) {
  var ciphers = crypto.getCiphers();
  console.log(ciphers);
});

$ node amd2cjs.js amd_module.js
var crypto = require('crypto');
var ciphers = crypto.getCiphers();
console.log(ciphers);

API

createElement(string || object)

Creates and element from either Javascript code or Mozilla Parser AST.

Element

Attributes

All attributes from the AST are copied onto the Element, for example an element of type Program will have an attribute body.

Element.parentElement

A reference to the Elements parent, if it has no parent it's null.

Element.parentAttribute

A string denoting the attribute on the parent in which this element is in.

Methods

Element.childElements()

Get all children of an element.

Element.firstChild()

Get the first child of an element.

Element.lastChild()

Get the last child of an element.

Element.querySelectorAll(selector)

Select elements using esquery.

Element.querySelector(selector)

Select the first matching element using esquery.

Element.setAttribute(attribute, value)

Sets the attribute attribute to value. Does checking to see if value is of the correct type for attribute.

Element.appendChild(attribute, child)

Append child to element attribute attribute.

Element.removeChild(child)

Remove child from element.

Element.replaceChild(newChild, oldChild)

Replace oldChild with newChild in element.

Element.cloneElement()

Return a clone of element.

Element.outerAST()

Return the AST for element.

Element.outerCode()

Return the javascript code for element.