forked from codymikol/karma-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (164 loc) · 5.85 KB
/
index.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
189
190
var _ = require("lodash");
var path = require("path");
var async = require("async");
var webpackDevMiddleware = require("webpack-dev-middleware");
var webpack = require("webpack");
var SingleEntryDependency = require("webpack/lib/dependencies/SingleEntryDependency");
function Plugin(
/* config.webpack */webpackOptions,
/* config.webpackServer */webpackServerOptions,
/* config.webpackMiddleware */webpackMiddlewareOptions,
/* config.basePath */basePath,
/* config.files */files,
/* config.frameworks */frameworks,
fileList,
customFileHandlers,
emitter) {
webpackOptions = _.clone(webpackOptions) || {};
webpackMiddlewareOptions = _.clone(webpackMiddlewareOptions || webpackServerOptions) || {};
var applyOptions = Array.isArray(webpackOptions) ? webpackOptions : [webpackOptions];
var includeIndex = applyOptions.length > 1;
applyOptions.forEach(function(webpackOptions, index) {
// The webpack tier owns the watch behavior so we want to force it in the config
webpackOptions.watch = true;
if(!webpackOptions.output) webpackOptions.output = {};
// When using an array, even of length 1, we want to include the index value for the build.
// This is due to the way that the dev server exposes commonPath for build output.
var indexPath = includeIndex ? index + "/" : "";
// Must have the common _karma_webpack_ prefix on path here to avoid
// https://github.com/webpack/webpack/issues/645
webpackOptions.output.path = "/_karma_webpack_/" + indexPath;
webpackOptions.output.publicPath = "/_karma_webpack_/" + indexPath + "/";
webpackOptions.output.filename = "[name]";
if(includeIndex)
webpackOptions.output.jsonpFunction = "webpackJsonp" + index;
webpackOptions.output.chunkFilename = "[id].chunk.js";
});
this.fileList = fileList;
this.wrapMocha = frameworks.indexOf('mocha') >= 0 && includeIndex;
this.optionsCount = applyOptions.length;
this.files = [];
this.basePath = basePath;
this.waiting = [];
var compiler = webpack(webpackOptions);
var applyPlugins = compiler.compilers || [compiler];
applyPlugins.forEach(function(compiler) {
compiler.plugin("this-compilation", function(compilation, params) {
compilation.dependencyFactories.set(SingleEntryDependency, params.normalModuleFactory);
});
compiler.plugin("make", this.make.bind(this));
}, this);
compiler.plugin("done", function(stats) {
var applyStats = Array.isArray(stats.stats) ? stats.stats : [stats];
var assets = [];
var noAssets = false;
applyStats.forEach(function(stats) {
stats = stats.toJson();
assets.push.apply(assets, stats.assets);
if(stats.assets.length === 0)
noAssets = true;
});
if(!this.waiting || this.waiting.length === 0) {
this.notifyKarmaAboutChanges();
}
if(this.waiting && !noAssets) {
var w = this.waiting;
this.waiting = null;
w.forEach(function(cb) {
cb();
});
}
}.bind(this));
compiler.plugin("invalid", function() {
if(!this.waiting) this.waiting = [];
}.bind(this));
webpackMiddlewareOptions.publicPath = "/_karma_webpack_/";
var middleware = this.middleware = new webpackDevMiddleware(compiler, webpackMiddlewareOptions);
customFileHandlers.push({
urlRegex: /^\/_karma_webpack_\/.*/,
handler: function(req, res) {
middleware(req, res, function() {
res.statusCode = 404;
res.end('Not found');
});
}
});
emitter.on("exit", function (done) {
middleware.close();
done();
});
}
Plugin.prototype.notifyKarmaAboutChanges = function() {
// Force a rebuild
this.fileList.refresh();
};
Plugin.prototype.addFile = function(entry) {
if(this.files.indexOf(entry) >= 0) return;
this.files.push(entry);
return true;
};
Plugin.prototype.make = function(compilation, callback) {
async.forEach(this.files.slice(), function(file, callback) {
var entry = file;
if (this.wrapMocha) {
entry = require.resolve("./mocha-env-loader") + "!" + entry;
}
var dep = new SingleEntryDependency(entry);
compilation.addEntry("", dep, path.relative(this.basePath, file).replace(/\\/g, "/"), function() {
// If the module fails because of an File not found error, remove the test file
if(dep.module && dep.module.error && dep.module.error.error && dep.module.error.error.code === "ENOENT") {
this.files = this.files.filter(function(f) {
return file !== f;
});
this.middleware.invalidate();
}
callback();
}.bind(this));
}.bind(this), callback);
};
Plugin.prototype.readFile = function(file, callback) {
var middleware = this.middleware;
var optionsCount = this.optionsCount;
function doRead() {
if(optionsCount > 1) {
async.times(optionsCount, function(idx, callback) {
middleware.fileSystem.readFile("/_karma_webpack_/" + idx + "/" + file.replace(/\\/g, "/"), callback);
}, function(err, contents) {
if(err) return callback(err);
contents = contents.reduce(function(arr, x) {
if(!arr) return [x];
arr.push(new Buffer("\n"), x);
return arr;
}, null);
callback(null, Buffer.concat(contents));
});
} else {
middleware.fileSystem.readFile("/_karma_webpack_/" + file.replace(/\\/g, "/"), callback);
}
}
if(!this.waiting)
doRead();
else
// Retry to read once a build is finished
// do it on process.nextTick to catch changes while building
this.waiting.push(process.nextTick.bind(process, this.readFile.bind(this, file, callback)));
};
function createPreprocesor(/* config.basePath */basePath, webpackPlugin) {
return function(content, file, done) {
if (webpackPlugin.addFile(file.path)) {
// recompile as we have an asset that we have not seen before
webpackPlugin.middleware.invalidate();
}
// read blocks until bundle is done
webpackPlugin.readFile(path.relative(basePath, file.path), function(err, content) {
if (err) {
throw err;
}
done(err, content && content.toString());
});
};
}
module.exports = {
"webpackPlugin": ["type", Plugin],
"preprocessor:webpack": ["factory", createPreprocesor]
};