Skip to content
This repository has been archived by the owner on Jan 6, 2018. It is now read-only.

Commit

Permalink
Strip fn declarations in addition to calls from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijar committed Jan 22, 2015
1 parent 3492700 commit 18588f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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:

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 18588f5

Please sign in to comment.