file.js
4.7 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";
var _stringify = require("babel-runtime/core-js/json/stringify");
var _stringify2 = _interopRequireDefault(_stringify);
var _set = require("babel-runtime/core-js/set");
var _set2 = _interopRequireDefault(_set);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var convertSourceMap = require("convert-source-map");
var sourceMap = require("source-map");
var slash = require("slash");
var path = require("path");
var util = require("./util");
var fs = require("fs");
module.exports = function (commander, filenames, opts) {
if (commander.sourceMaps === "inline") {
opts.sourceMaps = true;
}
var results = [];
var buildResult = function buildResult() {
var map = new sourceMap.SourceMapGenerator({
file: path.basename(commander.outFile || "") || "stdout",
sourceRoot: opts.sourceRoot
});
var code = "";
var offset = 0;
results.forEach(function (result) {
code += result.code + "\n";
if (result.map) {
var consumer = new sourceMap.SourceMapConsumer(result.map);
var sources = new _set2.default();
consumer.eachMapping(function (mapping) {
if (mapping.source != null) sources.add(mapping.source);
map.addMapping({
generated: {
line: mapping.generatedLine + offset,
column: mapping.generatedColumn
},
source: mapping.source,
original: mapping.source == null ? null : {
line: mapping.originalLine,
column: mapping.originalColumn
}
});
});
sources.forEach(function (source) {
var content = consumer.sourceContentFor(source, true);
if (content !== null) {
map.setSourceContent(source, content);
}
});
offset = code.split("\n").length - 1;
}
});
if (commander.sourceMaps === "inline" || !commander.outFile && commander.sourceMaps) {
code += "\n" + convertSourceMap.fromObject(map).toComment();
}
return {
map: map,
code: code
};
};
var output = function output() {
var result = buildResult();
if (commander.outFile) {
if (commander.sourceMaps && commander.sourceMaps !== "inline") {
var mapLoc = commander.outFile + ".map";
result.code = util.addSourceMappingUrl(result.code, mapLoc);
fs.writeFileSync(mapLoc, (0, _stringify2.default)(result.map));
}
fs.writeFileSync(commander.outFile, result.code);
} else {
process.stdout.write(result.code + "\n");
}
};
var stdin = function stdin() {
var code = "";
process.stdin.setEncoding("utf8");
process.stdin.on("readable", function () {
var chunk = process.stdin.read();
if (chunk !== null) code += chunk;
});
process.stdin.on("end", function () {
results.push(util.transform(commander.filename, code, {
sourceFileName: "stdin"
}));
output();
});
};
var walk = function walk() {
var _filenames = [];
results = [];
filenames.forEach(function (filename) {
if (!fs.existsSync(filename)) return;
var stat = fs.statSync(filename);
if (stat.isDirectory()) {
var dirname = filename;
util.readdirFilter(filename).forEach(function (filename) {
_filenames.push(path.join(dirname, filename));
});
} else {
_filenames.push(filename);
}
});
_filenames.forEach(function (filename) {
if (util.shouldIgnore(filename)) return;
var sourceFilename = filename;
if (commander.outFile) {
sourceFilename = path.relative(path.dirname(commander.outFile), sourceFilename);
}
sourceFilename = slash(sourceFilename);
var data = util.compile(filename, {
sourceFileName: sourceFilename
});
if (data.ignored) return;
results.push(data);
});
output();
};
var files = function files() {
if (!commander.skipInitialBuild) {
walk();
}
if (commander.watch) {
var chokidar = util.requireChokidar();
chokidar.watch(filenames, {
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50,
pollInterval: 10
}
}).on("all", function (type, filename) {
if (util.shouldIgnore(filename) || !util.canCompile(filename, commander.extensions)) return;
if (type === "add" || type === "change") {
util.log(type + " " + filename);
try {
walk();
} catch (err) {
console.error(err.stack);
}
}
});
}
};
if (filenames.length) {
files();
} else {
stdin();
}
};