├── .babelrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── src └── babel-plugin-strip-function-call.js └── test ├── babel-plugin-strip-function-test.js ├── fixtures ├── console │ ├── .babelrc │ ├── actual.js │ └── expected.js ├── deep-nest-method │ ├── .babelrc │ ├── actual.js │ └── expected.js ├── not-match-computed-method │ ├── .babelrc │ ├── actual.js │ └── expected.js ├── not-match-fn │ ├── .babelrc │ ├── actual.js │ └── expected.js ├── single-fn │ ├── .babelrc │ ├── actual.js │ └── expected.js └── wildcard-pattern │ ├── .babelrc │ ├── actual.js │ └── expected.js └── mocha.opts /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ], 5 | "env": { 6 | "development": { 7 | "presets": [ 8 | "jsdoc-to-assert", 9 | "power-assert" 10 | ] 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore 2 | 3 | # Logs 4 | logs 5 | *.log 6 | 7 | # Runtime data 8 | pids 9 | *.pid 10 | *.seed 11 | 12 | # Directory for instrumented libs generated by jscoverage/JSCover 13 | lib-cov 14 | 15 | # Coverage directory used by tools like istanbul 16 | coverage 17 | 18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 19 | .grunt 20 | 21 | # node-waf configuration 22 | .lock-wscript 23 | 24 | # Compiled binary addons (http://nodejs.org/api/addons.html) 25 | build/Release 26 | 27 | # Dependency directory 28 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 29 | node_modules 30 | 31 | # Debug log from npm 32 | npm-debug.log 33 | 34 | 35 | ### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Global/JetBrains.gitignore 36 | 37 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 38 | 39 | *.iml 40 | 41 | ## Directory-based project format: 42 | .idea/ 43 | # if you remove the above rule, at least ignore the following: 44 | 45 | # User-specific stuff: 46 | # .idea/workspace.xml 47 | # .idea/tasks.xml 48 | # .idea/dictionaries 49 | 50 | # Sensitive or high-churn files: 51 | # .idea/dataSources.ids 52 | # .idea/dataSources.xml 53 | # .idea/sqlDataSources.xml 54 | # .idea/dynamic.xml 55 | # .idea/uiDesigner.xml 56 | 57 | # Gradle: 58 | # .idea/gradle.xml 59 | # .idea/libraries 60 | 61 | # Mongo Explorer plugin: 62 | # .idea/mongoSettings.xml 63 | 64 | ## File-based project format: 65 | *.ipr 66 | *.iws 67 | 68 | ## Plugin-specific files: 69 | 70 | # IntelliJ 71 | out/ 72 | 73 | # mpeltonen/sbt-idea plugin 74 | .idea_modules/ 75 | 76 | # JIRA plugin 77 | atlassian-ide-plugin.xml 78 | 79 | # Crashlytics plugin (for Android Studio and IntelliJ) 80 | com_crashlytics_export_strings.xml 81 | crashlytics.properties 82 | crashlytics-build.properties 83 | 84 | 85 | /lib 86 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: "stable" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 azu 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # babel-plugin-strip-function-call [![Build Status](https://travis-ci.org/azu/babel-plugin-strip-function-call.svg?branch=master)](https://travis-ci.org/azu/babel-plugin-strip-function-call) 2 | 3 | Babel plugin that strip any function call. 4 | 5 | This is simiar with [yahoo/strip-loader: Webpack loader to strip arbitrary functions out of your production code.](https://github.com/yahoo/strip-loader "yahoo/strip-loader: Webpack loader to strip arbitrary functions out of your production code."). 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | npm install babel-plugin-strip-function-call --save-dev 12 | 13 | ## Usage 14 | 15 | 16 | Via `.babelrc` 17 | 18 | ```json 19 | { 20 | "plugins": [ 21 | ["strip-function-call", { 22 | "strip": [ 23 | "console.log" 24 | ] 25 | }] 26 | ] 27 | } 28 | ``` 29 | 30 | In production only: 31 | 32 | ```json 33 | { 34 | "presets": [ 35 | "es2015" 36 | ], 37 | "env": { 38 | "production": { 39 | "plugins": [ 40 | ["strip-function-call", { 41 | "strip": [ 42 | "console.log" 43 | ] 44 | }] 45 | ] 46 | } 47 | } 48 | } 49 | ``` 50 | 51 | ## Options 52 | 53 | - `strip`: `string[]` 54 | - specify to strip function names 55 | 56 | For example, want to strip `console.log(...)`. 57 | Write following: 58 | 59 | ```js 60 | ["strip-function-call", { 61 | "strip": [ 62 | // not include () 63 | "console.log" 64 | ] 65 | }] 66 | ``` 67 | 68 | ### Notes 69 | 70 | ```js 71 | ["strip-function-call", { 72 | "strip": [ 73 | // not include () 74 | "console.log" 75 | ] 76 | }] 77 | ``` 78 | 79 | The pattern don't strip `console["log"](...)` by design. 80 | If you want to strip computed method pattern, please file issue. 81 | 82 | 83 | ## Changelog 84 | 85 | See [Releases page](https://github.com/azu/babel-plugin-strip-function-call/releases). 86 | 87 | ## Running tests 88 | 89 | Install devDependencies and Run `npm test`: 90 | 91 | npm i -d && npm test 92 | 93 | ## Contributing 94 | 95 | Pull requests and stars are always welcome. 96 | 97 | For bugs and feature requests, [please create an issue](https://github.com/azu/babel-plugin-strip-function-call/issues). 98 | 99 | 1. Fork it! 100 | 2. Create your feature branch: `git checkout -b my-new-feature` 101 | 3. Commit your changes: `git commit -am 'Add some feature'` 102 | 4. Push to the branch: `git push origin my-new-feature` 103 | 5. Submit a pull request :D 104 | 105 | ## Author 106 | 107 | - [github/azu](https://github.com/azu) 108 | - [twitter/azu_re](https://twitter.com/azu_re) 109 | 110 | ## License 111 | 112 | MIT © azu 113 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "directories": { 3 | "test": "test" 4 | }, 5 | "author": "azu", 6 | "license": "MIT", 7 | "files": [ 8 | "bin/", 9 | "lib/", 10 | "src/" 11 | ], 12 | "name": "babel-plugin-strip-function-call", 13 | "version": "1.0.2", 14 | "description": "Babel plugin strip any function call.", 15 | "main": "lib/babel-plugin-strip-function-call.js", 16 | "scripts": { 17 | "test": "mocha test/", 18 | "build": "NODE_ENV=production babel src --out-dir lib --source-maps", 19 | "watch": "babel src --out-dir lib --watch --source-maps", 20 | "prepublish": "npm run --if-present build" 21 | }, 22 | "keywords": [ 23 | "babel", 24 | "plugin", 25 | "optimize", 26 | "production" 27 | ], 28 | "repository": { 29 | "type": "git", 30 | "url": "https://github.com/azu/babel-plugin-strip-function-call.git" 31 | }, 32 | "bugs": { 33 | "url": "https://github.com/azu/babel-plugin-strip-function-call/issues" 34 | }, 35 | "homepage": "https://github.com/azu/babel-plugin-strip-function-call", 36 | "devDependencies": { 37 | "babel-cli": "^6.18.0", 38 | "babel-core": "^6.18.0", 39 | "babel-preset-es2015": "^6.18.0", 40 | "babel-preset-jsdoc-to-assert": "^4.0.0", 41 | "babel-preset-power-assert": "^1.0.0", 42 | "babel-register": "^6.18.0", 43 | "mocha": "^3.1.2", 44 | "power-assert": "^1.4.1" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/babel-plugin-strip-function-call.js: -------------------------------------------------------------------------------- 1 | // LICENSE : MIT 2 | "use strict"; 3 | module.exports = function() { 4 | return { 5 | visitor: { 6 | CallExpression(path) { 7 | const stripFunctionNameList = this.opts.strip || []; 8 | const isMatched = stripFunctionNameList.some((fnName) => { 9 | // method call 10 | const calleePath = path.get("callee"); 11 | if (calleePath.matchesPattern(fnName)) { 12 | // ignore computed style 13 | // e.g.) console["log"]() 14 | return !calleePath.node.computed; 15 | 16 | } 17 | // function call 18 | return calleePath.node.name === fnName; 19 | }); 20 | if (isMatched) { 21 | path.remove(); 22 | } 23 | } 24 | } 25 | }; 26 | }; -------------------------------------------------------------------------------- /test/babel-plugin-strip-function-test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | import plugin from '../src/babel-plugin-strip-function-call'; 5 | import {transformFileSync} from 'babel-core'; 6 | function trim(str) { 7 | return str.replace(/^\s+|\s+$/, ''); 8 | } 9 | 10 | describe('finds JSDoc', () => { 11 | const fixturesDir = path.join(__dirname, 'fixtures'); 12 | fs.readdirSync(fixturesDir).map((caseName) => { 13 | it(`should ${caseName.split('-').join(' ')}`, () => { 14 | const fixtureDir = path.join(fixturesDir, caseName); 15 | let actualPath = path.join(fixtureDir, 'actual.js'); 16 | const actual = transformFileSync(actualPath).code; 17 | 18 | if (path.sep === '\\') { 19 | // Specific case of windows, transformFileSync return code with '/' 20 | actualPath = actualPath.replace(/\\/g, '/'); 21 | } 22 | 23 | const expected = fs.readFileSync( 24 | path.join(fixtureDir, 'expected.js') 25 | ).toString().replace(/%FIXTURE_PATH%/g, actualPath); 26 | 27 | assert.equal(trim(actual), trim(expected)); 28 | }); 29 | }); 30 | }); -------------------------------------------------------------------------------- /test/fixtures/console/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "console.log" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/console/actual.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | console.log("test"); -------------------------------------------------------------------------------- /test/fixtures/console/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; -------------------------------------------------------------------------------- /test/fixtures/deep-nest-method/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "Application.logger.log" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/deep-nest-method/actual.js: -------------------------------------------------------------------------------- 1 | Application.logger.log(1, 2, 3,); -------------------------------------------------------------------------------- /test/fixtures/deep-nest-method/expected.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/azu/babel-plugin-strip-function-call/a73b5e0e694ddb2d9cbf4e0de29058aa6fa44cf4/test/fixtures/deep-nest-method/expected.js -------------------------------------------------------------------------------- /test/fixtures/not-match-computed-method/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "console.log" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/not-match-computed-method/actual.js: -------------------------------------------------------------------------------- 1 | console["log"]("test"); -------------------------------------------------------------------------------- /test/fixtures/not-match-computed-method/expected.js: -------------------------------------------------------------------------------- 1 | console["log"]("test"); -------------------------------------------------------------------------------- /test/fixtures/not-match-fn/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "fn" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/not-match-fn/actual.js: -------------------------------------------------------------------------------- 1 | var fn = function () {}; -------------------------------------------------------------------------------- /test/fixtures/not-match-fn/expected.js: -------------------------------------------------------------------------------- 1 | var fn = function () {}; -------------------------------------------------------------------------------- /test/fixtures/single-fn/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "fn" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/single-fn/actual.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | fn("test"); -------------------------------------------------------------------------------- /test/fixtures/single-fn/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; -------------------------------------------------------------------------------- /test/fixtures/wildcard-pattern/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | [ 4 | "../../../src/babel-plugin-strip-function-call", 5 | { 6 | "strip": [ 7 | "*.log" 8 | ] 9 | } 10 | ] 11 | ] 12 | } -------------------------------------------------------------------------------- /test/fixtures/wildcard-pattern/actual.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | console.log("test"); 3 | app.log(1, 2); 4 | logger.log(1, 2, 3); -------------------------------------------------------------------------------- /test/fixtures/wildcard-pattern/expected.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers js:babel-register --------------------------------------------------------------------------------