-
-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathindex.js
133 lines (113 loc) · 3.64 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
'use strict';
var assign = require('object-assign');
var babel = require('babel-core');
var loaderUtils = require('loader-utils');
var cache = require('./lib/fs-cache.js');
var exists = require('./lib/helpers/exists')();
var read = require('./lib/helpers/read')();
var resolveRc = require('./lib/resolve-rc.js');
var pkg = require('./package.json');
var path = require('path');
/**
* 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;
var STRIP_FILENAME_RE = /^[^:]+: /;
var formatMessage = function(name, message, codeFrame) {
return (name ? name + ': ' : '') + message + '\n\n' + codeFrame + '\n';
};
var transpile = function(source, options) {
var result;
try {
result = babel.transform(source, options);
} catch (error) {
if (error.message && error.codeFrame) {
var message = error.message;
var name;
var 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;
}
}
var code = result.code;
var map = result.map;
if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
map.sourcesContent = [source];
}
return {
code: code,
map: map,
};
};
module.exports = function(source, inputSourceMap) {
var result = {};
// Handle filenames (#106)
var webpackRemainingChain = loaderUtils.getRemainingRequest(this).split('!');
var filename = webpackRemainingChain[webpackRemainingChain.length - 1];
// Handle options
var globalOptions = this.options.babel || {};
var loaderOptions = loaderUtils.parseQuery(this.query);
var userOptions = assign({}, globalOptions, loaderOptions);
var defaultOptions = {
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: process.env.BABEL_ENV || process.env.NODE_ENV,
}),
};
var options = assign({}, defaultOptions, userOptions);
if (userOptions.sourceMap === undefined) {
options.sourceMap = this.sourceMap;
}
if (options.sourceFileName === undefined) {
options.sourceFileName = path.relative(
options.sourceRoot,
options.filename
);
}
var cacheDirectory = options.cacheDirectory;
var cacheIdentifier = options.cacheIdentifier;
delete options.cacheDirectory;
delete options.cacheIdentifier;
this.cacheable();
if (cacheDirectory) {
var callback = this.async();
return cache({
directory: cacheDirectory,
identifier: cacheIdentifier,
source: source,
options: options,
transform: transpile,
}, function(err, result) {
if (err) { return callback(err); }
return callback(null, result.code, result.map);
});
}
result = transpile(source, options);
this.callback(null, result.code, result.map);
};