pax_global_header00006660000000000000000000000064131065371620014516gustar00rootroot0000000000000052 comment=43e7a1efa32e96aec0c9f408d77e41ae882e527f babel-plugin-transform-define-1.3.0/000077500000000000000000000000001310653716200173215ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/.babelrc000066400000000000000000000000341310653716200207110ustar00rootroot00000000000000{ "presets": ["es2015"] } babel-plugin-transform-define-1.3.0/.eslintrc000066400000000000000000000002671310653716200211520ustar00rootroot00000000000000--- "extends": - "formidable/configurations/es6" rules: no-magic-numbers: OFF max-statements: OFF no-console: OFF max-len: - error - 120 globals: console: false babel-plugin-transform-define-1.3.0/.gitignore000066400000000000000000000000041310653716200213030ustar00rootroot00000000000000lib babel-plugin-transform-define-1.3.0/.travis.yml000066400000000000000000000001531310653716200214310ustar00rootroot00000000000000language: node_js node_js: - "4" - "5" - "6" script: npm run check branches: only: - master babel-plugin-transform-define-1.3.0/CHANGELOG.md000066400000000000000000000020131310653716200211260ustar00rootroot00000000000000## 1.3.0 (2017-05-15) #### User Facing Changes * Support falsy replacement values [https://github.com/FormidableLabs/babel-plugin-transform-define/pull/33] #### Internal * Update eslint config and packages * Update lodash version ## 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) #### User Facing Changes * Add the ability to get config from a file * Add support for Identifiers * Major improvements to the README.md #### Internal * Add tests * Add lint * Add JSDoc * Add CI * Add explicit LICENSE file * Move dependencies into devDependencies * Major refactors to DRY the code ## 1.0.0 (2016-03-21) #### User Facing Changes * Update README.md to reference Webpack's Define Plugin ## 1.0.0 (2016-03-21) Initial Release babel-plugin-transform-define-1.3.0/CODE_OF_CONDUCT.md000066400000000000000000000057651310653716200221350ustar00rootroot00000000000000Contributor Covenant Code of Conduct Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. Our Standards Examples of behavior that contributes to creating a positive environment include: Using welcoming and inclusive language Being respectful of differing viewpoints and experiences Gracefully accepting constructive criticism Focusing on what is best for the community Showing empathy towards other community members Examples of unacceptable behavior by participants include: The use of sexualized language or imagery and unwelcome sexual attention or advances Trolling, insulting/derogatory comments, and personal or political attacks Public or private harassment Publishing others' private information, such as a physical or electronic address, without explicit permission Other conduct which could reasonably be considered inappropriate in a professional setting Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [INSERT EMAIL ADDRESS]. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. Attribution This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4. babel-plugin-transform-define-1.3.0/LICENSE000066400000000000000000000020641310653716200203300ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Formidable Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. babel-plugin-transform-define-1.3.0/README.md000066400000000000000000000043621310653716200206050ustar00rootroot00000000000000

babel-plugin-transform-define

npm version

Compile time code replacement for babel similar to Webpack's DefinePlugin

