├── .npmignore ├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── tests ├── fixtures │ └── app │ │ └── index.js └── unit │ └── index.js ├── lib └── index.js ├── package.json ├── LICENSE.md └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | /artifacts/ 2 | /tests/ 3 | -------------------------------------------------------------------------------- /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | tests/fixtures/app/bundle.js 3 | /artifacts/ 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.11" 4 | - "0.10" 5 | after_success: 6 | - "npm run cover" 7 | - "cat artifacts/lcov.info | ./node_modules/coveralls/bin/coveralls.js" 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing Code to `strip-loader` 2 | ------------------------------- 3 | 4 | Please be sure to sign our [CLA][] before you submit pull requests or otherwise contribute to `strip-loader`. This protects developers, who rely on [BSD license][]. 5 | 6 | [BSD license]: https://github.com/yahoo/strip-loader/blob/master/LICENSE.md 7 | [CLA]: https://yahoocla.herokuapp.com/ 8 | -------------------------------------------------------------------------------- /tests/fixtures/app/index.js: -------------------------------------------------------------------------------- 1 | console.log('a console.log on the first line should get stripped'); 2 | /** 3 | * Copyright 2015, Yahoo! Inc. 4 | * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. 5 | */ 6 | 7 | var makeFoo = function (bar, baz) { 8 | // The following 2 lines of code will be stripped with our webpack loader 9 | var foo = function (something) { 10 | return "foo"; 11 | }; 12 | console.log('some debug info'); 13 | debug('better debug info'); 14 | console.log('some' + "debug " + info + '(even closing parenthesis);'); 15 | console.log('some' + "debug" + foo(info) + '(even closing parenthesis)'); 16 | // This code would remain 17 | return new Foo(bar, baz); 18 | }; 19 | console.log('a console.log on the last line without a semicolon should get stripped') 20 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015, Yahoo! Inc. 3 | * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. 4 | */ 5 | 6 | "use strict"; 7 | 8 | var loaderUtils = require('loader-utils'); 9 | 10 | function StripFnLoader(source) { 11 | var query = loaderUtils.parseQuery(this.query); 12 | 13 | if (!query || !query.strip) return; 14 | 15 | var toStrip = query.strip.join('|'); 16 | 17 | var regexPattern = new RegExp('(?:^|\\n)[ \\t]*(' + toStrip + ')\\(((\\"[^\\"]*\\")|(\\\'[^\\\']*\\\')|[^\\);]|\\([^\\);]*\\))*\\)[ \\t]*(?:$|[;\\n])', 'g'); 18 | 19 | var transformed = source.replace(regexPattern, '\n'); 20 | 21 | this.callback(null, transformed); 22 | } 23 | module.exports = StripFnLoader; 24 | 25 | module.exports.loader = function () { 26 | return __filename + '?' + [].slice.call(arguments, 0).map(function (fn) { 27 | return 'strip[]=' + fn; 28 | }).join(','); 29 | }; 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strip-loader", 3 | "version": "0.1.2", 4 | "description": "Webpack loader to strip arbitrary functions out of your production code.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "cover": "node node_modules/istanbul/lib/cli.js cover --dir artifacts -- ./node_modules/mocha/bin/_mocha tests/unit/ --recursive --reporter spec", 8 | "lint": "./node_modules/.bin/jshint lib tests", 9 | "test": "mocha tests/unit --recursive --reporter spec" 10 | }, 11 | "keywords": [ 12 | "webpack", 13 | "strip" 14 | ], 15 | "repository": { 16 | "type": "git", 17 | "url": "git@github.com:yahoo/strip-loader" 18 | }, 19 | "author": "Rajiv Tirumalareddy ", 20 | "devDependencies": { 21 | "chai": "^1.10.0", 22 | "coveralls": "^2.11.2", 23 | "istanbul": "^0.3.5", 24 | "jshint": "^2.5.11", 25 | "mocha": "^2.1.0", 26 | "webpack": "^1.4.15" 27 | }, 28 | "dependencies": { 29 | "loader-utils": "^0.2.6" 30 | }, 31 | "licenses": [ 32 | { 33 | "type": "BSD", 34 | "url": "https://github.com/yahoo/strip-loader/blob/master/LICENSE.md" 35 | } 36 | ], 37 | "jshintConfig": { 38 | "node": true 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | ======================================== 3 | 4 | Copyright (c) 2015, Yahoo! Inc. All rights reserved. 5 | ---------------------------------------------------- 6 | 7 | Redistribution and use of this software in source and binary forms, with or 8 | without modification, are permitted provided that the following conditions are 9 | met: 10 | 11 | * Redistributions of source code must retain the above copyright notice, this 12 | list of conditions and the following disclaimer. 13 | * Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | * Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be 17 | used to endorse or promote products derived from this software without 18 | specific prior written permission of Yahoo! Inc. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 27 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /tests/unit/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015, Yahoo! Inc. 3 | * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. 4 | */ 5 | 6 | /*global describe,it */ 7 | 'use strict'; 8 | 9 | var webpack = require("webpack"); 10 | var expect = require('chai').expect; 11 | var loaderLib = require('../../lib/index.js'); 12 | var loaderLibPath = require.resolve('../../lib/index.js'); 13 | var fs = require('fs'); 14 | 15 | var cwd = __dirname + '/../fixtures/app'; 16 | 17 | var createWebpackConfigWithLoader = function (loaderOpt) { 18 | return { 19 | context: cwd, 20 | entry: './index.js', 21 | output: { 22 | path: cwd, 23 | filename: 'bundle.js' 24 | }, 25 | module: { 26 | loaders: [ 27 | { test: /\.js$/, loader: loaderOpt } 28 | ] 29 | } 30 | }; 31 | }; 32 | 33 | var createWebpackTest = function (done) { 34 | return function(err, stats) { 35 | 36 | expect(err).to.be.null(); 37 | expect(stats.hasErrors()).to.be.false(); 38 | 39 | var statsJson = stats.toJson(); 40 | expect(statsJson.errors).to.have.length(0); 41 | 42 | var originalSource = fs.readFileSync(cwd + '/index.js', {encoding: 'utf8'}); 43 | expect(originalSource).to.contain('console.log'); 44 | expect(originalSource).to.contain('debug'); 45 | 46 | var strippedSource = statsJson.modules[0].source; 47 | expect(strippedSource).to.not.contain('console.log'); 48 | expect(strippedSource).to.not.contain('debug'); 49 | 50 | done(err); 51 | }; 52 | }; 53 | 54 | 55 | describe('integration', function () { 56 | describe('webpack', function () { 57 | it('should work with loader query params', function (done) { 58 | webpack( 59 | createWebpackConfigWithLoader(loaderLibPath + '?strip[]=console.log,strip[]=debug'), 60 | createWebpackTest(done) 61 | ); 62 | }); 63 | 64 | it('should work with loader used as library', function (done) { 65 | webpack( 66 | createWebpackConfigWithLoader(loaderLib.loader('console.log', 'debug')), 67 | createWebpackTest(done) 68 | ); 69 | }); 70 | }); 71 | }); 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ># This Project Is Deprecated 2 | 3 | >There have been a lot of long standing issues that haven't been addressed and we haven't had the time to dedicate to this library. If I were to restart this project today, it would probably be a codemod script using Facebook's [jscodeshift](https://github.com/facebook/jscodeshift). The regex based approach in this loader only works for very basic use cases. 4 | 5 | >The most common use case is when trying to strip `console.log` from your code. You can actually do this without using this loader at all. You can use uglifyjs's `drop_console` option. Here is what that would look like in a webpack plugin: 6 | ``` 7 | new webpack.optimize.UglifyJsPlugin({ 8 | compress: { 9 | drop_console: true 10 | } 11 | }) 12 | ``` 13 | 14 | 15 | 16 | # Strip Loader 17 | 18 | [![npm version](https://badge.fury.io/js/strip-loader.svg)](http://badge.fury.io/js/strip-loader) 19 | [![Build Status](https://travis-ci.org/yahoo/strip-loader.svg?branch=master)](https://travis-ci.org/yahoo/strip-loader) 20 | [![Dependency Status](https://david-dm.org/yahoo/strip-loader.svg)](https://david-dm.org/yahoo/strip-loader) 21 | [![devDependency Status](https://david-dm.org/yahoo/strip-loader/dev-status.svg)](https://david-dm.org/yahoo/strip-loader#info=devDependencies) 22 | [![Coverage Status](https://coveralls.io/repos/yahoo/strip-loader/badge.png?branch=master)](https://coveralls.io/r/yahoo/strip-loader?branch=master) 23 | 24 | Simple [Webpack](http://webpack.github.io/) loader to strip custom functions from your code. This can be useful if you want to use debug statements while developing your app but don't want this info exposed in your production code. 25 | 26 | 27 | ## Install 28 | 29 | `npm install --save-dev strip-loader` 30 | 31 | ## Usage 32 | 33 | In your client js source files: 34 | 35 | ```javascript 36 | 37 | var debug = require('debug')('MyFile'); 38 | 39 | var makeFoo = function () { 40 | // The following two lines of code will be stripped with our webpack loader 41 | debug('makeFoo called'); 42 | debug('makeFoo args', arguments); 43 | // This code would remain 44 | return 'Foo'; 45 | }; 46 | 47 | ``` 48 | 49 | ### Single function 50 | In your webpack config: 51 | 52 | ```javascript 53 | { 54 | module: { 55 | loaders: [ 56 | { test: /\.js$/, loader: "strip-loader?strip[]=debug" } 57 | ] 58 | } 59 | }; 60 | ``` 61 | 62 | ### Multiple functions 63 | In your webpack config: 64 | 65 | ```javascript 66 | { 67 | module: { 68 | loaders: [ 69 | { test: /\.js$/, loader: "strip-loader?strip[]=debug,strip[]=console.log" } 70 | ] 71 | } 72 | }; 73 | ``` 74 | 75 | ### Use as library 76 | In your webpack config: 77 | 78 | ```javascript 79 | var WebpackStrip = require('strip-loader'); 80 | 81 | var webpackConfig = { 82 | module: { 83 | loaders: [ 84 | { test: /\.js$/, loader: WebpackStrip.loader('debug', 'console.log') } 85 | ] 86 | } 87 | }; 88 | ``` 89 | 90 | ### Replace unused module 91 | 92 | So far we've removed the calls to the debug function, but your app still requires the `debug` module in the final bundle. Use the [`NormalModuleReplacementPlugin`](http://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin) to replace it with an empty function: 93 | 94 | ```javascript 95 | // webpack config 96 | { 97 | plugins: [ 98 | new webpack.NormalModuleReplacementPlugin(/debug/, process.cwd() + '/emptyDebug.js'), 99 | ] 100 | } 101 | 102 | // emptyDebug.js 103 | module.exports = function() { return new Function(); }; 104 | ``` 105 | 106 | 107 | 108 | ## License 109 | 110 | This software is free to use under the Yahoo! Inc. BSD license. 111 | See the [LICENSE file][] for license text and copyright information. 112 | 113 | [LICENSE file]: https://github.com/yahoo/strip-loader/blob/master/LICENSE.md 114 | --------------------------------------------------------------------------------