1 | var _ = require('lodash');
|
2 | var serialize = require('serialize-javascript');
|
3 |
|
4 | var Compiler = require('./Compiler');
|
5 | var Cache = require('./plugins/Cache');
|
6 | var Clean = require('./plugins/Clean');
|
7 | var LiveReload = require('./plugins/LiveReload');
|
8 | var Log = require('./plugins/Log');
|
9 | var convertOptions = require('./convertOptions');
|
10 |
|
11 | module.exports = function(options, callback) {
|
12 | options = convertOptions(options);
|
13 |
|
14 | var compiler = new Compiler(options);
|
15 |
|
16 | var plugins = options.plugins || [];
|
17 |
|
18 | options.cache && plugins.unshift(
|
19 | new Cache(
|
20 | serialize(_.pick(options, ['output', 'cache', 'hashLength', 'hashConnector', 'domain', 'plugins', 'modules', 'resolve'])),
|
21 | options.cache,
|
22 | options.context,
|
23 | options.output
|
24 | )
|
25 | );
|
26 | plugins.push(new Clean());
|
27 | plugins.push(new Log(options.output));
|
28 | options.watch && plugins.push(new LiveReload());
|
29 |
|
30 | plugins.forEach(function(plugin) {
|
31 | compiler.apply(plugin);
|
32 | });
|
33 |
|
34 | if (callback) {
|
35 | if (options.watch) {
|
36 | compiler.watch(callback);
|
37 | } else {
|
38 | compiler.run(callback);
|
39 | }
|
40 | }
|
41 |
|
42 | return compiler;
|
43 | };
|