*** ## Quick Start ```shell npm install babel-plugin-transform-define ``` **.babelrc** ```json { "plugins": [ ["transform-define", { "process.env.NODE_ENV": "production", "typeof window": "object" }] ] } ``` **.babelrc** ```json { "plugins": [ ["transform-define", "./path/to/config/file.js"] ] } ``` _Note_: Paths are relative to `process.cwd()`` ## Reference Documentation `babel-plugin-transform-define` can transform certain types of code as a babel transformation. #####`Identifiers` *.babelrc* ```json { "plugins": [ ["transform-define", { "VERSION": "1.0.0", }] ] } ``` *Source Code* ```js VERSION; window.__MY_COMPANY__ = { version: VERSION }; ``` *Output Code* ```js "1.0.0"; window.__MY_COMPANY__ = { version: "1.0.0" }; ``` *** #####`Member Expressions` *.babelrc* ```json { "plugins": [ ["transform-define", { "process.env.NODE_ENV": "production" }] ] } ``` *Source Code* ```js if (process.env.NODE_ENV === "production") { console.log(true); } ``` *Output Code* ```js if (true) { console.log(true); } ``` *** #####`Unary Expressions` *.babelrc* ```json { "plugins": [ ["transform-define", { "typeof window": "object" }] ] } ``` *Source Code* ```js typeof window; typeof window === "object"; ``` *Output Code* ```js 'object'; true; ``` *** ## License [MIT License](http://opensource.org/licenses/MIT) babel-plugin-transform-define-1.3.0/package.json000066400000000000000000000035461310653716200216170ustar00rootroot00000000000000{ "name" : "babel-plugin-transform-define", "description" : "Babel plugin that replaces member expressions and typeof statements with strings", "version" : "1.3.0", "contributors": [ "Eric Baer (https://github.com/baer)", "Michael Jackson (https://github.com/mjackson)", "Andy Edwards (https://github.com/jedwards1211)" ], "homepage" : "https://github.com/FormidableLabs/babel-plugin-transform-define", "bugs": { "url": "https://github.com/FormidableLabs/babel-plugin-transform-define/issues" }, "repository" : "git://github.com/FormidableLabs/babel-plugin-transform-define.git", "private" : false, "dependencies": { "lodash" : "4.17.4", "traverse" : "0.6.6" }, "devDependencies": { "assert-transform" : "^1.0.0", "babel-core" : "^6.13.2", "babel-cli" : "^6.6.5", "babel-eslint" : "6.1.2", "babel-preset-es2015" : "^6.6.0", "eslint-config-formidable" : "3.0.0", "eslint-plugin-filenames" : "1.1.0", "eslint-plugin-import" : "1.13.0", "eslint" : "3.19.0", "mocha" : "^3.0.2", "rimraf" : "^2.5.2" }, "main" : "lib", "scripts": { "build": "babel ./src -d lib", "check": "npm run clean && npm run build && npm run test && npm run lint", "clean": "rimraf lib/", "lint": "eslint src", "prepublish": "npm run clean && npm run build", "release": "node ./scripts/release.js", "test": "mocha ./test/index.js" }, "engines":{ "node": ">= 4.x.x" }, "license" : "MIT", "keywords": [ "babel-plugin", "babel-transform", "babel", "define", "DefinePlugin", "webpack" ] } babel-plugin-transform-define-1.3.0/src/000077500000000000000000000000001310653716200201105ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/src/index.js000066400000000000000000000105701310653716200215600ustar00rootroot00000000000000const fs = require("fs"); const path = require("path"); const traverse = require("traverse"); const { get, has, find } = require("lodash"); /** * 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 */ export const getSortedObjectPaths = (obj) => { if (!obj) { return []; } return traverse(obj) .paths() .filter((arr) => arr.length) .map((arr) => arr.join(".")) .sort((elem) => elem.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 */ const getReplacements = (configOptions) => { if (typeof configOptions === "object") { return configOptions; } try { const 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 * evaluated. For example, if the result of the replacement is `var x = "production" === "production"` * The evaluation will make a second replacement resulting in `var x = true` * @param {function} replaceFn The function used to replace the node * @param {babelNode} nodePath The node to evaluate * @param {*} replacement The value the node will be replaced with * @return {undefined} */ const replaceAndEvaluateNode = (replaceFn, nodePath, replacement) => { nodePath.replaceWith(replaceFn(replacement)); if (nodePath.parentPath.isBinaryExpression()) { const result = nodePath.parentPath.evaluate(); if (result.confident) { nodePath.parentPath.replaceWith(replaceFn(result.value)); } } }; /** * Finds the first replacement in sorted object paths for replacements that causes comparator * to return true. If one is found, replaces the node with it. * @param {Object} replacements The object to search for replacements * @param {babelNode} nodePath The node to evaluate * @param {function} replaceFn The function used to replace the node * @param {function} comparator The function used to evaluate whether a node matches a value in `replacements` * @return {undefined} */ const processNode = (replacements, nodePath, replaceFn, comparator) => { // eslint-disable-line const replacementKey = find(getSortedObjectPaths(replacements), (value) => comparator(nodePath, value)); if (has(replacements, replacementKey)) { replaceAndEvaluateNode(replaceFn, nodePath, get(replacements, replacementKey)); } }; const memberExpressionComparator = (nodePath, value) => nodePath.matchesPattern(value); const identifierComparator = (nodePath, value) => nodePath.node.name === value; const unaryExpressionComparator = (nodePath, value) => nodePath.node.argument.name === value; export default function ({ types: t }) { return { visitor: { // process.env.NODE_ENV; MemberExpression(nodePath, state) { processNode(getReplacements(state.opts), nodePath, t.valueToNode, memberExpressionComparator); }, // const x = { version: VERSION }; Identifier(nodePath, state) { processNode(getReplacements(state.opts), nodePath, t.valueToNode, identifierComparator); }, // typeof window UnaryExpression(nodePath, state) { if (nodePath.node.operator !== "typeof") { return; } const replacements = getReplacements(state.opts); const keys = Object.keys(replacements); const typeofValues = {}; keys.forEach((key) => { if (key.substring(0, 7) === "typeof ") { typeofValues[key.substring(7)] = replacements[key]; } }); processNode(typeofValues, nodePath, t.valueToNode, unaryExpressionComparator); } } }; } babel-plugin-transform-define-1.3.0/test/000077500000000000000000000000001310653716200203005ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/.eslintrc000066400000000000000000000001631310653716200221240ustar00rootroot00000000000000--- "extends": - "formidable/configurations/es6-node-test" rules: no-magic-numbers: OFF max-statements: OFF babel-plugin-transform-define-1.3.0/test/0/000077500000000000000000000000001310653716200204375ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/0/actual.js000066400000000000000000000001741310653716200222500ustar00rootroot00000000000000var x = PRODUCTION; if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/0/expected.js000066400000000000000000000001601310653716200225730ustar00rootroot00000000000000'use strict'; var x = 0; if (!0) { console.log('Debug info'); } if (0) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/emptyString/000077500000000000000000000000001310653716200226255ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/emptyString/actual.js000066400000000000000000000001741310653716200244360ustar00rootroot00000000000000var x = PRODUCTION; if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/emptyString/expected.js000066400000000000000000000001631310653716200247640ustar00rootroot00000000000000'use strict'; var x = ''; if (!'') { console.log('Debug info'); } if ('') { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/false/000077500000000000000000000000001310653716200213725ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/false/actual.js000066400000000000000000000001741310653716200232030ustar00rootroot00000000000000var x = PRODUCTION; if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/false/expected.js000066400000000000000000000001741310653716200235330ustar00rootroot00000000000000'use strict'; var x = false; if (!false) { console.log('Debug info'); } if (false) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/identifier/000077500000000000000000000000001310653716200224225ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/identifier/actual.js000066400000000000000000000002231310653716200242260ustar00rootroot00000000000000VERSION; var x = { "version": VERSION } if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/identifier/expected.js000066400000000000000000000002271310653716200245620ustar00rootroot00000000000000'use strict'; '1.0.0'; var x = { "version": '1.0.0' }; if (!true) { console.log('Debug info'); } if (true) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/index.js000066400000000000000000000121441310653716200217470ustar00rootroot00000000000000"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) => { return { "presets": ["es2015"], "plugins": [ [path.resolve(__dirname, "../lib/index.js"), pluginOps] ] }; }; describe("babel-plugin-transform-define", () => { before(function () { // TODO: WTF babel needs to warm up! This is Bullshit! this.timeout(10000); // eslint-disable-line babel.transform("const x = 1;", getBabelOps()); }); 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); }); }); 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); }); 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); }); it("should transform false", () => { const babelOpts = getBabelOps({ "PRODUCTION": false }); return assertTransform( path.join(__dirname, "./false/actual.js"), path.join(__dirname, "./false/expected.js"), babelOpts); }) it("should transform 0", () => { const babelOpts = getBabelOps({ "PRODUCTION": 0 }); return assertTransform( path.join(__dirname, "./0/actual.js"), path.join(__dirname, "./0/expected.js"), babelOpts); }) it("should transform empty string", () => { const babelOpts = getBabelOps({ "PRODUCTION": '' }); return assertTransform( path.join(__dirname, "./emptyString/actual.js"), path.join(__dirname, "./emptyString/expected.js"), babelOpts); }) it("should transform null", () => { const babelOpts = getBabelOps({ "PRODUCTION": null }); return assertTransform( path.join(__dirname, "./null/actual.js"), path.join(__dirname, "./null/expected.js"), babelOpts); }) it("should transform undefined", () => { const babelOpts = getBabelOps({ "PRODUCTION": undefined }); return assertTransform( path.join(__dirname, "./undefined/actual.js"), path.join(__dirname, "./undefined/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)); }); }); }); }); babel-plugin-transform-define-1.3.0/test/load-config-file/000077500000000000000000000000001310653716200233775ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/load-config-file/actual.js000066400000000000000000000000761310653716200252110ustar00rootroot00000000000000process.env.NODE_ENV; process.env.NODE_ENV === "development"; babel-plugin-transform-define-1.3.0/test/load-config-file/config.js000066400000000000000000000001151310653716200251770ustar00rootroot00000000000000"use strict"; module.exports = { "process.env.NODE_ENV": "development" }; babel-plugin-transform-define-1.3.0/test/load-config-file/expected.js000066400000000000000000000000441310653716200255340ustar00rootroot00000000000000"use strict"; "development"; true; babel-plugin-transform-define-1.3.0/test/member-expression/000077500000000000000000000000001310653716200237445ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/member-expression/actual.js000066400000000000000000000000761310653716200255560ustar00rootroot00000000000000process.env.NODE_ENV; process.env.NODE_ENV === "development"; babel-plugin-transform-define-1.3.0/test/member-expression/expected.js000066400000000000000000000000441310653716200261010ustar00rootroot00000000000000"use strict"; "development"; true; babel-plugin-transform-define-1.3.0/test/null/000077500000000000000000000000001310653716200212525ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/null/actual.js000066400000000000000000000001741310653716200230630ustar00rootroot00000000000000var x = PRODUCTION; if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/null/expected.js000066400000000000000000000001711310653716200234100ustar00rootroot00000000000000'use strict'; var x = null; if (!null) { console.log('Debug info'); } if (null) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/unary-expression/000077500000000000000000000000001310653716200236335ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/unary-expression/actual.js000066400000000000000000000000531310653716200254400ustar00rootroot00000000000000typeof window; typeof window === "object"; babel-plugin-transform-define-1.3.0/test/unary-expression/expected.js000066400000000000000000000000371310653716200257720ustar00rootroot00000000000000"use strict"; "object"; true; babel-plugin-transform-define-1.3.0/test/undefined/000077500000000000000000000000001310653716200222415ustar00rootroot00000000000000babel-plugin-transform-define-1.3.0/test/undefined/actual.js000066400000000000000000000001741310653716200240520ustar00rootroot00000000000000var x = PRODUCTION; if (!PRODUCTION) { console.log('Debug info'); } if (PRODUCTION) { console.log('Production log'); } babel-plugin-transform-define-1.3.0/test/undefined/expected.js000066400000000000000000000002101310653716200243710ustar00rootroot00000000000000'use strict'; var x = undefined; if (!undefined) { console.log('Debug info'); } if (undefined) { console.log('Production log'); }