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 | }
|
18 |
|
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 |
|
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 | }
|
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 | }
|
118 |
|
119 |
|
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 || loaderOptions.inputSourceMap,
|
132 |
|
133 |
|
134 | sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
|
135 |
|
136 |
|
137 |
|
138 | sourceFileName: filename
|
139 | });
|
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 | }
|
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 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
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 | }
|
202 |
|
203 |
|
204 |
|
205 | if (config.files) {
|
206 | config.files.forEach(configFile => this.addDependency(configFile));
|
207 | } else {
|
208 |
|
209 | if (typeof config.babelrc === "string") {
|
210 | this.addDependency(config.babelrc);
|
211 | }
|
212 |
|
213 |
|
214 | if (config.config) {
|
215 | this.addDependency(config.config);
|
216 | }
|
217 | }
|
218 |
|
219 | if (result) {
|
220 | if (overrides && overrides.result) {
|
221 | result = yield overrides.result.call(this, result, {
|
222 | source,
|
223 | map: inputSourceMap,
|
224 | customOptions,
|
225 | config,
|
226 | options
|
227 | });
|
228 | }
|
229 |
|
230 | const {
|
231 | code,
|
232 | map,
|
233 | metadata
|
234 | } = result;
|
235 | metadataSubscribers.forEach(subscriber => {
|
236 | subscribe(subscriber, metadata, this);
|
237 | });
|
238 | return [code, map];
|
239 | }
|
240 | }
|
241 |
|
242 |
|
243 | return [source, inputSourceMap];
|
244 | });
|
245 | return _loader.apply(this, arguments);
|
246 | } |
\ | No newline at end of file |