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