index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. let babel;
  3. try {
  4. babel = require("@babel/core");
  5. } catch (err) {
  6. if (err.code === "MODULE_NOT_FOUND") {
  7. err.message += "\n babel-loader@9 requires Babel 7.12+ (the package '@babel/core'). " + "If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.";
  8. }
  9. throw err;
  10. }
  11. // Since we've got the reverse bridge package at @babel/core@6.x, give
  12. // people useful feedback if they try to use it alongside babel-loader.
  13. if (/^6\./.test(babel.version)) {
  14. throw new Error("\n babel-loader@9 will not work with the '@babel/core@6' bridge package. " + "If you want to use Babel 6.x, install 'babel-loader@7'.");
  15. }
  16. const {
  17. version
  18. } = require("../package.json");
  19. const cache = require("./cache");
  20. const transform = require("./transform");
  21. const injectCaller = require("./injectCaller");
  22. const schema = require("./schema");
  23. const {
  24. isAbsolute
  25. } = require("path");
  26. const validateOptions = require("schema-utils").validate;
  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. async function loader(source, inputSourceMap, overrides) {
  43. const filename = this.resourcePath;
  44. let loaderOptions = this.getOptions();
  45. validateOptions(schema, loaderOptions, {
  46. name: "Babel loader"
  47. });
  48. if (loaderOptions.customize != null) {
  49. if (typeof loaderOptions.customize !== "string") {
  50. throw new Error("Customized loaders must be implemented as standalone modules.");
  51. }
  52. if (!isAbsolute(loaderOptions.customize)) {
  53. 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.");
  54. }
  55. if (overrides) {
  56. throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
  57. }
  58. let override = require(loaderOptions.customize);
  59. if (override.__esModule) override = override.default;
  60. if (typeof override !== "function") {
  61. throw new Error("Custom overrides must be functions.");
  62. }
  63. overrides = override(babel);
  64. }
  65. let customOptions;
  66. if (overrides && overrides.customOptions) {
  67. const result = await overrides.customOptions.call(this, loaderOptions, {
  68. source,
  69. map: inputSourceMap
  70. });
  71. customOptions = result.custom;
  72. loaderOptions = result.loader;
  73. }
  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. }
  81. // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
  82. // users may safely use either one alongside our default use of
  83. // 'this.sourceMap' below without getting error about conflicting aliases.
  84. if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
  85. loaderOptions = Object.assign({}, loaderOptions, {
  86. sourceMaps: loaderOptions.sourceMap
  87. });
  88. delete loaderOptions.sourceMap;
  89. }
  90. const programmaticOptions = Object.assign({}, loaderOptions, {
  91. filename,
  92. inputSourceMap: inputSourceMap || loaderOptions.inputSourceMap,
  93. // Set the default sourcemap behavior based on Webpack's mapping flag,
  94. // but allow users to override if they want.
  95. sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
  96. // Ensure that Webpack will get a full absolute path in the sourcemap
  97. // so that it can properly map the module back to its internal cached
  98. // modules.
  99. sourceFileName: filename
  100. });
  101. // Remove loader related options
  102. delete programmaticOptions.customize;
  103. delete programmaticOptions.cacheDirectory;
  104. delete programmaticOptions.cacheIdentifier;
  105. delete programmaticOptions.cacheCompression;
  106. delete programmaticOptions.metadataSubscribers;
  107. const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
  108. if (config) {
  109. let options = config.options;
  110. if (overrides && overrides.config) {
  111. options = await overrides.config.call(this, config, {
  112. source,
  113. map: inputSourceMap,
  114. customOptions
  115. });
  116. }
  117. if (options.sourceMaps === "inline") {
  118. // Babel has this weird behavior where if you set "inline", we
  119. // inline the sourcemap, and set 'result.map = null'. This results
  120. // in bad behavior from Babel since the maps get put into the code,
  121. // which Webpack does not expect, and because the map we return to
  122. // Webpack is null, which is also bad. To avoid that, we override the
  123. // behavior here so "inline" just behaves like 'true'.
  124. options.sourceMaps = true;
  125. }
  126. const {
  127. cacheDirectory = null,
  128. cacheIdentifier = JSON.stringify({
  129. options,
  130. "@babel/core": transform.version,
  131. "@babel/loader": version
  132. }),
  133. cacheCompression = true,
  134. metadataSubscribers = []
  135. } = loaderOptions;
  136. let result;
  137. if (cacheDirectory) {
  138. result = await cache({
  139. source,
  140. options,
  141. transform,
  142. cacheDirectory,
  143. cacheIdentifier,
  144. cacheCompression
  145. });
  146. } else {
  147. result = await transform(source, options);
  148. }
  149. config.files.forEach(configFile => this.addDependency(configFile));
  150. if (result) {
  151. if (overrides && overrides.result) {
  152. result = await 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. externalDependencies
  165. } = result;
  166. externalDependencies == null ? void 0 : externalDependencies.forEach(dep => this.addDependency(dep));
  167. metadataSubscribers.forEach(subscriber => {
  168. subscribe(subscriber, metadata, this);
  169. });
  170. return [code, map];
  171. }
  172. }
  173. // If the file was ignored, pass through the original content.
  174. return [source, inputSourceMap];
  175. }