-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·75 lines (73 loc) · 2.75 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
"use strict";
var babel = require("babel-core");
var shouldIgnoreByBabel = require("./lib/babelrc-util").shouldIgnoreByBabel;
var fs = require("fs");
var minimatch = require("minimatch");
var extend = require("xtend");
var createEspowerPlugin = require("babel-plugin-espower/create");
var sourceMapSupport = require("source-map-support");
var extensions = require.extensions,
originalLoader = extensions[".js"];
function espowerBabel(options) {
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
var pattern = options.cwd + separator + options.pattern;
var babelrc = options.babelrc || {};
var extension = options.extension || ".js";
// extend babel option
babelrc = extend(babelrc, {
babelrc: false,
sourceMap: "both",
ast: false
});
// attach espower option
var espoweredBabelrc = useEspower(babelrc);
var sourceMaps = {};
// https://github.com/evanw/node-source-map-support
// `sourceMaps` is the cached map object of transform by babel
sourceMapSupport.install({
handleUncaughtExceptions: false,
retrieveSourceMap: function (source) {
var map = sourceMaps && sourceMaps[source];
if (map) {
return {
url: null,
map: map
};
} else {
return null;
}
}
});
function useEspower(babelOptions) {
babelOptions.plugins = babelOptions.plugins || [];
var espowerPluginExists = babelOptions.plugins.some(function (plugin) {
var pluginName = typeof plugin === "string" ? plugin : plugin.key;
return pluginName === "babel-plugin-espower";
});
if (!espowerPluginExists) {
babelOptions.plugins.push(createEspowerPlugin(babel, options.espowerOptions));
}
return babelOptions;
}
extensions[extension] = function (localModule, filepath) {
var result;
// https://babeljs.io/docs/usage/api/
babelrc.filename = filepath;
// transform the other files
if (shouldIgnoreByBabel(filepath, babelrc)) {
originalLoader(localModule, filepath);
return
}
// transform test files using espower's `pattern` value
if (minimatch(filepath, pattern)) {
result = babel.transform(fs.readFileSync(filepath, "utf-8"), espoweredBabelrc);
sourceMaps[filepath] = result.map;
localModule._compile(result.code, filepath);
} else {
result = babel.transform(fs.readFileSync(filepath, "utf-8"), babelrc);
sourceMaps[filepath] = result.map;
localModule._compile(result.code, filepath);
}
};
}
module.exports = espowerBabel;