UNPKG

1.44 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const loader_utils_1 = require("loader-utils");
4const tsxTryTsLoaderPtrn = /Unexpected|Expected/;
5async function ESBuildLoader(source) {
6 var _a, _b;
7 const done = this.async();
8 const options = loader_utils_1.getOptions(this);
9 const service = this._compiler.$esbuildService;
10 if (!service) {
11 done(new Error('[esbuild-loader] You need to add ESBuildPlugin to your webpack config first'));
12 return;
13 }
14 const transformOptions = {
15 ...options,
16 target: (_a = options.target) !== null && _a !== void 0 ? _a : 'es2015',
17 loader: (_b = options.loader) !== null && _b !== void 0 ? _b : 'js',
18 sourcemap: this.sourceMap,
19 sourcefile: this.resourcePath,
20 };
21 try {
22 const result = await service.transform(source, transformOptions).catch(async (error) => {
23 // Target might be a TS file accidentally parsed as TSX
24 if (transformOptions.loader === 'tsx' && tsxTryTsLoaderPtrn.test(error.message)) {
25 transformOptions.loader = 'ts';
26 return service.transform(source, transformOptions).catch(_ => {
27 throw error;
28 });
29 }
30 throw error;
31 });
32 done(null, result.code, result.map);
33 }
34 catch (error) {
35 done(error);
36 }
37}
38exports.default = ESBuildLoader;