Projekt

Obecné

Profil

Stáhnout (4.87 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
var babel = require("babel-core");
4
var loaderUtils = require("loader-utils");
5
var path = require("path");
6
var cache = require("./fs-cache.js");
7
var exists = require("./utils/exists");
8
var relative = require("./utils/relative");
9
var read = require("./utils/read");
10
var resolveRc = require("./resolve-rc.js");
11
var pkg = require("../package.json");
12
var fs = require("fs");
13

    
14
/**
15
 * Error thrown by Babel formatted to conform to Webpack reporting.
16
 */
17
function BabelLoaderError(name, message, codeFrame, hideStack, error) {
18
  Error.call(this);
19

    
20
  this.name = "BabelLoaderError";
21
  this.message = formatMessage(name, message, codeFrame);
22
  this.hideStack = hideStack;
23
  this.error = error;
24

    
25
  Error.captureStackTrace(this, BabelLoaderError);
26
}
27

    
28
BabelLoaderError.prototype = Object.create(Error.prototype);
29
BabelLoaderError.prototype.constructor = BabelLoaderError;
30

    
31
var STRIP_FILENAME_RE = /^[^:]+: /;
32

    
33
var formatMessage = function formatMessage(name, message, codeFrame) {
34
  return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n";
35
};
36

    
37
var transpile = function transpile(source, options) {
38
  var forceEnv = options.forceEnv;
39
  var tmpEnv = void 0;
40

    
41
  delete options.forceEnv;
42

    
43
  if (forceEnv) {
44
    tmpEnv = process.env.BABEL_ENV;
45
    process.env.BABEL_ENV = forceEnv;
46
  }
47

    
48
  var result = void 0;
49
  try {
50
    result = babel.transform(source, options);
51
  } catch (error) {
52
    if (forceEnv) restoreBabelEnv(tmpEnv);
53
    if (error.message && error.codeFrame) {
54
      var message = error.message;
55
      var name = void 0;
56
      var hideStack = void 0;
57
      if (error instanceof SyntaxError) {
58
        message = message.replace(STRIP_FILENAME_RE, "");
59
        name = "SyntaxError";
60
        hideStack = true;
61
      } else if (error instanceof TypeError) {
62
        message = message.replace(STRIP_FILENAME_RE, "");
63
        hideStack = true;
64
      }
65
      throw new BabelLoaderError(name, message, error.codeFrame, hideStack, error);
66
    } else {
67
      throw error;
68
    }
69
  }
70
  var code = result.code;
71
  var map = result.map;
72
  var metadata = result.metadata;
73

    
74
  if (map && (!map.sourcesContent || !map.sourcesContent.length)) {
75
    map.sourcesContent = [source];
76
  }
77

    
78
  if (forceEnv) restoreBabelEnv(tmpEnv);
79

    
80
  return {
81
    code: code,
82
    map: map,
83
    metadata: metadata
84
  };
85
};
86

    
87
function restoreBabelEnv(prevValue) {
88
  if (prevValue === undefined) {
89
    delete process.env.BABEL_ENV;
90
  } else {
91
    process.env.BABEL_ENV = prevValue;
92
  }
93
}
94

    
95
function passMetadata(s, context, metadata) {
96
  if (context[s]) {
97
    context[s](metadata);
98
  }
99
}
100

    
101
module.exports = function (source, inputSourceMap) {
102
  var _this = this;
103

    
104
  var filename = this.resourcePath;
105

    
106
  // Handle options
107
  var loaderOptions = loaderUtils.getOptions(this) || {};
108
  var fileSystem = this.fs ? this.fs : fs;
109
  var babelrcPath = null;
110
  if (loaderOptions.babelrc !== false) {
111
    babelrcPath = typeof loaderOptions.babelrc === "string" && exists(fileSystem, loaderOptions.babelrc) ? loaderOptions.babelrc : resolveRc(fileSystem, path.dirname(filename));
112
  }
113

    
114
  if (babelrcPath) {
115
    this.addDependency(babelrcPath);
116
  }
117

    
118
  var defaultOptions = {
119
    metadataSubscribers: [],
120
    inputSourceMap: inputSourceMap,
121
    sourceRoot: process.cwd(),
122
    filename: filename,
123
    cacheIdentifier: JSON.stringify({
124
      "babel-loader": pkg.version,
125
      "babel-core": babel.version,
126
      babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
127
      env: loaderOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development"
128
    })
129
  };
130

    
131
  var options = Object.assign({}, defaultOptions, loaderOptions);
132

    
133
  if (loaderOptions.sourceMap === undefined) {
134
    options.sourceMap = this.sourceMap;
135
  }
136

    
137
  if (options.sourceFileName === undefined) {
138
    options.sourceFileName = relative(options.sourceRoot, options.filename);
139
  }
140

    
141
  var cacheDirectory = options.cacheDirectory;
142
  var cacheIdentifier = options.cacheIdentifier;
143
  var metadataSubscribers = options.metadataSubscribers;
144

    
145
  delete options.cacheDirectory;
146
  delete options.cacheIdentifier;
147
  delete options.metadataSubscribers;
148

    
149
  if (cacheDirectory) {
150
    var callback = this.async();
151
    return cache({
152
      directory: cacheDirectory,
153
      identifier: cacheIdentifier,
154
      source: source,
155
      options: options,
156
      transform: transpile
157
    }, function (err) {
158
      var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
159
          code = _ref.code,
160
          map = _ref.map,
161
          metadata = _ref.metadata;
162

    
163
      if (err) return callback(err);
164

    
165
      metadataSubscribers.forEach(function (s) {
166
        return passMetadata(s, _this, metadata);
167
      });
168

    
169
      return callback(null, code, map);
170
    });
171
  }
172

    
173
  var _transpile = transpile(source, options),
174
      code = _transpile.code,
175
      map = _transpile.map,
176
      metadata = _transpile.metadata;
177

    
178
  metadataSubscribers.forEach(function (s) {
179
    return passMetadata(s, _this, metadata);
180
  });
181

    
182
  this.callback(null, code, map);
183
};
(2-2/3)