[79a0317] | 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 |
|
---|
| 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 | const logger = typeof this.getLogger === "function" ? this.getLogger("babel-loader") : {
|
---|
| 45 | debug: () => {}
|
---|
| 46 | };
|
---|
| 47 | let loaderOptions = this.getOptions();
|
---|
| 48 | validateOptions(schema, loaderOptions, {
|
---|
| 49 | name: "Babel loader"
|
---|
| 50 | });
|
---|
| 51 | if (loaderOptions.customize != null) {
|
---|
| 52 | if (typeof loaderOptions.customize !== "string") {
|
---|
| 53 | throw new Error("Customized loaders must be implemented as standalone modules.");
|
---|
| 54 | }
|
---|
| 55 | if (!isAbsolute(loaderOptions.customize)) {
|
---|
| 56 | 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.");
|
---|
| 57 | }
|
---|
| 58 | if (overrides) {
|
---|
| 59 | throw new Error("babel-loader's 'customize' option is not available when already " + "using a customized babel-loader wrapper.");
|
---|
| 60 | }
|
---|
| 61 | logger.debug(`loading customize override: '${loaderOptions.customize}'`);
|
---|
| 62 | let override = require(loaderOptions.customize);
|
---|
| 63 | if (override.__esModule) override = override.default;
|
---|
| 64 | if (typeof override !== "function") {
|
---|
| 65 | throw new Error("Custom overrides must be functions.");
|
---|
| 66 | }
|
---|
| 67 | logger.debug("applying customize override to @babel/core");
|
---|
| 68 | overrides = override(babel);
|
---|
| 69 | }
|
---|
| 70 | let customOptions;
|
---|
| 71 | if (overrides && overrides.customOptions) {
|
---|
| 72 | logger.debug("applying overrides customOptions() to loader options");
|
---|
| 73 | const result = await overrides.customOptions.call(this, loaderOptions, {
|
---|
| 74 | source,
|
---|
| 75 | map: inputSourceMap
|
---|
| 76 | });
|
---|
| 77 | customOptions = result.custom;
|
---|
| 78 | loaderOptions = result.loader;
|
---|
| 79 | }
|
---|
| 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 | logger.debug("normalizing loader options");
|
---|
| 89 | // Standardize on 'sourceMaps' as the key passed through to Webpack, so that
|
---|
| 90 | // users may safely use either one alongside our default use of
|
---|
| 91 | // 'this.sourceMap' below without getting error about conflicting aliases.
|
---|
| 92 | if (Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMap") && !Object.prototype.hasOwnProperty.call(loaderOptions, "sourceMaps")) {
|
---|
| 93 | loaderOptions = Object.assign({}, loaderOptions, {
|
---|
| 94 | sourceMaps: loaderOptions.sourceMap
|
---|
| 95 | });
|
---|
| 96 | delete loaderOptions.sourceMap;
|
---|
| 97 | }
|
---|
| 98 | const programmaticOptions = Object.assign({}, loaderOptions, {
|
---|
| 99 | filename,
|
---|
| 100 | inputSourceMap: inputSourceMap || loaderOptions.inputSourceMap,
|
---|
| 101 | // Set the default sourcemap behavior based on Webpack's mapping flag,
|
---|
| 102 | // but allow users to override if they want.
|
---|
| 103 | sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
|
---|
| 104 | // Ensure that Webpack will get a full absolute path in the sourcemap
|
---|
| 105 | // so that it can properly map the module back to its internal cached
|
---|
| 106 | // modules.
|
---|
| 107 | sourceFileName: filename
|
---|
| 108 | });
|
---|
| 109 | // Remove loader related options
|
---|
| 110 | delete programmaticOptions.customize;
|
---|
| 111 | delete programmaticOptions.cacheDirectory;
|
---|
| 112 | delete programmaticOptions.cacheIdentifier;
|
---|
| 113 | delete programmaticOptions.cacheCompression;
|
---|
| 114 | delete programmaticOptions.metadataSubscribers;
|
---|
| 115 | logger.debug("resolving Babel configs");
|
---|
| 116 | const config = await babel.loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
|
---|
| 117 | if (config) {
|
---|
| 118 | let options = config.options;
|
---|
| 119 | if (overrides && overrides.config) {
|
---|
| 120 | logger.debug("applying overrides config() to Babel config");
|
---|
| 121 | options = await overrides.config.call(this, config, {
|
---|
| 122 | source,
|
---|
| 123 | map: inputSourceMap,
|
---|
| 124 | customOptions
|
---|
| 125 | });
|
---|
| 126 | }
|
---|
| 127 | if (options.sourceMaps === "inline") {
|
---|
| 128 | // Babel has this weird behavior where if you set "inline", we
|
---|
| 129 | // inline the sourcemap, and set 'result.map = null'. This results
|
---|
| 130 | // in bad behavior from Babel since the maps get put into the code,
|
---|
| 131 | // which Webpack does not expect, and because the map we return to
|
---|
| 132 | // Webpack is null, which is also bad. To avoid that, we override the
|
---|
| 133 | // behavior here so "inline" just behaves like 'true'.
|
---|
| 134 | options.sourceMaps = true;
|
---|
| 135 | }
|
---|
| 136 | const {
|
---|
| 137 | cacheDirectory = null,
|
---|
| 138 | cacheIdentifier = JSON.stringify({
|
---|
| 139 | options,
|
---|
| 140 | "@babel/core": transform.version,
|
---|
| 141 | "@babel/loader": version
|
---|
| 142 | }),
|
---|
| 143 | cacheCompression = true,
|
---|
| 144 | metadataSubscribers = []
|
---|
| 145 | } = loaderOptions;
|
---|
| 146 | let result;
|
---|
| 147 | if (cacheDirectory) {
|
---|
| 148 | logger.debug("cache is enabled");
|
---|
| 149 | result = await cache({
|
---|
| 150 | source,
|
---|
| 151 | options,
|
---|
| 152 | transform,
|
---|
| 153 | cacheDirectory,
|
---|
| 154 | cacheIdentifier,
|
---|
| 155 | cacheCompression,
|
---|
| 156 | logger
|
---|
| 157 | });
|
---|
| 158 | } else {
|
---|
| 159 | logger.debug("cache is disabled, applying Babel transform");
|
---|
| 160 | result = await transform(source, options);
|
---|
| 161 | }
|
---|
| 162 | config.files.forEach(configFile => {
|
---|
| 163 | this.addDependency(configFile);
|
---|
| 164 | logger.debug(`added '${configFile}' to webpack dependencies`);
|
---|
| 165 | });
|
---|
| 166 | if (result) {
|
---|
| 167 | if (overrides && overrides.result) {
|
---|
| 168 | logger.debug("applying overrides result() to Babel transform results");
|
---|
| 169 | result = await overrides.result.call(this, result, {
|
---|
| 170 | source,
|
---|
| 171 | map: inputSourceMap,
|
---|
| 172 | customOptions,
|
---|
| 173 | config,
|
---|
| 174 | options
|
---|
| 175 | });
|
---|
| 176 | }
|
---|
| 177 | const {
|
---|
| 178 | code,
|
---|
| 179 | map,
|
---|
| 180 | metadata,
|
---|
| 181 | externalDependencies
|
---|
| 182 | } = result;
|
---|
| 183 | externalDependencies?.forEach(dep => {
|
---|
| 184 | this.addDependency(dep);
|
---|
| 185 | logger.debug(`added '${dep}' to webpack dependencies`);
|
---|
| 186 | });
|
---|
| 187 | metadataSubscribers.forEach(subscriber => {
|
---|
| 188 | subscribe(subscriber, metadata, this);
|
---|
| 189 | logger.debug(`invoked metadata subscriber '${String(subscriber)}'`);
|
---|
| 190 | });
|
---|
| 191 | return [code, map];
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | // If the file was ignored, pass through the original content.
|
---|
| 196 | return [source, inputSourceMap];
|
---|
| 197 | } |
---|