Socket
Socket
Sign inDemoInstall

es6-module-crosspiler

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es6-module-crosspiler - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

LICENSE

18

lib/common.js

@@ -10,2 +10,6 @@

/**
* Return a list of `require()`s.
*/
memo(Module.prototype, 'requires', function () {

@@ -30,3 +34,6 @@ var requires = []

// look for module.exports or exports.xxxx
/**
* Look for `module.exports = ` or `exports[x] = `
*/
memo(Module.prototype, 'hasCommonExports', function () {

@@ -48,2 +55,7 @@ var hasCommonExports = false

/**
* Skip function traversals if a `require()` function is defined
* somewhere, because then we're not trying to use a CommonJS require.
*/
function skipFunctionTraversal(path) {

@@ -59,2 +71,6 @@ // `require` is defined somewhere

/**
* Check whether a node is a `require()` expression.
*/
function isRequireExpression(path) {

@@ -61,0 +77,0 @@ // only global `require`

14

lib/exports.js

@@ -29,3 +29,3 @@

var id
if (n.FunctionExpression.check(declaration) && declaration.id) {
if (n.FunctionDeclaration.check(declaration) && declaration.id) {
id = node._varname = declaration.id.name

@@ -39,3 +39,3 @@ } else {

if (node.specifiers) {
if (node.specifiers.length) {
return node.specifiers.forEach(function (specifier) {

@@ -79,2 +79,7 @@ var id = specifier.id.name

/**
* Remove all the `export` statements after
* converting them to CommonJS.
*/
Module.prototype.removeExports = function () {

@@ -86,4 +91,3 @@ types.visit(this.ast.program, {

// remove `export default`s
if (n.FunctionExpression.check(declaration) && declaration.id) {
declaration.type = 'FunctionDeclaration'
if (n.FunctionDeclaration.check(declaration) && declaration.id) {
path.replace(declaration)

@@ -98,3 +102,3 @@ } else {

}
} else if (path.node.specifiers) {
} else if (path.node.specifiers.length) {
// remove `export { x, y }`s

@@ -101,0 +105,0 @@ path.replace()

@@ -5,2 +5,3 @@

var recast = require('recast')
var assert = require('assert')

@@ -40,3 +41,3 @@ var types = recast.types

/**
* Build require() statements
* Build `require()` statements
*/

@@ -56,20 +57,28 @@

// or what type of module the dependency is here
if (node.kind === undefined)
return declarations.push(b.expressionStatement(buildRequire(value)))
if (!node.specifiers.length)
return declarations.push(b.expressionStatement(buildRequire(value)))
// import X from 'y' -> var X = require('y')
// this is a special case when the dependency is CJS
// to do: also check if it's renamed
var dep = this.lookup(value) || {}
if (node.kind === 'default' && dep.type === 'commonjs') {
var name = node.specifiers[0].id.name
node.specifiers.forEach(function (specifier) {
// import X from 'y' -> var X = require('y')
// this is a special case when the dependency is CJS
// to do: also check if it's renamed
if (specifier.type === 'ImportDefaultSpecifier' && dep.type === 'commonjs') {
var name = specifier.id.name
return declarations.push(buildRequireDeclaration(name, value))
}
// import * as x from 'y'
if (specifier.type === 'ImportNamespaceSpecifier') {
var name = specifier.id.name
return declarations.push(buildRequireDeclaration(name, value))
}
// named exports, creates a unique variable name
// then exports that as the require
// import { x, y } from 'z' -> var __z = require('z')
// then the `x` and `y`s are done later
var name = node._varname = this.sourceToVariableName(value)
return declarations.push(buildRequireDeclaration(name, value))
}
// named exports, creates a unique variable name
// then exports that as the require
// import { x, y } from 'z' -> var __z = require('z')
// then the `x` and `y`s are done later
var name = node._varname = this.sourceToVariableName(value)
return declarations.push(buildRequireDeclaration(name, value))
}, this)
}, this)

@@ -81,2 +90,6 @@

/**
* var `variable` = require(`value`)
*/
function buildRequireDeclaration(variable, value) {

@@ -107,3 +120,3 @@ return b.variableDeclaration('var', [

node.specifiers.forEach(function (specifier) {
var id = node.kind === 'default'
var id = specifier.type === 'ImportDefaultSpecifier'
? 'default'

@@ -121,2 +134,3 @@ : specifier.id.name

// rewrite lookups
types.visit(this.ast.program, {

@@ -144,2 +158,6 @@ visitIdentifier: function (path) {

/**
* Remove all `import` statements after converting them to `require()`s
*/
Module.prototype.removeImports = function () {

@@ -146,0 +164,0 @@ var body = this.ast.program.body

{
"name": "es6-module-crosspiler",
"description": "An ES6 and CommonJS cross-compatible transpiler",
"version": "1.0.1",
"version": "2.0.0",
"author": {

@@ -14,5 +14,6 @@ "name": "Jonathan Ong",

"dependencies": {
"ast-util": "0",
"debug": "*",
"esprima-fb": "*",
"memorizer": "1",
"ast-util": "0",
"recast": "0"

@@ -22,3 +23,3 @@ },

"esprima": "git://github.com/esnext/esprima#harmony-esnext",
"mocha": "1",
"mocha": "2",
"istanbul": "0"

@@ -25,0 +26,0 @@ },

@@ -75,3 +75,3 @@

var recast = require('recast')
var esprima = require('esprima')
var esprima = require('esprima-fb')
var Module = require('es6-module-crosspiler')

@@ -78,0 +78,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc