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

Commit 076fdea

Browse files
author
Rajiv Tirumalareddy
committed
Initial commit
0 parents  commit 076fdea

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
tests/fixtures/app/bundle.js

lib/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
var loaderUtils = require('loader-utils');
4+
5+
function StripFnLoader(source) {
6+
var query = loaderUtils.parseQuery(this.query);
7+
8+
if (!query || !query.strip) return;
9+
10+
var toStrip = query.strip.join('|');
11+
12+
var regexPattern = new RegExp('\\n[ \\t]*(' + toStrip + ')\\([^\\);]+\\)[ \\t]*[;\\n]', 'g');
13+
14+
var transformed = source.replace(regexPattern, '\n');
15+
16+
this.callback(null, transformed);
17+
}
18+
module.exports = StripFnLoader;
19+
20+
module.exports.loader = function () {
21+
return __filename + '?' + [].slice.call(arguments, 0).map(function (fn) {
22+
return 'strip[]=' + fn;
23+
}).join(',');
24+
};

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "webpack-strip",
3+
"version": "0.0.1",
4+
"description": "Webpack loader to strip arbitrary functions out of your code.",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "mocha tests/unit --recursive --reporter spec"
8+
},
9+
"keywords": [
10+
"webpack",
11+
"strip"
12+
],
13+
"author": "Rajiv Tirumalareddy <rajivtirum@yahoo-inc.com>",
14+
"devDependencies": {
15+
"chai": "^1.10.0",
16+
"mocha": "^2.1.0",
17+
"webpack": "^1.4.15"
18+
},
19+
"dependencies": {
20+
"loader-utils": "^0.2.6"
21+
}
22+
}

tests/fixtures/app/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var makeFoo = function (bar, baz) {
2+
// The following 2 lines of code will be stripped with our webpack loader
3+
assert(1!==2, '1 must not equal 2');
4+
debug('1 must not equal 2');
5+
// This code would remain
6+
return new Foo(bar, baz);
7+
};

tests/unit/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*global describe,it */
2+
'use strict';
3+
4+
var webpack = require("webpack");
5+
var expect = require('chai').expect;
6+
var loaderLib = require('../../lib/index.js');
7+
var loaderLibPath = require.resolve('../../lib/index.js');
8+
var fs = require('fs');
9+
10+
var cwd = __dirname + '/../fixtures/app';
11+
12+
var createWebpackConfigWithLoader = function (loaderOpt) {
13+
return {
14+
context: cwd,
15+
entry: './index.js',
16+
output: {
17+
path: cwd,
18+
filename: 'bundle.js'
19+
},
20+
module: {
21+
loaders: [
22+
{ test: /\.js$/, loader: loaderOpt }
23+
]
24+
}
25+
};
26+
};
27+
28+
var createWebpackTest = function (done) {
29+
return function(err, stats) {
30+
31+
expect(err).to.be.null;
32+
expect(stats.hasErrors()).to.be.false;
33+
34+
var statsJson = stats.toJson();
35+
expect(statsJson.errors).to.have.length(0);
36+
37+
var originalSource = fs.readFileSync(cwd + '/index.js', {encoding: 'utf8'})
38+
expect(originalSource).to.contain('assert');
39+
expect(originalSource).to.contain('debug');
40+
41+
var strippedSource = statsJson.modules[0].source;
42+
expect(strippedSource).to.not.contain('assert');
43+
expect(strippedSource).to.not.contain('debug');
44+
45+
done(err);
46+
};
47+
}
48+
49+
50+
describe('integration', function () {
51+
describe('webpack', function () {
52+
it('should work with loader query params', function (done) {
53+
webpack(
54+
createWebpackConfigWithLoader(loaderLibPath + '?strip[]=assert,strip[]=debug'),
55+
createWebpackTest(done)
56+
);
57+
});
58+
59+
it('should work with loader usage as library', function (done) {
60+
webpack(
61+
createWebpackConfigWithLoader(loaderLib.loader('assert', 'debug')),
62+
createWebpackTest(done)
63+
);
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)