Skip to content

Commit

Permalink
fix: 编译器转义target异常
Browse files Browse the repository at this point in the history
  • Loading branch information
Eavid committed Sep 9, 2023
1 parent ca7a75f commit 6533f21
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/compiler/errors.cts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ export class NoCompileError extends Error {
);
}
}
console.log(home);
2 changes: 1 addition & 1 deletion src/compiler/swc-complier.cts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function transform(source: string, path: string, options?: Options) {
function TsConfigToSwcConfig(filename?: string, options: ts.CompilerOptions = {}): SwcOptions {
const { target, module } = options;
let swcTarget: JscTarget = (
target === ScriptTarget.ESNext ? "esnext" : ScriptTarget[target as any].toLowerCase()
(target as any as ScriptTarget) === ScriptTarget.ESNext ? "esnext" : ScriptTarget[target as any]?.toLowerCase()
) as any;

let swcModule: string = "es6";
Expand Down
5 changes: 2 additions & 3 deletions src/hook_config.cts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type ts from "typescript";
import * as Path from "node:path";
import { ModuleKind, jsonToTsConfig, parseJson, readTsConfigFileSync } from "./util/tslib.js";
import { getESVersion } from "./lib/es.js";
import { ModuleKind, ScriptTarget, jsonToTsConfig, parseJson, readTsConfigFileSync } from "./util/tslib.js";
interface ProcessEnv {
SAME_PARSER?: string;
TS_COMPILER_OPTIONS?: string;
Expand Down Expand Up @@ -35,7 +34,7 @@ function getDefaultCompilerOptions() {
//解析环境变量中的编译选项
let fileContent = process.env["TS_COMPILER_OPTIONS"];
if (fileContent) Object.assign(compilerOptions, jsonToTsConfig(parseJson(fileContent)));
if (!compilerOptions.target) compilerOptions.target = getESVersion() - 13;
if (!compilerOptions.target) compilerOptions.target = ScriptTarget.ES2022 as any;
return compilerOptions;
}
const DEFAULT_COMPILER_OPTIONS = getDefaultCompilerOptions();
Expand Down
1 change: 0 additions & 1 deletion src/lib/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ export function getESVersion(): number {
return version;
}
declare let Array: any;
console.log(getESVersion());
14 changes: 10 additions & 4 deletions src/util/tslib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export function jsonToTsConfig(json: RawTsCompilerOptions, safe?: boolean) {
let tsConfig: ts.CompilerOptions = { ...json };
if (json.target) {
let targetKey: any = (json.target as string).toUpperCase();
if (targetKey === "ESNEXT") tsConfig.target = ScriptTarget.ESNext;
else if (targetKey.startsWith("ES")) tsConfig.target = ScriptTarget[targetKey] as any as ScriptTarget;
else if (targetKey === "LATEST") tsConfig.target = ScriptTarget.Latest;
else if (targetKey === "JSON") tsConfig.target = ScriptTarget.JSON;
if (targetKey === "ESNEXT") tsConfig.target = ScriptTarget.ESNext as any;
else if (targetKey.startsWith("ES")) tsConfig.target = ScriptTarget[targetKey] as any;
else if (targetKey === "LATEST") tsConfig.target = ScriptTarget.Latest as any;
else if (targetKey === "JSON") tsConfig.target = ScriptTarget.JSON as any;
else if (!safe) throw new FelidError("target");
}
if (json.moduleResolution) {
Expand Down Expand Up @@ -106,6 +106,8 @@ enum ScriptTarget {
ES2020 = 7,
ES2021 = 8,
ES2022 = 9,
ES2023 = 10,
ES2024 = 11,
ESNext = 99,
JSON = 100,
Latest = 99,
Expand Down Expand Up @@ -135,6 +137,10 @@ class FelidError extends Error {
super(`The value of Field ${felid} is incorrect`);
}
}
export type CompilerOptions = Omit<ts.CompilerOptions, "target" | "moduleResolution"> & {
target: ScriptTarget;
moduleResolution: ModuleKind;
};
export type RawTsCompilerOptions = {
[key in keyof ts.CompilerOptions]: ts.CompilerOptions[key] extends string | boolean | undefined | null
? ts.CompilerOptions[key]
Expand Down

0 comments on commit 6533f21

Please sign in to comment.