1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
8 |
|
9 | var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
10 |
|
11 | var _renderer = require('./renderer');
|
12 |
|
13 | var _renderer2 = _interopRequireDefault(_renderer);
|
14 |
|
15 | var _configReader = require('./config-reader');
|
16 |
|
17 | var _configReader2 = _interopRequireDefault(_configReader);
|
18 |
|
19 | var _prodBuilder = require('./builder/prod-builder');
|
20 |
|
21 | var _prodBuilder2 = _interopRequireDefault(_prodBuilder);
|
22 |
|
23 | var _devBuilder = require('./builder/dev-builder');
|
24 |
|
25 | var _devBuilder2 = _interopRequireDefault(_devBuilder);
|
26 |
|
27 | var _middlewareComposer = require('./middleware-composer');
|
28 |
|
29 | var _middlewareComposer2 = _interopRequireDefault(_middlewareComposer);
|
30 |
|
31 | var _logger = require('./utils/logger');
|
32 |
|
33 | var _logger2 = _interopRequireDefault(_logger);
|
34 |
|
35 | var _events = require('events');
|
36 |
|
37 | var _events2 = _interopRequireDefault(_events);
|
38 |
|
39 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
40 |
|
41 | class LavasCore extends _events2.default {
|
42 | constructor(cwd = process.cwd()) {
|
43 | super();
|
44 | this.cwd = cwd;
|
45 | }
|
46 |
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | init(env, isInBuild, options = {}) {
|
56 | var _this = this;
|
57 |
|
58 | return (0, _asyncToGenerator3.default)(function* () {
|
59 | _this.env = env;
|
60 | _this.isProd = _this.env === 'production';
|
61 | _this.configReader = new _configReader2.default(_this.cwd, _this.env, options.config);
|
62 |
|
63 | if (!process.env.NODE_ENV) {
|
64 | process.env.NODE_ENV = env;
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 | if (isInBuild) {
|
72 |
|
73 | _this.config = yield _this.configReader.read();
|
74 | } else {
|
75 |
|
76 | _this.config = yield _this.configReader.readConfigFile();
|
77 | }
|
78 |
|
79 | _this.middlewareComposer = new _middlewareComposer2.default(_this);
|
80 | _this.renderer = new _renderer2.default(_this);
|
81 | _this.builder = _this.isProd ? new _prodBuilder2.default(_this) : new _devBuilder2.default(_this);
|
82 |
|
83 |
|
84 | _this.koaMiddleware = _this.middlewareComposer.koa.bind(_this.middlewareComposer);
|
85 | _this.expressMiddleware = _this.middlewareComposer.express.bind(_this.middlewareComposer);
|
86 |
|
87 |
|
88 | _this.render = _this.renderer.render.bind(_this.renderer);
|
89 |
|
90 | if (!_this.isProd) {
|
91 |
|
92 | _this.on('start-rebuild', (0, _asyncToGenerator3.default)(function* () {
|
93 |
|
94 | let newConfig = yield _this.configReader.read();
|
95 |
|
96 |
|
97 | _this.builder.init(newConfig);
|
98 |
|
99 |
|
100 | _this.middlewareComposer.reset(newConfig);
|
101 |
|
102 |
|
103 | _this.emit('rebuild');
|
104 | }));
|
105 | }
|
106 | })();
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 | build() {
|
114 | var _this2 = this;
|
115 |
|
116 | return (0, _asyncToGenerator3.default)(function* () {
|
117 | try {
|
118 | yield _this2.builder.build();
|
119 | } catch (e) {
|
120 | console.error(e);
|
121 | _logger2.default.error('build', `${_this2.env} build failed.`);
|
122 | }
|
123 | })();
|
124 | }
|
125 |
|
126 | |
127 |
|
128 |
|
129 |
|
130 | runAfterBuild() {
|
131 | var _this3 = this;
|
132 |
|
133 | return (0, _asyncToGenerator3.default)(function* () {
|
134 |
|
135 | yield _this3.renderer.createWithBundle();
|
136 | })();
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 | close() {
|
144 | var _this4 = this;
|
145 |
|
146 | return (0, _asyncToGenerator3.default)(function* () {
|
147 | yield _this4.builder.close();
|
148 | console.log('[Lavas] lavas closed.');
|
149 | })();
|
150 | }
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | ignore(req) {
|
158 | req.lavasIgnoreFlag = true;
|
159 | }
|
160 | }
|
161 | exports.default = LavasCore; |
162 |
|
163 |
|
164 |
|
165 |
|
166 | module.exports = exports['default']; |
\ | No newline at end of file |