forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeTargetPlugin.js
More file actions
113 lines (86 loc) · 3.5 KB
/
NodeTargetPlugin.js
File metadata and controls
113 lines (86 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Dependency = require("../Dependency");
var Module = require("../Module");
var RawSource = require("webpack-core/lib/RawSource");
function NodeNativeDependency(request, range) {
Dependency.call(this);
this.Class = NodeNativeDependency;
this.userRequest = request;
this.request = request;
this.range = range;
}
NodeNativeDependency.prototype = Object.create(Dependency.prototype);
NodeNativeDependency.prototype.type = "native module";
NodeNativeDependency.prototype.isEqualResource = function isEqualResource(other) {
if(!(other instanceof NodeNativeDependency))
return false;
return this.request == other.request;
};
NodeNativeDependency.Template = require("../dependencies/ModuleDependencyTemplateAsRequireId");
function NodeNativeCommonJsDependency(request, range) {
NodeNativeDependency.call(this, request, range);
this.Class = NodeNativeCommonJsDependency;
}
NodeNativeCommonJsDependency.prototype = Object.create(NodeNativeDependency.prototype);
NodeNativeCommonJsDependency.Template = require("../dependencies/ModuleDependencyTemplateAsId");
function NodeNativeModule(request) {
Module.call(this);
this.request = request;
this.built = false;
this.cacheable = true;
}
NodeNativeModule.prototype = Object.create(Module.prototype);
NodeNativeModule.prototype.identifier = NodeNativeModule.prototype.readableIdentifier = function() {
return this.request;
};
NodeNativeModule.prototype.build = function(options, compilation, resolver, fs, callback) {callback()};
NodeNativeModule.prototype.source = function() {
return new RawSource("module.exports = require.parentRequire(" + JSON.stringify(this.request) + ");");
};
NodeNativeModule.prototype.needRebuild = function() {
return false;
};
NodeNativeModule.prototype.size = function() {
return 42 + this.request.length;
};
function NodeNativeModuleFactory() {
}
NodeNativeModuleFactory.prototype.create = function(context, dependency, callback) {
return callback(null, new NodeNativeModule(dependency.request));
}
function NodeTargetPlugin() {
}
module.exports = NodeTargetPlugin;
NodeTargetPlugin.prototype.apply = function(compiler) {
compiler.plugin("compilation", function(compilation, params) {
compilation.dependencyFactories.set(NodeNativeDependency, new NodeNativeModuleFactory());
compilation.dependencyTemplates.set(NodeNativeDependency, new NodeNativeDependency.Template());
compilation.dependencyFactories.set(NodeNativeCommonJsDependency, new NodeNativeModuleFactory());
compilation.dependencyTemplates.set(NodeNativeCommonJsDependency, new NodeNativeCommonJsDependency.Template());
});
var natives = Object.keys(process.binding("natives"));
compiler.parser.plugin("call require:commonjs:item", function(expr, param) {
if(param.isString() && natives.indexOf(param.string) >= 0) {
var dep = new NodeNativeCommonJsDependency(param.string, param.range);
this.state.current.addDependency(dep);
return true;
}
});
compiler.parser.plugin("call define:amd:item", function(expr, param) {
if(param.isString() && natives.indexOf(param.string) >= 0) {
var dep = new NodeNativeDependency(param.string, param.range);
this.state.current.addDependency(dep);
return true;
}
});
compiler.parser.plugin("call require:amd:item", function(expr, param) {
if(param.isString() && natives.indexOf(param.string) >= 0) {
var dep = new NodeNativeDependency(param.string, param.range);
this.state.current.addDependency(dep);
return true;
}
});
};