1 | var fs = require('fs');
|
2 | var assert = require('assert');
|
3 | var applyLoaders = require('./applyLoaders');
|
4 | var serializeError = require('./serializeError');
|
5 | var SourceMapSerializer = require('./SourceMapSerializer');
|
6 |
|
7 | function HappyWorker(params) {
|
8 | this._compiler = params.compiler;
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | HappyWorker.prototype.compile = function(params, done) {
|
20 | assert(typeof params.loaderContext.resourcePath === 'string',
|
21 | "ArgumentError: expected params.sourcePath to contain path to the source file."
|
22 | );
|
23 |
|
24 | assert(typeof params.compiledPath === 'string',
|
25 | "ArgumentError: expected params.compiledPath to contain path to the compiled file."
|
26 | );
|
27 |
|
28 | assert(Array.isArray(params.loaders),
|
29 | "ArgumentError: expected params.loaders to contain a list of loaders."
|
30 | );
|
31 |
|
32 | applyLoaders({
|
33 | compiler: this._compiler,
|
34 | loaders: params.loaders,
|
35 | loaderContext: params.loaderContext,
|
36 | }, params.loaderContext.sourceCode, params.loaderContext.sourceMap, function(err, source, sourceMap) {
|
37 | var compiledPath = params.compiledPath;
|
38 | var success = false;
|
39 |
|
40 | if (err) {
|
41 | console.error(err);
|
42 | fs.writeFileSync(compiledPath, serializeError(err), 'utf-8');
|
43 | }
|
44 | else {
|
45 | fs.writeFileSync(compiledPath, source);
|
46 | fs.writeFileSync(compiledPath + '.map', SourceMapSerializer.serialize(sourceMap));
|
47 |
|
48 | success = true;
|
49 | }
|
50 |
|
51 | done({
|
52 | sourcePath: params.loaderContext.resourcePath,
|
53 | compiledPath: compiledPath,
|
54 | success: success
|
55 | });
|
56 | });
|
57 | };
|
58 |
|
59 | module.exports = HappyWorker;
|