babel-plugin-transform-define
Advanced tools
Comparing version 1.3.2 to 2.0.0
@@ -0,1 +1,15 @@ | ||
# Changelog | ||
## 2.0.0 (2019-10-23) | ||
#### Breaking Changes | ||
* Change plugin options to **only** be a real JS object. Removes string configuration path option as now this is all possible with dynamic `.babelrc.js` or `babel.config.js` files. | ||
* Update to `@babel/core` / Babel 7+. | ||
* Update `package.json:engines` to minimum Node 8. | ||
#### Internal | ||
* Lint all `test` code. | ||
## 1.3.2 (2019-10-22) | ||
@@ -2,0 +16,0 @@ |
162
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
const traverse = require("traverse"); | ||
const { get, has, find } = require("lodash"); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
exports.default = function (_ref) { | ||
var t = _ref.types; | ||
return { | ||
visitor: { | ||
// process.env.NODE_ENV; | ||
MemberExpression: function MemberExpression(nodePath, state) { | ||
processNode(getReplacements(state.opts), nodePath, t.valueToNode, memberExpressionComparator); | ||
}, | ||
// const x = { version: VERSION }; | ||
Identifier: function Identifier(nodePath, state) { | ||
processNode(getReplacements(state.opts), nodePath, t.valueToNode, identifierComparator); | ||
}, | ||
// typeof window | ||
UnaryExpression: function UnaryExpression(nodePath, state) { | ||
if (nodePath.node.operator !== "typeof") { | ||
return; | ||
} | ||
var replacements = getReplacements(state.opts); | ||
var keys = Object.keys(replacements); | ||
var typeofValues = {}; | ||
keys.forEach(function (key) { | ||
if (key.substring(0, 7) === "typeof ") { | ||
typeofValues[key.substring(7)] = replacements[key]; | ||
} | ||
}); | ||
processNode(typeofValues, nodePath, t.valueToNode, unaryExpressionComparator); | ||
} | ||
} | ||
}; | ||
}; | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var traverse = require("traverse"); | ||
var _require = require("lodash"), | ||
get = _require.get, | ||
has = _require.has, | ||
find = _require.find; | ||
/** | ||
@@ -69,41 +17,13 @@ * Return an Array of every possible non-cyclic path in the object as a dot separated string sorted | ||
*/ | ||
const getSortedObjectPaths = (obj) => { | ||
if (!obj) { return []; } | ||
var getSortedObjectPaths = exports.getSortedObjectPaths = function getSortedObjectPaths(obj) { | ||
if (!obj) { | ||
return []; | ||
} | ||
return traverse(obj).paths().filter(function (arr) { | ||
return arr.length; | ||
}).map(function (arr) { | ||
return arr.join("."); | ||
}).sort(function (a, b) { | ||
return b.length - a.length; | ||
}); | ||
return traverse(obj) | ||
.paths() | ||
.filter((arr) => arr.length) | ||
.map((arr) => arr.join(".")) | ||
.sort((a, b) => b.length - a.length); | ||
}; | ||
/** | ||
* `babel-plugin-transfor-define` take options of two types: static config and a path to a file that | ||
* can define config in any way a user sees fit. getReplacements takes the options and will either | ||
* return the static config or get the dynamic config from disk | ||
* @param {Object|String} configOptions configuration to parse | ||
* @return {Object} replacement object | ||
*/ | ||
var getReplacements = function getReplacements(configOptions) { | ||
if ((typeof configOptions === "undefined" ? "undefined" : _typeof(configOptions)) === "object") { | ||
return configOptions; | ||
} | ||
try { | ||
var fullPath = path.join(process.cwd(), configOptions); | ||
fs.accessSync(fullPath, fs.F_OK); | ||
return require(fullPath); | ||
} catch (err) { | ||
console.error("The nodePath: " + configOptions + " is not valid."); // eslint-disable-line | ||
throw new Error(err); | ||
} | ||
}; | ||
/** | ||
* Replace a node with a given value. If the replacement results in a BinaryExpression, it will be | ||
@@ -117,7 +37,7 @@ * evaluated. For example, if the result of the replacement is `var x = "production" === "production"` | ||
*/ | ||
var replaceAndEvaluateNode = function replaceAndEvaluateNode(replaceFn, nodePath, replacement) { | ||
const replaceAndEvaluateNode = (replaceFn, nodePath, replacement) => { | ||
nodePath.replaceWith(replaceFn(replacement)); | ||
if (nodePath.parentPath.isBinaryExpression()) { | ||
var result = nodePath.parentPath.evaluate(); | ||
const result = nodePath.parentPath.evaluate(); | ||
@@ -139,7 +59,6 @@ if (result.confident) { | ||
*/ | ||
var processNode = function processNode(replacements, nodePath, replaceFn, comparator) { | ||
// eslint-disable-line | ||
var replacementKey = find(getSortedObjectPaths(replacements), function (value) { | ||
return comparator(nodePath, value); | ||
}); | ||
// eslint-disable-next-line max-params | ||
const processNode = (replacements, nodePath, replaceFn, comparator) => { | ||
const replacementKey = find(getSortedObjectPaths(replacements), | ||
(value) => comparator(nodePath, value)); | ||
if (has(replacements, replacementKey)) { | ||
@@ -150,10 +69,45 @@ replaceAndEvaluateNode(replaceFn, nodePath, get(replacements, replacementKey)); | ||
var memberExpressionComparator = function memberExpressionComparator(nodePath, value) { | ||
return nodePath.matchesPattern(value); | ||
const memberExpressionComparator = (nodePath, value) => nodePath.matchesPattern(value); | ||
const identifierComparator = (nodePath, value) => nodePath.node.name === value; | ||
const unaryExpressionComparator = (nodePath, value) => nodePath.node.argument.name === value; | ||
const TYPEOF_PREFIX = "typeof "; | ||
const plugin = function ({ types: t }) { | ||
return { | ||
visitor: { | ||
// process.env.NODE_ENV; | ||
MemberExpression(nodePath, state) { | ||
processNode(state.opts, nodePath, t.valueToNode, memberExpressionComparator); | ||
}, | ||
// const x = { version: VERSION }; | ||
Identifier(nodePath, state) { | ||
processNode(state.opts, nodePath, t.valueToNode, identifierComparator); | ||
}, | ||
// typeof window | ||
UnaryExpression(nodePath, state) { | ||
if (nodePath.node.operator !== "typeof") { return; } | ||
const { opts } = state; | ||
const keys = Object.keys(opts); | ||
const typeofValues = {}; | ||
keys.forEach((key) => { | ||
if (key.substring(0, TYPEOF_PREFIX.length) === TYPEOF_PREFIX) { | ||
typeofValues[key.substring(TYPEOF_PREFIX.length)] = opts[key]; | ||
} | ||
}); | ||
processNode(typeofValues, nodePath, t.valueToNode, unaryExpressionComparator); | ||
} | ||
} | ||
}; | ||
}; | ||
var identifierComparator = function identifierComparator(nodePath, value) { | ||
return nodePath.node.name === value; | ||
}; | ||
var unaryExpressionComparator = function unaryExpressionComparator(nodePath, value) { | ||
return nodePath.node.argument.name === value; | ||
}; | ||
// Exports. | ||
module.exports = plugin; | ||
module.exports.default = plugin; | ||
module.exports.getSortedObjectPaths = getSortedObjectPaths; |
{ | ||
"name": "babel-plugin-transform-define", | ||
"description": "Babel plugin that replaces member expressions and typeof statements with strings", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"contributors": [ | ||
@@ -21,20 +21,17 @@ "Eric Baer <me@ericbaer.com> (https://github.com/baer)", | ||
"devDependencies": { | ||
"assert-transform": "^1.0.0", | ||
"babel-cli": "^6.6.5", | ||
"babel-core": "^6.13.2", | ||
"babel-eslint": "6.1.2", | ||
"babel-preset-es2015": "^6.6.0", | ||
"eslint": "3.19.0", | ||
"eslint-config-formidable": "3.0.0", | ||
"eslint-plugin-filenames": "1.1.0", | ||
"eslint-plugin-import": "1.13.0", | ||
"mocha": "^5.2.0", | ||
"rimraf": "^2.5.2" | ||
"@babel/cli": "^7.6.4", | ||
"@babel/core": "^7.6.4", | ||
"@babel/preset-env": "^7.6.3", | ||
"babel-eslint": "^10.0.3", | ||
"eslint": "^5.13.0", | ||
"eslint-config-formidable": "^4.0.0", | ||
"eslint-plugin-filenames": "^1.3.2", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"mocha": "^6.2.2" | ||
}, | ||
"main": "lib", | ||
"scripts": { | ||
"preversion": "yarn clean && yarn build && yarn run check", | ||
"clean": "rimraf lib", | ||
"build": "babel src -d lib", | ||
"lint": "eslint src", | ||
"preversion": "yarn run check", | ||
"lint": "eslint .", | ||
"test": "mocha test/index.js", | ||
@@ -44,3 +41,3 @@ "check": "yarn run lint && yarn run test" | ||
"engines": { | ||
"node": ">= 4.x.x" | ||
"node": ">= 8.x.x" | ||
}, | ||
@@ -47,0 +44,0 @@ "license": "MIT", |
@@ -16,3 +16,3 @@ <h1 align="center">babel-plugin-transform-define</h1> | ||
<h4 align="center"> | ||
Compile time code replacement for babel similar to Webpack's <a href='https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin'>DefinePlugin</a> | ||
Compile time code replacement for babel similar to Webpack's <a href='https://webpack.js.org/plugins/define-plugin/'>DefinePlugin</a> | ||
</h4> | ||
@@ -25,3 +25,3 @@ | ||
```shell | ||
npm install babel-plugin-transform-define | ||
$ npm install --save-dev babel-plugin-transform-define | ||
``` | ||
@@ -42,12 +42,18 @@ | ||
**.babelrc** | ||
**.babelrc.js** | ||
```json | ||
{ | ||
"plugins": [ | ||
["transform-define", "./path/to/config/file.js"] | ||
```js | ||
// E.g., any dynamic logic with JS, environment variables, etc. | ||
const overrides = require("./another-path.js"); | ||
module.exports = { | ||
plugins: [ | ||
["transform-define", { | ||
"process.env.NODE_ENV": "production", | ||
"typeof window": "object", | ||
...overrides | ||
}] | ||
] | ||
} | ||
}; | ||
``` | ||
_Note_: Paths are relative to `process.cwd()`` | ||
@@ -54,0 +60,0 @@ ## Reference Documentation |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
10
150
1
0
10914
92