1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const esbuild_1 = require("esbuild");
|
9 | const loader_utils_1 = require("loader-utils");
|
10 | const joycon_1 = __importDefault(require("joycon"));
|
11 | const json5_1 = __importDefault(require("json5"));
|
12 | const joycon = new joycon_1.default();
|
13 | joycon.addLoader({
|
14 | test: /\.json$/,
|
15 | async load(filePath) {
|
16 | try {
|
17 | const config = fs_1.default.readFileSync(filePath, 'utf8');
|
18 | return json5_1.default.parse(config);
|
19 | }
|
20 | catch (error) {
|
21 | throw new Error(`Failed to parse tsconfig at ${path_1.default.relative(process.cwd(), filePath)}: ${error.message}`);
|
22 | }
|
23 | },
|
24 | });
|
25 | const isTsExtensionPtrn = /\.ts$/i;
|
26 | let tsConfig;
|
27 | async function ESBuildLoader(source) {
|
28 | var _a, _b, _c;
|
29 | const done = this.async();
|
30 | const options = loader_utils_1.getOptions(this);
|
31 | const { implementation, ...esbuildTransformOptions } = options;
|
32 | if (implementation && typeof implementation.transform !== 'function') {
|
33 | done(new TypeError(`esbuild-loader: options.implementation.transform must be an ESBuild transform function. Received ${typeof implementation.transform}`));
|
34 | return;
|
35 | }
|
36 | const transform = (_a = implementation === null || implementation === void 0 ? void 0 : implementation.transform) !== null && _a !== void 0 ? _a : esbuild_1.transform;
|
37 | const transformOptions = {
|
38 | ...esbuildTransformOptions,
|
39 | target: (_b = options.target) !== null && _b !== void 0 ? _b : 'es2015',
|
40 | loader: (_c = options.loader) !== null && _c !== void 0 ? _c : 'js',
|
41 | sourcemap: this.sourceMap,
|
42 | sourcefile: this.resourcePath,
|
43 | };
|
44 | if (!('tsconfigRaw' in transformOptions)) {
|
45 | if (!tsConfig) {
|
46 | tsConfig = await joycon.load(['tsconfig.json']);
|
47 | }
|
48 | if (tsConfig.data) {
|
49 | transformOptions.tsconfigRaw = tsConfig.data;
|
50 | }
|
51 | }
|
52 |
|
53 | if (transformOptions.loader === 'tsx'
|
54 | && isTsExtensionPtrn.test(this.resourcePath)) {
|
55 | transformOptions.loader = 'ts';
|
56 | }
|
57 | try {
|
58 | const { code, map } = await transform(source, transformOptions);
|
59 | done(null, code, map && JSON.parse(map));
|
60 | }
|
61 | catch (error) {
|
62 | done(error);
|
63 | }
|
64 | }
|
65 | exports.default = ESBuildLoader;
|