马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
/* eslint-disable @typescript-eslint/no-require-imports */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadTs = exports.loadTsSync = exports.loadYaml = exports.loadJson = exports.loadJs = exports.loadJsSync = void 0;
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const path_1 = __importDefault(require("path"));
const url_1 = require("url");
let importFresh;
const loadJsSync = function loadJsSync(filepath) {
    if (importFresh === undefined) {
        importFresh = require('import-fresh');
    }
    return importFresh(filepath);
};
exports.loadJsSync = loadJsSync;
const loadJs = async function loadJs(filepath) {
    try {
        const { href } = (0, url_1.pathToFileURL)(filepath);
        return (await import(href)).default;
    }
    catch (error) {
        try {
            return (0, exports.loadJsSync)(filepath, '');
        }
        catch (requireError) {
            if (requireError.code === 'ERR_REQUIRE_ESM' ||
                (requireError instanceof SyntaxError &&
                    requireError
                        .toString()
                        .includes('Cannot use import statement outside a module'))) {
                throw error;
            }
            throw requireError;
        }
    }
};
exports.loadJs = loadJs;
let parseJson;
const loadJson = function loadJson(filepath, content) {
    if (parseJson === undefined) {
        parseJson = require('parse-json');
    }
    try {
        return parseJson(content);
    }
    catch (error) {
        error.message = `JSON Error in ${filepath}:\n${error.message}`;
        throw error;
    }
};
exports.loadJson = loadJson;
let yaml;
const loadYaml = function loadYaml(filepath, content) {
    if (yaml === undefined) {
        yaml = require('js-yaml');
    }
    try {
        return yaml.load(content);
    }
    catch (error) {
        error.message = `YAML Error in ${filepath}:\n${error.message}`;
        throw error;
    }
};
exports.loadYaml = loadYaml;
let typescript;
const loadTsSync = function loadTsSync(filepath, content) {
    /* istanbul ignore next -- @preserve */
    if (typescript === undefined) {
        typescript = require('typescript');
    }
    const compiledFilepath = `${filepath.slice(0, -2)}cjs`;
    try {
        const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
        config.compilerOptions = {
            ...config.compilerOptions,
            module: typescript.ModuleKind.NodeNext,
            moduleResolution: typescript.ModuleResolutionKind.NodeNext,
            target: typescript.ScriptTarget.ES2022,
            noEmit: false,
        };
        content = typescript.transpileModule(content, config).outputText;
        (0, fs_1.writeFileSync)(compiledFilepath, content);
        return (0, exports.loadJsSync)(compiledFilepath, content).default;
    }
    catch (error) {
        error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
        throw error;
    }
    finally {
        if ((0, fs_1.existsSync)(compiledFilepath)) {
            (0, fs_1.rmSync)(compiledFilepath);
        }
    }
};
exports.loadTsSync = loadTsSync;
const loadTs = async function loadTs(filepath, content) {
    if (typescript === undefined) {
        typescript = (await import('typescript')).default;
    }
    const compiledFilepath = `${filepath.slice(0, -2)}mjs`;
    let transpiledContent;
    try {
        try {
            const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
            config.compilerOptions = {
                ...config.compilerOptions,
                module: typescript.ModuleKind.ES2022,
                moduleResolution: typescript.ModuleResolutionKind.Bundler,
                target: typescript.ScriptTarget.ES2022,
                noEmit: false,
            };
            transpiledContent = typescript.transpileModule(content, config).outputText;
            await (0, promises_1.writeFile)(compiledFilepath, transpiledContent);
        }
        catch (error) {
            error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
            throw error;
        }
        // eslint-disable-next-line @typescript-eslint/return-await
        return await (0, exports.loadJs)(compiledFilepath, transpiledContent);
    }
    finally {
        if ((0, fs_1.existsSync)(compiledFilepath)) {
            await (0, promises_1.rm)(compiledFilepath);
        }
    }
};
exports.loadTs = loadTs;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function resolveTsConfig(directory) {
    const filePath = typescript.findConfigFile(directory, (fileName) => {
        return typescript.sys.fileExists(fileName);
    });
    if (filePath !== undefined) {
        const { config, error } = typescript.readConfigFile(filePath, (path) => typescript.sys.readFile(path));
        if (error) {
            throw new Error(`Error in ${filePath}: ${error.messageText.toString()}`);
        }
        return config;
    }
    return;
}
//# sourceMappingURL=loaders.js.map