You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.0 KiB

4 years ago
  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 {
  18. version
  19. } = require("../package.json");
  20. const cache = require("./cache");
  21. const transform = require("./transform");
  22. const injectCaller = require("./injectCaller");
  23. const {
  24. isAbsolute
  25. } = require("path");
  26. const loaderUtils = require("loader-utils");
  27. function subscribe(subscriber, metadata, context) {
  28. if (context[subscriber]) {
  29. context[subscriber](metadata);
  30. }
  31. }
  32. module.exports = makeLoader();
  33. module.exports.custom = makeLoader;
  34. function makeLoader(callback) {
  35. const overrides = callback ? callback(babel) : undefined;
  36. return function (source, inputSourceMap) {
  37. // Make the loader async
  38. const callback = this.async();
  39. loader.call(this, source, inputSourceMap, overrides).then(args => callback(null, ...args), err => callback(err));
  40. };
  41. }
  42. function loader(_x, _x2, _x3) {
  43. return _loader.apply(this, arguments);
  44. }
  45. function _loader() {
  46. _loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
  47. const filename = this.resourcePath;
  48. let loaderOptions = loaderUtils.getOptions(this) || {};
  49. if (loaderOptions.customize != null) {
  50. if (typeof loaderOptions.customize !== "string") {
  51. throw new Error("Customized loaders must be implemented as standalone modules.");
  52. }
  53. if (!isAbsolute(loaderOptions.customize)) {
  54. 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.");
  55. }
  56. if (overrides) {
  57. throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
  58. }
  59. let override = require(loaderOptions.customize);
  60. if (override.__esModule) override = override.default;
  61. if (typeof override !== "function") {
  62. throw new Error("Custom overrides must be functions.");
  63. }
  64. overrides = override(babel);
  65. }
  66. let customOptions;
  67. if (overrides && overrides.customOptions) {
  68. const result = yield overrides.customOptions.call(this, loaderOptions, {
  69. source,
  70. map: inputSourceMap
  71. });
  72. customOptions = result.custom;
  73. loaderOptions = result.loader;
  74. } // Deprecation handling
  75. if ("forceEnv" in loaderOptions) {
  76. console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
  77. }
  78. if (typeof loaderOptions.babelrc === "string") {
  79. 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");
  80. } // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
  81. // users may safely use either one alongside our default use of
  82. // 'this.sourceMap' below without getting error about conflicting aliases.
  83. if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
  84. loaderOptions = Object.assign({}, loaderOptions, {
  85. sourceMaps: loaderOptions.sourceMap
  86. });
  87. delete loaderOptions.sourceMap;
  88. }
  89. const programmaticOptions = Object.assign({}, loaderOptions, {
  90. filename,
  91. inputSourceMap: inputSourceMap || undefined,
  92. // Set the default sourcemap behavior based on Webpack's mapping flag,
  93. // but allow users to override if they want.
  94. sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
  95. // Ensure that Webpack will get a full absolute path in the sourcemap
  96. // so that it can properly map the module back to its internal cached
  97. // modules.
  98. sourceFileName: filename
  99. }); // Remove loader related options
  100. delete programmaticOptions.customize;
  101. delete programmaticOptions.cacheDirectory;
  102. delete programmaticOptions.cacheIdentifier;
  103. delete programmaticOptions.cacheCompression;
  104. delete programmaticOptions.metadataSubscribers;
  105. if (!babel.loadPartialConfig) {
  106. 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`);
  107. }
  108. const config = babel.loadPartialConfig(injectCaller(programmaticOptions));
  109. if (config) {
  110. let options = config.options;
  111. if (overrides && overrides.config) {
  112. options = yield overrides.config.call(this, config, {
  113. source,
  114. map: inputSourceMap,
  115. customOptions
  116. });
  117. }
  118. if (options.sourceMaps === "inline") {
  119. // Babel has this weird behavior where if you set "inline", we
  120. // inline the sourcemap, and set 'result.map = null'. This results
  121. // in bad behavior from Babel since the maps get put into the code,
  122. // which Webpack does not expect, and because the map we return to
  123. // Webpack is null, which is also bad. To avoid that, we override the
  124. // behavior here so "inline" just behaves like 'true'.
  125. options.sourceMaps = true;
  126. }
  127. const {
  128. cacheDirectory = null,
  129. cacheIdentifier = JSON.stringify({
  130. options,
  131. "@babel/core": transform.version,
  132. "@babel/loader": version
  133. }),
  134. cacheCompression = true,
  135. metadataSubscribers = []
  136. } = loaderOptions;
  137. let result;
  138. if (cacheDirectory) {
  139. result = yield cache({
  140. source,
  141. options,
  142. transform,
  143. cacheDirectory,
  144. cacheIdentifier,
  145. cacheCompression
  146. });
  147. } else {
  148. result = yield transform(source, options);
  149. } // TODO: Babel should really provide the full list of config files that
  150. // were used so that this can also handle files loaded with 'extends'.
  151. if (typeof config.babelrc === "string") {
  152. this.addDependency(config.babelrc);
  153. }
  154. if (result) {
  155. if (overrides && overrides.result) {
  156. result = yield overrides.result.call(this, result, {
  157. source,
  158. map: inputSourceMap,
  159. customOptions,
  160. config,
  161. options
  162. });
  163. }
  164. const {
  165. code,
  166. map,
  167. metadata
  168. } = result;
  169. metadataSubscribers.forEach(subscriber => {
  170. subscribe(subscriber, metadata, this);
  171. });
  172. return [code, map];
  173. }
  174. } // If the file was ignored, pass through the original content.
  175. return [source, inputSourceMap];
  176. });
  177. return _loader.apply(this, arguments);
  178. }