Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-define

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-define - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

CODE_OF_CONDUCT.md

15

CHANGELOG.md

@@ -0,1 +1,16 @@

## 1.2.0 (2016-08-25)
#### User Facing Changes
* Add the ability define config as a deep object
* Add a Code of Conduct
#### Internal
* Remove release scripts
* Rename `./modules` to `./src`
* Add keywords and contributors to `package.json`
* Remove author from `package.json` in favor of contributors
* Expand test coverage
## 1.1.0 (2016-08-19)

@@ -2,0 +17,0 @@

31

lib/index.js

@@ -51,4 +51,31 @@ "use strict";

var path = require("path");
var traverse = require("traverse");
var get = require("lodash.get");
/**
* Return an Array of every possible non-cyclic path in the object as a dot separated string sorted
* by length.
*
* Example:
* getSortedObjectPaths({ process: { env: { NODE_ENV: "development" } } });
* // => [ "process.env.NODE_ENV", "process.env" "process" ]
*
* @param {Object} obj A plain JavaScript Object
* @return {Array} Sorted list of non-cyclic paths into obj
*/
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 (elem) {
return elem.length;
});
};
/**
* `babel-plugin-transfor-define` take options of two types: static config and a path to a file that

@@ -104,6 +131,6 @@ * can define config in any way a user sees fit. getReplacements takes the options and will either

var getFirstReplacementValueForNode = function getFirstReplacementValueForNode(obj, comparator, nodePath) {
var replacementKey = Object.keys(obj).filter(function (value) {
var replacementKey = getSortedObjectPaths(obj).filter(function (value) {
return comparator(nodePath, value);
}).shift();
return obj[replacementKey] || null;
return get(obj, replacementKey) || null;
};

@@ -110,0 +137,0 @@

25

package.json

@@ -5,4 +5,7 @@ {

"version" : "1.1.0",
"author" : "Michael Jackson",
"version" : "1.2.0",
"contributors": [
"Eric Baer <me@ericbaer.com> (https://github.com/baer)",
"Michael Jackson <mjijackson@gmail.com> (https://github.com/mjackson)"
],

@@ -18,2 +21,4 @@ "homepage" : "https://github.com/FormidableLabs/babel-plugin-transform-define",

"dependencies": {
"lodash.get" : "4.4.2",
"traverse" : "0.6.6"
},

@@ -30,3 +35,2 @@ "devDependencies": {

"eslint" : "2.10.2",
"readline-sync" : "^1.4.1",
"mocha" : "^3.0.2",

@@ -39,6 +43,6 @@ "rimraf" : "^2.5.2"

"scripts": {
"build": "babel ./modules -d lib",
"build": "babel ./src -d lib",
"check": "npm run clean && npm run build && npm run test && npm run lint",
"clean": "rimraf lib/",
"lint": "eslint modules",
"lint": "eslint src",
"prepublish": "npm run clean && npm run build",

@@ -53,3 +57,12 @@ "release": "node ./scripts/release.js",

"license" : "MIT"
"license" : "MIT",
"keywords": [
"babel-plugin",
"babel-transform",
"babel",
"define",
"DefinePlugin",
"webpack"
]
}

@@ -0,5 +1,10 @@

"use strict";
const assertTransform = require("assert-transform");
const babel = require("babel-core");
const path = require("path");
const assert = require("assert");
const babelPluginTransformDefine = require("../lib/index.js");
const getBabelOps = (pluginOps) => {

@@ -9,6 +14,3 @@ return {

"plugins": [
[path.resolve(__dirname, "../lib/index.js"), pluginOps || {
// TODO: get rid of this when you fix the bug that prevents this from running with no config
"test": true
}]
[path.resolve(__dirname, "../lib/index.js"), pluginOps]
]

@@ -25,40 +27,85 @@ };

it("should transform Member Expressions", () => {
const babelOpts = getBabelOps({
"process.env.NODE_ENV": "development"
describe("transformation tests", () => {
describe("Member Expressions", () => {
it("should transform with config defined by String keys", () => {
const babelOpts = getBabelOps({
"process.env.NODE_ENV": "development"
});
return assertTransform(
path.join(__dirname, "./member-expression/actual.js"),
path.join(__dirname, "./member-expression/expected.js"), babelOpts);
});
it("should transform with config defined by an Object", () => {
const babelOpts = getBabelOps({
"process": {
env: {
NODE_ENV: "development"
}
}
});
return assertTransform(
path.join(__dirname, "./member-expression/actual.js"),
path.join(__dirname, "./member-expression/expected.js"), babelOpts);
});
});
return assertTransform(
path.join(__dirname, "./member-expression/actual.js"),
path.join(__dirname, "./member-expression/expected.js"), babelOpts);
});
it("should transform Unary Expressions", () => {
const babelOpts = getBabelOps({
"typeof window": "object"
});
it("should transform Unary Expressions", () => {
const babelOpts = getBabelOps({
"typeof window": "object"
return assertTransform(
path.join(__dirname, "./unary-expression/actual.js"),
path.join(__dirname, "./unary-expression/expected.js"), babelOpts);
});
return assertTransform(
path.join(__dirname, "./unary-expression/actual.js"),
path.join(__dirname, "./unary-expression/expected.js"), babelOpts);
});
it("should transform Identifiers", () => {
const babelOpts = getBabelOps({
"VERSION": "1.0.0",
"PRODUCTION": true
});
it("should transform Identifiers", () => {
const babelOpts = getBabelOps({
"VERSION": "1.0.0",
"PRODUCTION": true
return assertTransform(
path.join(__dirname, "./identifier/actual.js"),
path.join(__dirname, "./identifier/expected.js"), babelOpts);
});
return assertTransform(
path.join(__dirname, "./identifier/actual.js"),
path.join(__dirname, "./identifier/expected.js"), babelOpts);
it("should transform code from config in a file", () => {
const babelOpts = getBabelOps("./test/load-config-file/config.js");
return assertTransform(
path.join(__dirname, "./load-config-file/actual.js"),
path.join(__dirname, "./load-config-file/expected.js"), babelOpts);
});
});
it("should transform code from config in a file", () => {
const babelOpts = getBabelOps("./test/load-config-file/config.js");
return assertTransform(
path.join(__dirname, "./load-config-file/actual.js"),
path.join(__dirname, "./load-config-file/expected.js"), babelOpts);
describe("unit tests", () => {
describe("getSortedObjectPaths", () => {
it("should return an array", () => {
let objectPaths = babelPluginTransformDefine.getSortedObjectPaths(null);
assert(Array.isArray(objectPaths));
objectPaths = babelPluginTransformDefine.getSortedObjectPaths(undefined);
assert(Array.isArray(objectPaths));
objectPaths = babelPluginTransformDefine.getSortedObjectPaths();
assert(Array.isArray(objectPaths));
objectPaths = babelPluginTransformDefine.getSortedObjectPaths({});
assert(Array.isArray(objectPaths));
objectPaths = babelPluginTransformDefine.getSortedObjectPaths({ process: "env" });
assert(Array.isArray(objectPaths));
});
it("should return a complete list of paths", () => {
const obj = { process: { env: { NODE_ENV: "development" } } };
const objectPaths = babelPluginTransformDefine.getSortedObjectPaths(obj);
assert.deepEqual(objectPaths, [ "process.env.NODE_ENV", "process.env", "process" ]);
});
it("should return a list sorted by length", () => {
const obj = { process: { env: { NODE_ENV: "development" } } };
const objectPaths = babelPluginTransformDefine.getSortedObjectPaths(obj);
assert.deepEqual(objectPaths, objectPaths.sort((elem) => elem.length));
});
});
});
});
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