/
index.js
161 lines (138 loc) · 4.5 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
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
const assign = require("object-assign");
const babel = require("babel-core");
const loaderUtils = require("loader-utils");
const path = require("path");
const cache = require("./fs-cache.js");
const exists = require("./utils/exists")();
const relative = require("./utils/relative");
const read = require("./utils/read")();
const resolveRc = require("./resolve-rc.js");
const pkg = require("./../package.json");
/**
* Error thrown by Babel formatted to conform to Webpack reporting.
*/
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
Error.call(this);
Error.captureStackTrace(this, BabelLoaderError);
this.name = "BabelLoaderError";
this.message = formatMessage(name, message, codeFrame);
this.hideStack = hideStack;
this.error = error;
}
BabelLoaderError.prototype = Object.create(Error.prototype);
BabelLoaderError.prototype.constructor = BabelLoaderError;
const STRIP_FILENAME_RE = /^[^:]+: /;
const formatMessage = function(name, message, codeFrame) {
return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n";
};
const transpile = function(source, options) {
const forceEnv = options.forceEnv;
let tmpEnv;
delete options.forceEnv;
if (forceEnv) {
tmpEnv = process.env.BABEL_ENV;
process.env.BABEL_ENV = forceEnv;
}
let result;
try {
result = babel.transform(source, options);
} catch (error) {
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
if (error.message && error.codeFrame) {
let message = error.message;
let name;
let hideStack;
if (error instanceof SyntaxError) {
message = message.replace(STRIP_FILENAME_RE, "");
name = "SyntaxError";
hideStack = true;
} else if (error instanceof TypeError) {
message = message.replace(STRIP_FILENAME_RE, "");
hideStack = true;
}
throw new BabelLoaderError(
name, message, error.codeFrame, hideStack, error);
} else {
throw error;
}
}
const code = result.code;
const map = result.map;
const metadata = result.metadata;
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
map.sourcesContent = [source];
}
if (forceEnv) process.env.BABEL_ENV = tmpEnv;
return {
code: code,
map: map,
metadata: metadata,
};
};
function passMetadata(s, context, metadata) {
if (context[s]) {
context[s](metadata);
}
}
module.exports = function(source, inputSourceMap) {
// Handle filenames (#106)
const webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
const filename = webpackRemainingChain[webpackRemainingChain.length - 1];
// Handle options
const globalOptions = this.options.babel || {};
const loaderOptions = loaderUtils.parseQuery(this.query);
const userOptions = assign({}, globalOptions, loaderOptions);
const defaultOptions = {
metadataSubscribers: [],
inputSourceMap: inputSourceMap,
sourceRoot: process.cwd(),
filename: filename,
cacheIdentifier: JSON.stringify({
"babel-loader": pkg.version,
"babel-core": babel.version,
babelrc: exists(userOptions.babelrc) ?
read(userOptions.babelrc) :
resolveRc(path.dirname(filename)),
env: userOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development",
}),
};
const options = assign({}, defaultOptions, userOptions);
if (userOptions.sourceMap === undefined) {
options.sourceMap = this.sourceMap;
}
if (options.sourceFileName === undefined) {
options.sourceFileName = relative(
options.sourceRoot,
options.filename
);
}
const cacheDirectory = options.cacheDirectory;
const cacheIdentifier = options.cacheIdentifier;
const metadataSubscribers = options.metadataSubscribers;
delete options.cacheDirectory;
delete options.cacheIdentifier;
delete options.metadataSubscribers;
this.cacheable();
const context = this;
if (cacheDirectory) {
const callback = this.async();
return cache({
directory: cacheDirectory,
identifier: cacheIdentifier,
source: source,
options: options,
transform: transpile,
}, function(err, result) {
if (err) { return callback(err); }
metadataSubscribers.forEach(function (s) {
passMetadata(s, context, result.metadata);
});
return callback(null, result.code, result.map);
});
}
const result = transpile(source, options);
metadataSubscribers.forEach(function (s) {
passMetadata(s, context, result.metadata);
});
this.callback(null, result.code, result.map);
};