Skip to content

Commit 83eec78

Browse files
committed
follow the browser-module spec: ignoring export an empty module.
fixes webpack#187
1 parent b88938c commit 83eec78

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

lib/NormalModuleFactory.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var async = require("async");
66

77
var Tapable = require("tapable");
88
var NormalModule = require("./NormalModule");
9+
var RawModule = require("./RawModule");
910
var LoadersList = require("webpack-core/lib/LoadersList");
1011

1112
function NormalModuleFactory(context, resolvers, parser, options) {
@@ -55,7 +56,11 @@ NormalModuleFactory.prototype.create = function(context, dependency, callback) {
5556
var loaders = results[0];
5657
resource = results[1];
5758

58-
if(resource === false) return callback(); // ignored
59+
if(resource === false)
60+
return callback(null,
61+
new RawModule("/* (ignored) */",
62+
"ignored " + context + " " + request,
63+
request + " (ignored)")); // ignored
5964

6065
var userRequest = loaders.concat([resource]).join("!");
6166

lib/RawModule.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Module = require("./Module");
6+
var OriginalSource = require("webpack-core/lib/OriginalSource");
7+
var RawSource = require("webpack-core/lib/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;
16+
}
17+
module.exports = RawModule;
18+
19+
RawModule.prototype = Object.create(Module.prototype);
20+
21+
RawModule.prototype.identifier = function() {
22+
return this.identifierStr;
23+
};
24+
25+
RawModule.prototype.readableIdentifier = function(requestShortener) {
26+
return requestShortener.shorten(this.readableIdentifierStr);
27+
};
28+
29+
RawModule.prototype.needRebuild = function(fileTimestamps, contextTimestamps) {
30+
return false;
31+
};
32+
33+
RawModule.prototype.build = function(options, compilation, resolver, fs, callback) {
34+
this.builtTime = new Date().getTime();
35+
callback();
36+
};
37+
38+
RawModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
39+
if(this.useSourceMap)
40+
return new OriginalSource(this.sourceStr, this.identifier());
41+
else
42+
return new RawSource(this.sourceStr);
43+
};
44+
45+
RawModule.prototype.size = function() {
46+
return this.sourceStr.length;
47+
};
48+
49+
RawModule.prototype.getSourceHash = function() {
50+
var hash = require("crypto").createHash("md5");
51+
hash.update(this.sourceStr);
52+
return hash.digest("hex");
53+
};
54+
55+
RawModule.prototype.getAllModuleDependencies = function() {
56+
return [];
57+
};
58+
59+
RawModule.prototype.createTemplate = function() {
60+
return new RawModule(this.sourceStr, "template of " + this.id);
61+
};
62+
63+
RawModule.prototype.getTemplateArguments = function() {
64+
return [];
65+
};

test/cases/resolving/browser-field/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ it("should ignore recursive module mappings", function() {
2828
require("recursive-module").should.be.eql("new-module");
2929
});
3030

31+
it("should use empty modules for ignored modules", function() {
32+
require("ignoring-module").module.should.be.eql({});
33+
require("ignoring-module").file.should.be.eql({});
34+
require("ignoring-module").module.should.not.be.equal(require("ignoring-module").file);
35+
});
36+
3137
// Errors
3238
require.include("recursive-file/a");
3339
require.include("recursive-file/b");

test/cases/resolving/browser-field/node_modules/ignoring-module/file.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/resolving/browser-field/node_modules/ignoring-module/index.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/resolving/browser-field/node_modules/ignoring-module/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)