index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. let babel;
  2. try {
  3. babel = require("@babel/core");
  4. } catch (err) {
  5. if (err.code === "MODULE_NOT_FOUND") {
  6. 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'.";
  7. }
  8. throw err;
  9. }
  10. // Since we've got the reverse bridge package at @babel/core@6.x, give
  11. // people useful feedback if they try to use it alongside babel-loader.
  12. if (/^6\./.test(babel.version)) {
  13. 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'.");
  14. }
  15. const {
  16. version
  17. } = require("../package.json");
  18. const cache = require("./cache");
  19. const transform = require("./transform");
  20. const injectCaller = require("./injectCaller");
  21. const schema = require("./schema");
  22. const {
  23. isAbsolute
  24. } = require("path");
  25. const validateOptions = require("schema-utils").validate;
  26. function subscribe(subscriber, metadata, context) {
  27. if (context[subscriber]) {
  28. context[subscriber](metadata);
  29. }
  30. }
  31. module.exports = makeLoader();
  32. module.exports.custom = makeLoader;
  33. function makeLoader(callback) {
  34. const overrides = callback ? callback(babel) : undefined;
  35. return function (source, inputSourceMap) {
  36. // Make the loader async
  37. const callback = this.async();
  38. loader.call(this, source, inputSourceMap, overrides).then(args => callback(null, ...args), err => callback(err));
  39. };
  40. }
  41. async function loader(source, inputSourceMap, overrides) {
  42. const filename = this.resourcePath;
  43. let loaderOptions = this.getOptions();
  44. validateOptions(schema, loaderOptions, {
  45. name: "Babel loader"
  46. });
  47. if (loaderOptions.customize != null) {
  48. if (typeof loaderOptions.customize !== "string") {
  49. throw new Error("Customized loaders must be implemented as standalone modules.");
  50. }
  51. if (!isAbsolute(loaderOptions.customize)) {
  52. 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.");
  53. }
  54. if (overrides) {
  55. throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
  56. }
  57. let override = require(loaderOptions.customize);
  58. if (override.__esModule) override = override.default;
  59. if (typeof override !== "function") {
  60. throw new Error("Custom overrides must be functions.");
  61. }
  62. overrides = override(babel);
  63. }
  64. let customOptions;
  65. if (overrides && overrides.customOptions) {
  66. const result = await overrides.customOptions.call(this, loaderOptions, {
  67. source,
  68. map: inputSourceMap
  69. });
  70. customOptions = result.custom;
  71. loaderOptions = result.loader;
  72. }
  73. // Deprecation handling
  74. if ("forceEnv" in loaderOptions) {
  75. console.warn("The option `forceEnv` has been removed in favor of `envName` in Babel 7.");
  76. }
  77. if (typeof loaderOptions.babelrc === "string") {
  78. 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");
  79. }
  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 || loaderOptions.inputSourceMap,
  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. });
  100. // Remove loader related options
  101. delete programmaticOptions.customize;
  102. delete programmaticOptions.cacheDirectory;
  103. delete programmaticOptions.cacheIdentifier;
  104. delete programmaticOptions.cacheCompression;
  105. delete programmaticOptions.metadataSubscribers;
  106. const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
  107. if (config) {
  108. let options = config.options;
  109. if (overrides && overrides.config) {
  110. options = await overrides.config.call(this, config, {
  111. source,
  112. map: inputSourceMap,
  113. customOptions
  114. });
  115. }
  116. if (options.sourceMaps === "inline") {
  117. // Babel has this weird behavior where if you set "inline", we
  118. // inline the sourcemap, and set 'result.map = null'. This results
  119. // in bad behavior from Babel since the maps get put into the code,
  120. // which Webpack does not expect, and because the map we return to
  121. // Webpack is null, which is also bad. To avoid that, we override the
  122. // behavior here so "inline" just behaves like 'true'.
  123. options.sourceMaps = true;
  124. }
  125. const {
  126. cacheDirectory = null,
  127. cacheIdentifier = JSON.stringify({
  128. options,
  129. "@babel/core": transform.version,
  130. "@babel/loader": version
  131. }),
  132. cacheCompression = true,
  133. metadataSubscribers = []
  134. } = loaderOptions;
  135. let result;
  136. if (cacheDirectory) {
  137. result = await cache({
  138. source,
  139. options,
  140. transform,
  141. cacheDirectory,
  142. cacheIdentifier,
  143. cacheCompression
  144. });
  145. } else {
  146. result = await transform(source, options);
  147. }
  148. config.files.forEach(configFile => this.addDependency(configFile));
  149. if (result) {
  150. if (overrides && overrides.result) {
  151. result = await overrides.result.call(this, result, {
  152. source,
  153. map: inputSourceMap,
  154. customOptions,
  155. config,
  156. options
  157. });
  158. }
  159. const {
  160. code,
  161. map,
  162. metadata,
  163. externalDependencies
  164. } = result;
  165. externalDependencies == null ? void 0 : externalDependencies.forEach(dep => this.addDependency(dep));
  166. metadataSubscribers.forEach(subscriber => {
  167. subscribe(subscriber, metadata, this);
  168. });
  169. return [code, map];
  170. }
  171. }
  172. // If the file was ignored, pass through the original content.
  173. return [source, inputSourceMap];
  174. }