index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  3. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  4. let babel;
  5. try {
  6. babel = require("@babel/core");
  7. } catch (err) {
  8. if (err.code === "MODULE_NOT_FOUND") {
  9. err.message += "\n babel-loader@8 requires Babel 7.x (the package '@babel/core'). " + "If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.";
  10. }
  11. throw err;
  12. } // Since we've got the reverse bridge package at @babel/core@6.x, give
  13. // people useful feedback if they try to use it alongside babel-loader.
  14. if (/^6\./.test(babel.version)) {
  15. throw new Error("\n babel-loader@8 will not work with the '@babel/core@6' bridge package. " + "If you want to use Babel 6.x, install 'babel-loader@7'.");
  16. }
  17. const pkg = require("../package.json");
  18. const cache = require("./cache");
  19. const transform = require("./transform");
  20. const injectCaller = require("./injectCaller");
  21. const path = require("path");
  22. const loaderUtils = require("loader-utils");
  23. function subscribe(subscriber, metadata, context) {
  24. if (context[subscriber]) {
  25. context[subscriber](metadata);
  26. }
  27. }
  28. module.exports = makeLoader();
  29. module.exports.custom = makeLoader;
  30. function makeLoader(callback) {
  31. const overrides = callback ? callback(babel) : undefined;
  32. return function (source, inputSourceMap) {
  33. // Make the loader async
  34. const callback = this.async();
  35. loader.call(this, source, inputSourceMap, overrides).then(args => callback(null, ...args), err => callback(err));
  36. };
  37. }
  38. function loader(_x, _x2, _x3) {
  39. return _loader.apply(this, arguments);
  40. }
  41. function _loader() {
  42. _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
  43. const filename = this.resourcePath;
  44. let loaderOptions = loaderUtils.getOptions(this) || {};
  45. if (loaderOptions.customize != null) {
  46. if (typeof loaderOptions.customize !== "string") {
  47. throw new Error("Customized loaders must be implemented as standalone modules.");
  48. }
  49. if (!path.isAbsolute(loaderOptions.customize)) {
  50. throw new Error("Customized loaders must be passed as absolute paths, since " + "babel-loader has no way to know what they would be relative to.");
  51. }
  52. if (overrides) {
  53. throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
  54. }
  55. let override = require(loaderOptions.customize);
  56. if (override.__esModule) override = override.default;
  57. if (typeof override !== "function") {
  58. throw new Error("Custom overrides must be functions.");
  59. }
  60. overrides = override(babel);
  61. }
  62. let customOptions;
  63. if (overrides && overrides.customOptions) {
  64. const result = yield overrides.customOptions.call(this, loaderOptions, {
  65. source,
  66. map: inputSourceMap
  67. });
  68. customOptions = result.custom;
  69. loaderOptions = result.loader;
  70. } // Deprecation handling
  71. if ("forceEnv" in loaderOptions) {
  72. console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
  73. }
  74. if (typeof loaderOptions.babelrc === "string") {
  75. console.warn("The option `babelrc` should not be set to a string anymore in the babel-loader config. " + "Please update your configuration and set `babelrc` to true or false.\n" + "If you want to specify a specific babel config file to inherit config from " + "please use the `extends` option.\nFor more information about this options see " + "https://babeljs.io/docs/core-packages/#options");
  76. } // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
  77. // users may safely use either one alongside our default use of
  78. // 'this.sourceMap' below without getting error about conflicting aliases.
  79. if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
  80. loaderOptions = Object.assign({}, loaderOptions, {
  81. sourceMaps: loaderOptions.sourceMap
  82. });
  83. delete loaderOptions.sourceMap;
  84. }
  85. const programmaticOptions = Object.assign({}, loaderOptions, {
  86. filename,
  87. inputSourceMap: inputSourceMap || undefined,
  88. // Set the default sourcemap behavior based on Webpack's mapping flag,
  89. // but allow users to override if they want.
  90. sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
  91. // Ensure that Webpack will get a full absolute path in the sourcemap
  92. // so that it can properly map the module back to its internal cached
  93. // modules.
  94. sourceFileName: filename
  95. }); // Remove loader related options
  96. delete programmaticOptions.customize;
  97. delete programmaticOptions.cacheDirectory;
  98. delete programmaticOptions.cacheIdentifier;
  99. delete programmaticOptions.cacheCompression;
  100. delete programmaticOptions.metadataSubscribers;
  101. if (!babel.loadPartialConfig) {
  102. throw new Error(`babel-loader ^8.0.0-beta.3 requires @babel/core@7.0.0-beta.41, but ` + `you appear to be using "${babel.version}". Either update your ` + `@babel/core version, or pin you babel-loader version to 8.0.0-beta.2`);
  103. }
  104. const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
  105. if (config) {
  106. let options = config.options;
  107. if (overrides && overrides.config) {
  108. options = yield overrides.config.call(this, config, {
  109. source,
  110. map: inputSourceMap,
  111. customOptions
  112. });
  113. }
  114. if (options.sourceMaps === "inline") {
  115. // Babel has this weird behavior where if you set "inline", we
  116. // inline the sourcemap, and set 'result.map = null'. This results
  117. // in bad behavior from Babel since the maps get put into the code,
  118. // which Webpack does not expect, and because the map we return to
  119. // Webpack is null, which is also bad. To avoid that, we override the
  120. // behavior here so "inline" just behaves like 'true'.
  121. options.sourceMaps = true;
  122. }
  123. const {
  124. cacheDirectory = null,
  125. cacheIdentifier = JSON.stringify({
  126. options,
  127. "@babel/core": transform.version,
  128. "@babel/loader": pkg.version
  129. }),
  130. cacheCompression = true,
  131. metadataSubscribers = []
  132. } = loaderOptions;
  133. let result;
  134. if (cacheDirectory) {
  135. result = yield cache({
  136. source,
  137. options,
  138. transform,
  139. cacheDirectory,
  140. cacheIdentifier,
  141. cacheCompression
  142. });
  143. } else {
  144. result = yield transform(source, options);
  145. } // TODO: Babel should really provide the full list of config files that
  146. // were used so that this can also handle files loaded with 'extends'.
  147. if (typeof config.babelrc === "string") {
  148. this.addDependency(config.babelrc);
  149. }
  150. if (result) {
  151. if (overrides && overrides.result) {
  152. result = yield overrides.result.call(this, result, {
  153. source,
  154. map: inputSourceMap,
  155. customOptions,
  156. config,
  157. options
  158. });
  159. }
  160. const {
  161. code,
  162. map,
  163. metadata
  164. } = result;
  165. metadataSubscribers.forEach(subscriber => {
  166. subscribe(subscriber, metadata, this);
  167. });
  168. return [code, map];
  169. }
  170. } // If the file was ignored, pass through the original content.
  171. return [source, inputSourceMap];
  172. });
  173. return _loader.apply(this, arguments);
  174. }