index.js 8.5 KB

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