forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcompile-cache.js
259 lines (219 loc) · 7.06 KB
/
compile-cache.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'use strict';
// Atom's compile-cache when installing or updating packages, called by apm's Node-js
const path = require('path');
const fs = require('fs-plus');
const sourceMapSupport = require('@atom/source-map-support');
const PackageTranspilationRegistry = require('./package-transpilation-registry');
let CSON = null;
const packageTranspilationRegistry = new PackageTranspilationRegistry();
const COMPILERS = {
'.js': packageTranspilationRegistry.wrapTranspiler(require('./babel')),
'.ts': packageTranspilationRegistry.wrapTranspiler(require('./typescript')),
'.tsx': packageTranspilationRegistry.wrapTranspiler(require('./typescript')),
'.coffee': packageTranspilationRegistry.wrapTranspiler(
require('./coffee-script')
)
};
exports.addTranspilerConfigForPath = function(
packagePath,
packageName,
packageMeta,
config
) {
packagePath = fs.realpathSync(packagePath);
packageTranspilationRegistry.addTranspilerConfigForPath(
packagePath,
packageName,
packageMeta,
config
);
};
exports.removeTranspilerConfigForPath = function(packagePath) {
packagePath = fs.realpathSync(packagePath);
packageTranspilationRegistry.removeTranspilerConfigForPath(packagePath);
};
const cacheStats = {};
let cacheDirectory = null;
exports.setAtomHomeDirectory = function(atomHome) {
let cacheDir = path.join(atomHome, 'compile-cache');
if (
process.env.USER === 'root' &&
process.env.SUDO_USER &&
process.env.SUDO_USER !== process.env.USER
) {
cacheDir = path.join(cacheDir, 'root');
}
this.setCacheDirectory(cacheDir);
};
exports.setCacheDirectory = function(directory) {
cacheDirectory = directory;
};
exports.getCacheDirectory = function() {
return cacheDirectory;
};
exports.addPathToCache = function(filePath, atomHome) {
this.setAtomHomeDirectory(atomHome);
const extension = path.extname(filePath);
if (extension === '.cson') {
if (!CSON) {
CSON = require('season');
CSON.setCacheDir(this.getCacheDirectory());
}
return CSON.readFileSync(filePath);
} else {
const compiler = COMPILERS[extension];
if (compiler) {
return compileFileAtPath(compiler, filePath, extension);
}
}
};
exports.getCacheStats = function() {
return cacheStats;
};
exports.resetCacheStats = function() {
Object.keys(COMPILERS).forEach(function(extension) {
cacheStats[extension] = {
hits: 0,
misses: 0
};
});
};
function compileFileAtPath(compiler, filePath, extension) {
const sourceCode = fs.readFileSync(filePath, 'utf8');
if (compiler.shouldCompile(sourceCode, filePath)) {
const cachePath = compiler.getCachePath(sourceCode, filePath);
let compiledCode = readCachedJavaScript(cachePath);
if (compiledCode != null) {
cacheStats[extension].hits++;
} else {
cacheStats[extension].misses++;
compiledCode = compiler.compile(sourceCode, filePath);
writeCachedJavaScript(cachePath, compiledCode);
}
return compiledCode;
}
return sourceCode;
}
function readCachedJavaScript(relativeCachePath) {
const cachePath = path.join(cacheDirectory, relativeCachePath);
if (fs.isFileSync(cachePath)) {
try {
return fs.readFileSync(cachePath, 'utf8');
} catch (error) {}
}
return null;
}
function writeCachedJavaScript(relativeCachePath, code) {
const cachePath = path.join(cacheDirectory, relativeCachePath);
fs.writeFileSync(cachePath, code, 'utf8');
}
const INLINE_SOURCE_MAP_REGEXP = /\/\/[#@]\s*sourceMappingURL=([^'"\n]+)\s*$/gm;
exports.install = function(resourcesPath, nodeRequire) {
const snapshotSourceMapConsumer = {
originalPositionFor({ line, column }) {
const { relativePath, row } = snapshotResult.translateSnapshotRow(line);
return {
column,
line: row,
source: path.join(resourcesPath, 'app', 'static', relativePath),
name: null
};
}
};
sourceMapSupport.install({
handleUncaughtExceptions: false,
// Most of this logic is the same as the default implementation in the
// source-map-support module, but we've overridden it to read the javascript
// code from our cache directory.
retrieveSourceMap: function(filePath) {
if (filePath === '<embedded>') {
return { map: snapshotSourceMapConsumer };
}
if (!cacheDirectory || !fs.isFileSync(filePath)) {
return null;
}
try {
var sourceCode = fs.readFileSync(filePath, 'utf8');
} catch (error) {
console.warn('Error reading source file', error.stack);
return null;
}
let compiler = COMPILERS[path.extname(filePath)];
if (!compiler) compiler = COMPILERS['.js'];
try {
var fileData = readCachedJavaScript(
compiler.getCachePath(sourceCode, filePath)
);
} catch (error) {
console.warn('Error reading compiled file', error.stack);
return null;
}
if (fileData == null) {
return null;
}
let match, lastMatch;
INLINE_SOURCE_MAP_REGEXP.lastIndex = 0;
while ((match = INLINE_SOURCE_MAP_REGEXP.exec(fileData))) {
lastMatch = match;
}
if (lastMatch == null) {
return null;
}
const sourceMappingURL = lastMatch[1];
const rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
try {
var sourceMap = JSON.parse(Buffer.from(rawData, 'base64'));
} catch (error) {
console.warn('Error parsing source map', error.stack);
return null;
}
return {
map: sourceMap,
url: null
};
}
});
const prepareStackTraceWithSourceMapping = Error.prepareStackTrace;
var prepareStackTrace = prepareStackTraceWithSourceMapping;
function prepareStackTraceWithRawStackAssignment(error, frames) {
if (error.rawStack) {
// avoid infinite recursion
return prepareStackTraceWithSourceMapping(error, frames);
} else {
error.rawStack = frames;
return prepareStackTrace(error, frames);
}
}
Error.stackTraceLimit = 30;
Object.defineProperty(Error, 'prepareStackTrace', {
get: function() {
return prepareStackTraceWithRawStackAssignment;
},
set: function(newValue) {
prepareStackTrace = newValue;
process.nextTick(function() {
prepareStackTrace = prepareStackTraceWithSourceMapping;
});
}
});
// eslint-disable-next-line no-extend-native
Error.prototype.getRawStack = function() {
// Access this.stack to ensure prepareStackTrace has been run on this error
// because it assigns this.rawStack as a side-effect
this.stack; // eslint-disable-line no-unused-expressions
return this.rawStack;
};
Object.keys(COMPILERS).forEach(function(extension) {
const compiler = COMPILERS[extension];
Object.defineProperty(nodeRequire.extensions, extension, {
enumerable: true,
writable: false,
value: function(module, filePath) {
const code = compileFileAtPath(compiler, filePath, extension);
return module._compile(code, filePath);
}
});
});
};
exports.supportedExtensions = Object.keys(COMPILERS);
exports.resetCacheStats();