From 18588f55a8b07982c36370af85c41c8eabb8bdb1 Mon Sep 17 00:00:00 2001 From: Rajiv Tirumalareddy Date: Wed, 21 Jan 2015 18:47:41 -0800 Subject: [PATCH] Strip fn declarations in addition to calls from source --- README.md | 34 ++++++++++++++++++++-------------- lib/index.js | 9 +++++++-- tests/fixtures/app/index.js | 3 +++ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 05e5bf1..298fddd 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ In your client js source files: ```javascript +// The following two lines of code will be stripped with our webpack loader var debug = require('debug')('MyFile'); var makeFoo = function () { @@ -31,6 +32,25 @@ var makeFoo = function () { ``` +In your client js output files: + +```javascript + +// The following two lines of code will be stripped with our webpack loader + + +var makeFoo = function () { + // The following two lines of code will be stripped with our webpack loader + + + // This code would remain + return 'Foo'; +}; + +``` + + + ### Single function In your webpack config: @@ -72,20 +92,6 @@ var webpackConfig = { }; ``` -### Remove stripped functions from bundle - -So far we've removed the calls to the debug function, but webpack will still include the `debug` module in the final bundle. Use the [`IgnorePlugin`](http://webpack.github.io/docs/list-of-plugins.html#ignoreplugin) - -```javascript -{ - plugins: [ - new webpack.IgnorePlugin(/debug/) - ] -} -``` - - - ## License This software is free to use under the Yahoo! Inc. BSD license. diff --git a/lib/index.js b/lib/index.js index 9b6f111..dc7ca1c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,9 +14,14 @@ function StripFnLoader(source) { var toStrip = query.strip.join('|'); - var regexPattern = new RegExp('\\n[ \\t]*(' + toStrip + ')\\([^\\);]+\\)[ \\t]*[;\\n]', 'g'); + var fnDeclRegExp = new RegExp('\\n[ \\t]*var \\w+ = require\\(\\\\?[\'"](' + toStrip + ')[^\\)]*\\)[^;]*[;\\n]', 'g'); + var fnCallRegExp = new RegExp('\\n[ \\t]*(' + toStrip + ')\\([^\\);]+\\)[ \\t]*[;\\n]', 'g'); + + + var transformed = source.replace(fnDeclRegExp, '\n').replace(fnCallRegExp, '\n'); + + // var re = /\n[ \t]*var \w+ = require\(\\?['"]debug[^\)]*\)[^;]*[;\n]/g; - var transformed = source.replace(regexPattern, '\n'); this.callback(null, transformed); } diff --git a/tests/fixtures/app/index.js b/tests/fixtures/app/index.js index 87a1ef3..af23979 100644 --- a/tests/fixtures/app/index.js +++ b/tests/fixtures/app/index.js @@ -3,6 +3,9 @@ * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ +// The following line of code will be stripped with our webpack loader +var debug = require('debug')('Foo'); + var makeFoo = function (bar, baz) { // The following 2 lines of code will be stripped with our webpack loader console.log('some debug info');