-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathplugin.js
188 lines (149 loc) · 4.58 KB
/
plugin.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var MemoryFS = require("memory-fs");
var webpack = require("webpack");
var installer = require("./installer");
var depFromErr = function(err) {
if (!err) {
return undefined;
}
/**
* Supported package formats:
* - path
* - react-lite
* - @cycle/core
* - bootswatch/lumen/bootstrap.css
* - lodash.random
*/
var matches = /(?:(?:Cannot resolve module)|(?:Can't resolve)) '([@\w\/\.-]+)' in/.exec(err);
if (!matches) {
return undefined;
}
return matches[1];
}
function NpmInstallPlugin(options) {
this.preCompiler = null;
this.compiler = null;
this.options = Object.assign(installer.defaultOptions, options);
this.resolving = {};
installer.checkPackage();
installer.checkBabel();
}
NpmInstallPlugin.prototype.apply = function(compiler) {
this.compiler = compiler;
// Recursively install missing dependencies so primary build doesn't fail
compiler.plugin("watch-run", this.preCompile.bind(this));
// Install externals that wouldn't normally be resolved
if (Array.isArray(compiler.options.externals)) {
compiler.options.externals.unshift(this.resolveExternal.bind(this));
}
// Install loaders on demand
compiler.resolvers.loader.plugin("module", this.resolveLoader.bind(this));
// Install project dependencies on demand
compiler.resolvers.normal.plugin("module", this.resolveModule.bind(this));
};
NpmInstallPlugin.prototype.install = function(result) {
if (!result) {
return;
}
var dep = installer.check(result.request);
if (dep) {
var dev = this.options.dev;
if (typeof this.options.dev === "function") {
dev = !!this.options.dev(result.request, result.path);
}
installer.install(dep, Object.assign({}, this.options, { dev: dev }));
}
}
NpmInstallPlugin.prototype.preCompile = function(compilation, next) {
if (!this.preCompiler) {
var options = this.compiler.options;
var config = Object.assign(
// Start with new config object
{},
// Inherit the current config
options,
{
// Ensure fresh cache
cache: {},
// Register plugin to install missing deps
plugins: [
new NpmInstallPlugin(this.options),
],
}
);
this.preCompiler = webpack(config);
this.preCompiler.outputFileSystem = new MemoryFS();
}
this.preCompiler.run(next);
};
NpmInstallPlugin.prototype.resolveExternal = function(context, request, callback) {
// Only install direct dependencies, not sub-dependencies
if (context.match("node_modules")) {
return callback();
}
// Ignore !!bundle?lazy!./something
if (request.match(/(\?|\!)/)) {
return callback();
}
var result = {
context: {},
path: context,
request: request,
};
this.resolve(result, function(err, filepath) {
if (err) {
this.install(Object.assign({}, result, { request: depFromErr(err) }));
}
callback();
}.bind(this));
};
NpmInstallPlugin.prototype.resolve = function(result, callback) {
var version = require("webpack/package.json").version;
var major = version.split(".").shift();
if (major === "1") {
return this.compiler.resolvers.normal.resolve(
result.path,
result.request,
callback
);
}
if (major === "2") {
return this.compiler.resolvers.normal.resolve(
result.context || {},
result.path,
result.request,
callback
);
}
throw new Error("Unsupport Webpack version: " + version);
}
NpmInstallPlugin.prototype.resolveLoader = function(result, next) {
// Ensure loaders end with `-loader` (e.g. `babel` => `babel-loader`)
// Also force Webpack2's duplication of `-loader` to a single occurrence
var loader = result.request // e.g. react-hot-loader/webpack
.split("/") // ["react-hot-loader", "webpack"]
.shift() // "react-hot-loader"
.split("-loader") // ["react-hot", ""]
.shift() // "react-hot"
.concat("-loader") // "react-hot-loader"
;
this.install(Object.assign({}, result, { request: loader }));
return next();
};
NpmInstallPlugin.prototype.resolveModule = function(result, next) {
// Only install direct dependencies, not sub-dependencies
if (result.path.match("node_modules")) {
return next();
}
if (this.resolving[result.request]) {
return next();
}
this.resolving[result.request] = true;
this.resolve(result, function(err, filepath) {
this.resolving[result.request] = false;
if (err) {
this.install(Object.assign({}, result, { request: depFromErr(err) }));
}
return next();
}.bind(this));
};
module.exports = NpmInstallPlugin;