1 | var debug = require('debug')('compiled')
|
2 | var la = require('lazy-ass')
|
3 | var is = require('check-more-types')
|
4 | var getConfig = require('./get-config')
|
5 | var utils = require('./utils')
|
6 | var path = require('path')
|
7 | var saveFile = require('fs').writeFileSync
|
8 |
|
9 | function roll (inputFilename, outputFilename) {
|
10 | var rollup = require('rollup')
|
11 |
|
12 | var firstLine = utils.getFirstLine(inputFilename)
|
13 | var hasHashBang = utils.isHashbang(firstLine)
|
14 |
|
15 | if (hasHashBang) {
|
16 | utils.removeFirstLine(inputFilename)
|
17 | }
|
18 |
|
19 | function restoreHashBang () {
|
20 | if (hasHashBang) {
|
21 | utils.restoreFirstLine(inputFilename, firstLine)
|
22 | utils.restoreFirstLine(outputFilename, firstLine + '\n')
|
23 | debug('restored hashbang in %s and added to %s',
|
24 | inputFilename, outputFilename)
|
25 | }
|
26 | }
|
27 |
|
28 | return rollup.rollup({
|
29 | entry: inputFilename
|
30 | }).then(function (bundle) {
|
31 | return bundle.write({
|
32 | format: 'cjs',
|
33 | dest: outputFilename
|
34 | })
|
35 | .then(restoreHashBang, function (err) {
|
36 | restoreHashBang()
|
37 | throw err
|
38 | })
|
39 | .then(function () {
|
40 | utils.finishWithEndline(outputFilename)
|
41 | })
|
42 | .then(function () {
|
43 | debug('saved bundle', outputFilename)
|
44 | return outputFilename
|
45 | })
|
46 | })
|
47 | }
|
48 |
|
49 | function findUsedES6 (config, outputFilename, filename) {
|
50 | var testify = require('es-feature-tests/testify')
|
51 | debug('scanning for es features', filename)
|
52 | var output = testify.scan({
|
53 | files: filename,
|
54 | output: 'json',
|
55 | enable: []
|
56 | })
|
57 | la(is.array(output), 'expected list of features', output)
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | if (is.array(config.features)) {
|
65 | debug('user required these features', config.features)
|
66 | config.features.filter(is.unemptyString).forEach(function (userListedFeature) {
|
67 | if (output.indexOf(userListedFeature) === -1) {
|
68 | output.push(userListedFeature)
|
69 | }
|
70 | })
|
71 | }
|
72 |
|
73 | output = output.sort()
|
74 | debug('used ES features', output)
|
75 |
|
76 | saveFile(outputFilename, JSON.stringify(output, null, 2), 'utf-8')
|
77 | debug('saved file with found es features', outputFilename)
|
78 | }
|
79 |
|
80 | function buildBundle (config, inputFilename, toDir, name) {
|
81 | la(is.unemptyString(inputFilename), 'missing input filename', inputFilename)
|
82 | la(is.unemptyString(toDir), 'missing output dir name', toDir)
|
83 | la(is.unemptyString(name), 'missing bundle name', name)
|
84 |
|
85 | var outputFilename = path.join(toDir, utils.builtName(name))
|
86 | debug('building %s from %s to %s', name, inputFilename, outputFilename)
|
87 | var featuresFilename = path.join(toDir, utils.featuresName(name))
|
88 |
|
89 | return roll(inputFilename, outputFilename)
|
90 | .then(findUsedES6.bind(null, config, featuresFilename))
|
91 | .catch(function (err) {
|
92 | console.error('problem building', inputFilename)
|
93 | console.error(err.message)
|
94 | console.error(err.stack)
|
95 | process.exit(-1)
|
96 | })
|
97 | }
|
98 |
|
99 | function build (options) {
|
100 | var config = is.object(options) ? options : getConfig()
|
101 | debug('found %d files to build', config.files.length)
|
102 | var promises = config.files.map(function (filename) {
|
103 | return buildBundle(config, filename, config.dir, utils.bundleName(filename))
|
104 | })
|
105 | return Promise.all(promises)
|
106 | }
|
107 |
|
108 | module.exports = build
|