index.js 8.6 KB

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