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