Skip to content

Commit 24594b3

Browse files
nitishdayalTheLarkInn
authored andcommitted
refactor(es6): Update RawModule to ES2015 syntax (webpack#3691)
* refactor(es6): Upgrade ModuleReason and ModuleTemplate to es6 * added RawModule tests
1 parent 8940f6d commit 24594b3

2 files changed

Lines changed: 116 additions & 43 deletions

File tree

lib/RawModule.js

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,49 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var Module = require("./Module");
6-
var OriginalSource = require("webpack-sources").OriginalSource;
7-
var RawSource = require("webpack-sources").RawSource;
8-
9-
function RawModule(source, identifier, readableIdentifier) {
10-
Module.call(this);
11-
this.sourceStr = source;
12-
this.identifierStr = identifier || this.sourceStr;
13-
this.readableIdentifierStr = readableIdentifier || this.identifierStr;
14-
this.cacheable = true;
15-
this.built = false;
5+
"use strict";
6+
7+
const Module = require("./Module");
8+
const OriginalSource = require("webpack-sources").OriginalSource;
9+
const RawSource = require("webpack-sources").RawSource;
10+
11+
module.exports = class RawModule extends Module {
12+
13+
constructor(source, identifier, readableIdentifier) {
14+
super()
15+
this.sourceStr = source;
16+
this.identifierStr = identifier || this.sourceStr;
17+
this.readableIdentifierStr = readableIdentifier || this.identifierStr;
18+
this.cacheable = true;
19+
this.built = false;
20+
}
21+
22+
identifier() {
23+
return this.identifierStr;
24+
}
25+
26+
size() {
27+
return this.sourceStr.length;
28+
}
29+
30+
readableIdentifier(requestShortener) {
31+
return requestShortener.shorten(this.readableIdentifierStr);
32+
}
33+
34+
needRebuild() {
35+
return false;
36+
}
37+
38+
build(options, compilations, resolver, fs, callback) {
39+
this.builtTime = new Date().getTime();
40+
callback();
41+
}
42+
43+
source() {
44+
if(this.useSourceMap)
45+
return new OriginalSource(this.sourceStr, this.identifier());
46+
else
47+
return new RawSource(this.sourceStr);
48+
}
49+
1650
}
17-
module.exports = RawModule;
18-
19-
RawModule.prototype = Object.create(Module.prototype);
20-
RawModule.prototype.constructor = RawModule;
21-
22-
RawModule.prototype.identifier = function() {
23-
return this.identifierStr;
24-
};
25-
26-
RawModule.prototype.readableIdentifier = function(requestShortener) {
27-
return requestShortener.shorten(this.readableIdentifierStr);
28-
};
29-
30-
RawModule.prototype.needRebuild = function() {
31-
return false;
32-
};
33-
34-
RawModule.prototype.build = function(options, compilation, resolver, fs, callback) {
35-
this.builtTime = new Date().getTime();
36-
callback();
37-
};
38-
39-
RawModule.prototype.source = function() {
40-
if(this.useSourceMap)
41-
return new OriginalSource(this.sourceStr, this.identifier());
42-
else
43-
return new RawSource(this.sourceStr);
44-
};
45-
46-
RawModule.prototype.size = function() {
47-
return this.sourceStr.length;
48-
};

test/RawModule.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var RawModule = require("../lib/RawModule");
2+
var OriginalSource = require("webpack-sources").OriginalSource;
3+
var RawSource = require("webpack-sources").RawSource;
4+
var RequestShortener = require("../lib/RequestShortener");
5+
var should = require("should");
6+
var path = require("path")
7+
8+
describe("RawModule", function() {
9+
var myRawModule;
10+
11+
before(function() {
12+
var source = 'sourceStr attribute';
13+
var identifier = 'identifierStr attribute';
14+
var readableIdentifier = 'readableIdentifierStr attribute';
15+
myRawModule = new RawModule(source, identifier, readableIdentifier);
16+
});
17+
18+
describe('identifier', function() {
19+
it('returns value for identifierStr attribute', function() {
20+
should(myRawModule.identifier()).be.exactly('identifierStr attribute');
21+
});
22+
});
23+
24+
describe('size', function() {
25+
it('returns value for sourceStr attribute\'s length property', function() {
26+
var sourceStrLength = myRawModule.sourceStr.length;
27+
should(myRawModule.size()).be.exactly(sourceStrLength);
28+
});
29+
});
30+
31+
describe('readableIdentifier', function() {
32+
it('returns result of calling provided requestShortener\'s shorten method\
33+
on readableIdentifierStr attribute', function() {
34+
var requestShortener = new RequestShortener(path.resolve());
35+
should.exist(myRawModule.readableIdentifier(requestShortener));
36+
});
37+
});
38+
39+
describe('needRebuild', function() {
40+
it('returns false', function() {
41+
should(myRawModule.needRebuild()).be.false();
42+
});
43+
});
44+
45+
describe('build', function() {
46+
it('sets builtTime attribute to current time value in milliseconds', function() {
47+
myRawModule.build('', '', '', '', () => {
48+
undefined
49+
})
50+
var currentTime = new Date().getTime();
51+
myRawModule.builtTime.should.be.exactly(currentTime);
52+
});
53+
});
54+
55+
describe('source', function() {
56+
it('returns a new OriginalSource instance with sourceStr attribute and\
57+
return value of identifier() function provided as constructor arguments',
58+
function() {
59+
var originalSource = new OriginalSource(myRawModule.sourceStr, myRawModule.identifier());
60+
myRawModule.useSourceMap = true;
61+
myRawModule.source().should.match(originalSource);
62+
});
63+
64+
it('returns a new RawSource instance with sourceStr attribute provided\
65+
as constructor argument if useSourceMap is falsey', function() {
66+
var rawSource = new RawSource(myRawModule.sourceStr);
67+
myRawModule.useSourceMap = false;
68+
myRawModule.source().should.match(rawSource);
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)