-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
76 lines (67 loc) · 2.08 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
'use strict';
const async = require('async');
const fs = require('fs-extra');
const hashBuilder = require('oc-hash-builder');
const path = require('path');
const MemoryFS = require('memory-fs');
const {
compiler,
configurator: webpackConfigurator
} = require('./lib/oc-webpack');
module.exports = (options, callback) => {
const { componentPackage, production, publishPath } = options;
const serverFileName = componentPackage.oc.files.data;
const serverPath = path.join(options.componentPath, serverFileName);
const publishFileName = options.publishFileName || 'server.js';
const stats = options.verbose ? 'verbose' : 'errors-only';
const dependencies = componentPackage.dependencies || {};
const config = webpackConfigurator({
dependencies,
production,
publishFileName,
serverPath,
stats
});
async.waterfall(
[
next => compiler(config, next),
(data, next) => {
const basePath = path.join(serverPath, '../build');
const getCompiled = fileName =>
new MemoryFS(data).readFileSync(`${basePath}/${fileName}`, 'UTF8');
return fs.ensureDir(publishPath, err => {
if (err) return next(err);
const result = { 'server.js': getCompiled('server.js') };
if (!production) {
try {
result['server.js.map'] = getCompiled('server.js.map');
} catch (e) {
// skip sourcemap if it doesn't exist
}
}
next(null, result);
});
},
(compiledFiles, next) =>
async.eachOf(
compiledFiles,
(fileContent, fileName, next) =>
fs.writeFile(path.join(publishPath, fileName), fileContent, next),
err =>
next(
err,
err
? null
: {
type: 'node.js',
hashKey: hashBuilder.fromString(
compiledFiles[publishFileName]
),
src: publishFileName
}
)
)
],
callback
);
};