forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel-compiler.js
85 lines (71 loc) · 2.67 KB
/
babel-compiler.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
/**
* A compiler that can be instantiated with features and used inside
* Plugin.registerCompiler
* @param {Object} extraFeatures The same object that getDefaultOptions takes
*/
BabelCompiler = function BabelCompiler(extraFeatures) {
Babel.validateExtraFeatures(extraFeatures);
this.extraFeatures = extraFeatures;
};
var BCp = BabelCompiler.prototype;
var excludedFileExtensionPattern = /\.es5\.js$/i;
BCp.processFilesForTarget = function (inputFiles) {
var self = this;
inputFiles.forEach(function (inputFile) {
var source = inputFile.getContentsAsString();
var inputFilePath = inputFile.getPathInPackage();
var outputFilePath = inputFile.getPathInPackage();
var fileOptions = inputFile.getFileOptions();
var toBeAdded = {
sourcePath: inputFilePath,
path: outputFilePath,
data: source,
hash: inputFile.getSourceHash(),
sourceMap: null,
bare: !! fileOptions.bare
};
// If you need to exclude a specific file within a package from Babel
// compilation, pass the { transpile: false } options to api.addFiles
// when you add that file.
if (fileOptions.transpile !== false &&
// If you need to exclude a specific file within an app from Babel
// compilation, give it the following file extension: .es5.js
! excludedFileExtensionPattern.test(inputFilePath)) {
var targetCouldBeInternetExplorer8 =
inputFile.getArch() === "web.browser";
self.extraFeatures = self.extraFeatures || {};
if (! self.extraFeatures.hasOwnProperty("jscript")) {
// Perform some additional transformations to improve
// compatibility in older browsers (e.g. wrapping named function
// expressions, per http://kiro.me/blog/nfe_dilemma.html).
self.extraFeatures.jscript = targetCouldBeInternetExplorer8;
}
var babelOptions = Babel.getDefaultOptions(self.extraFeatures);
babelOptions.sourceMap = true;
babelOptions.filename = inputFilePath;
babelOptions.sourceFileName = "/" + inputFilePath;
babelOptions.sourceMapName = "/" + outputFilePath + ".map";
try {
var result = Babel.compile(source, babelOptions);
} catch (e) {
if (e.loc) {
inputFile.error({
message: e.message,
sourcePath: inputFilePath,
line: e.loc.line,
column: e.loc.column,
});
return;
}
throw e;
}
toBeAdded.data = result.code;
toBeAdded.hash = result.hash;
toBeAdded.sourceMap = result.map;
}
inputFile.addJavaScript(toBeAdded);
});
};
BCp.setDiskCacheDirectory = function (cacheDir) {
Babel.setCacheDir(cacheDir);
};