1 |
|
2 |
|
3 | import deepClone from "lodash/lang/cloneDeep";
|
4 | import sourceMapSupport from "source-map-support";
|
5 | import * as registerCache from "./cache";
|
6 | import extend from "lodash/object/extend";
|
7 | import * as babel from "babel-core";
|
8 | import each from "lodash/collection/each";
|
9 | import { util, OptionManager } from "babel-core";
|
10 | import fs from "fs";
|
11 | import path from "path";
|
12 |
|
13 | sourceMapSupport.install({
|
14 | handleUncaughtExceptions: false,
|
15 | retrieveSourceMap(source) {
|
16 | let map = maps && maps[source];
|
17 | if (map) {
|
18 | return {
|
19 | url: null,
|
20 | map: map
|
21 | };
|
22 | } else {
|
23 | return null;
|
24 | }
|
25 | }
|
26 | });
|
27 |
|
28 | registerCache.load();
|
29 | let cache = registerCache.get();
|
30 |
|
31 | let transformOpts = {};
|
32 |
|
33 | let ignore;
|
34 | let only;
|
35 |
|
36 | let oldHandlers = {};
|
37 | let maps = {};
|
38 |
|
39 | let cwd = process.cwd();
|
40 |
|
41 | function getRelativePath(filename){
|
42 | return path.relative(cwd, filename);
|
43 | }
|
44 |
|
45 | function mtime(filename) {
|
46 | return +fs.statSync(filename).mtime;
|
47 | }
|
48 |
|
49 | function compile(filename) {
|
50 | let result;
|
51 |
|
52 | let optsManager = new OptionManager;
|
53 |
|
54 |
|
55 | optsManager.mergeOptions(deepClone(transformOpts), "base", null, path.dirname(filename));
|
56 |
|
57 | let opts = optsManager.init({ filename });
|
58 |
|
59 | let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
|
60 |
|
61 | let env = process.env.BABEL_ENV || process.env.NODE_ENV;
|
62 | if (env) cacheKey += `:${env}`;
|
63 |
|
64 | if (cache) {
|
65 | let cached = cache[cacheKey];
|
66 | if (cached && cached.mtime === mtime(filename)) {
|
67 | result = cached;
|
68 | }
|
69 | }
|
70 |
|
71 | if (!result) {
|
72 | result = babel.transformFileSync(filename, extend(opts, {
|
73 |
|
74 |
|
75 | babelrc: false,
|
76 | sourceMap: "both",
|
77 | ast: false
|
78 | }));
|
79 | }
|
80 |
|
81 | if (cache) {
|
82 | cache[cacheKey] = result;
|
83 | result.mtime = mtime(filename);
|
84 | }
|
85 |
|
86 | maps[filename] = result.map;
|
87 |
|
88 | return result.code;
|
89 | }
|
90 |
|
91 | function shouldIgnore(filename) {
|
92 | if (!ignore && !only) {
|
93 | return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
|
94 | } else {
|
95 | return util.shouldIgnore(filename, ignore || [], only);
|
96 | }
|
97 | }
|
98 |
|
99 | function loader(m, filename) {
|
100 | m._compile(compile(filename), filename);
|
101 | }
|
102 |
|
103 | function registerExtension(ext) {
|
104 | let old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
|
105 |
|
106 | require.extensions[ext] = function (m, filename) {
|
107 | if (shouldIgnore(filename)) {
|
108 | old(m, filename);
|
109 | } else {
|
110 | loader(m, filename, old);
|
111 | }
|
112 | };
|
113 | }
|
114 |
|
115 | function hookExtensions(_exts) {
|
116 | each(oldHandlers, function (old, ext) {
|
117 | if (old === undefined) {
|
118 | delete require.extensions[ext];
|
119 | } else {
|
120 | require.extensions[ext] = old;
|
121 | }
|
122 | });
|
123 |
|
124 | oldHandlers = {};
|
125 |
|
126 | each(_exts, function (ext) {
|
127 | oldHandlers[ext] = require.extensions[ext];
|
128 | registerExtension(ext);
|
129 | });
|
130 | }
|
131 |
|
132 | hookExtensions(util.canCompile.EXTENSIONS);
|
133 |
|
134 | export default function (opts?: Object = {}) {
|
135 | if (opts.only != null) only = util.arrayify(opts.only, util.regexify);
|
136 | if (opts.ignore != null) ignore = util.arrayify(opts.ignore, util.regexify);
|
137 |
|
138 | if (opts.extensions) hookExtensions(util.arrayify(opts.extensions));
|
139 |
|
140 | if (opts.cache === false) cache = null;
|
141 |
|
142 | delete opts.extensions;
|
143 | delete opts.ignore;
|
144 | delete opts.cache;
|
145 | delete opts.only;
|
146 |
|
147 | extend(transformOpts, opts);
|
148 | }
|