-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (88 loc) · 3.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var transpiler = require('es6-module-transpiler');
var AMDFormatter = require('es6-module-transpiler-amd-formatter');
var Container = transpiler.Container;
var FileResolver = transpiler.FileResolver;
var recast = require('recast');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var helpers = require('broccoli-kitchen-sink-helpers');
var Writer = require('broccoli-writer');
var jsStringEscape = require('js-string-escape');
var util = require('util');
module.exports = ES6Concat;
function ES6Concat(inputTree, options) {
if (!(this instanceof ES6Concat)) {
return new ES6Concat(inputTree, options);
}
options || (options = {});
this.inputTree = inputTree;
if ('wrapInEval' in options) {
this.wrapInEval = options.wrapInEval;
} else {
this.wrapInEval = true;
}
this.inputFiles = options.inputFiles;
this.outputFile = options.outputFile;
this.legacyFilesToAppend = options.legacyFilesToAppend;
this.loaderFile = options.loaderFile;
this.ignoredModules = options.ignoredModules || [];
}
util.inherits(ES6Concat, Writer);
ES6Concat.prototype.write = function (readTree, destDir) {
var self = this;
var output = [];
return readTree(this.inputTree).then(function (srcDir) {
var container = new Container({
resolvers: [new FileResolver([srcDir])],
formatter: new AMDFormatter()
});
if (self.loaderFile) {
addLegacyFile(self.loaderFile);
}
var inputFiles = helpers.multiGlob(self.inputFiles, {cwd: srcDir});
for (var i = 0; i < inputFiles.length; i++) {
var fileName = inputFiles[i];
if (self.ignoredModules.indexOf(fileName.slice(0, -3)) < 0) {
var module = container.getModule(fileName);
}
}
var builtModules = container.convert();
for (i = 0; i < builtModules.length; i++) {
var code;
if (self.wrapInEval) {
code = wrapInEval(recast.print(builtModules[i]).code, builtModules[i].filename);
} else {
code = recast.print(builtModules[i]).code;
}
output.push(code);
}
if (self.legacyFilesToAppend && self.legacyFilesToAppend.length) {
var legacyFiles = helpers.multiGlob(self.legacyFilesToAppend, {cwd: srcDir});
for (i = 0; i < legacyFiles.length; i++) {
addLegacyFile(legacyFiles[i]);
}
}
helpers.assertAbsolutePaths([self.outputFile]);
mkdirp.sync(path.join(destDir, path.dirname(self.outputFile)));
fs.writeFileSync(path.join(destDir, self.outputFile), output.join('\n;'));
function addLegacyFile (filePath) {
var fileContents = fs.readFileSync(srcDir + '/' + filePath, { encoding: 'utf8' });
if (self.wrapInEval) {
fileContents = wrapInEval(fileContents, filePath);
}
cacheObject = {
output: fileContents
};
output.push(cacheObject.output);
}
});
};
function wrapInEval (fileContents, fileName) {
// Should pull out copyright comment headers
// Eventually we want source maps instead of sourceURL
return 'eval("' +
jsStringEscape(fileContents) +
'//# sourceURL=' + jsStringEscape(fileName) +
'");\n';
}