forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralized browser tsconfig (Azure#32087)
- Loading branch information
1 parent
f2c56f7
commit 03bdd2e
Showing
12 changed files
with
269 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import * as path from "path"; | ||
import * as fs from "fs"; | ||
import ts from "typescript"; | ||
|
||
type Config = { compilerOptions: ts.CompilerOptions }; | ||
|
||
function resolveConfigDir(configPath: string, unresolvedPath: string) { | ||
return unresolvedPath.replace(/\$\{configDir\}/g, path.dirname(configPath)); | ||
} | ||
|
||
function mergeConfig(config1: Config, config2: Config) { | ||
return { | ||
...config1, | ||
...config2, | ||
compilerOptions: { | ||
...config1.compilerOptions, | ||
...config2.compilerOptions, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Generates a complete TypeScript configuration by reading and parsing a given tsconfig file. | ||
* | ||
* @param configPath - The path to the tsconfig file. | ||
* @returns A record containing the raw parsed configuration. | ||
*/ | ||
export function resolveConfig(configPath: string) { | ||
function helper(configPath: string) { | ||
const absolutePath = path.resolve(configPath); | ||
const configDir = path.dirname(absolutePath); | ||
const rawConfig = JSON.parse( | ||
fs.readFileSync( | ||
!absolutePath.toLowerCase().endsWith(".json") ? `${absolutePath}.json` : absolutePath, | ||
"utf8", | ||
), | ||
); | ||
|
||
let resolvedConfig = {} as Config; | ||
const { extends: parents, ...rest } = rawConfig; | ||
if (parents) { | ||
const baseConfigs = Array.isArray(parents) ? parents : [parents]; | ||
for (const baseConfigPath of baseConfigs) { | ||
const baseConfigAbsolutePath = path.resolve(configDir, baseConfigPath); | ||
const baseConfig = helper(baseConfigAbsolutePath); | ||
resolvedConfig = mergeConfig(resolvedConfig, baseConfig); | ||
} | ||
} | ||
return mergeConfig(resolvedConfig, rest); | ||
} | ||
const resolvedConfig = helper(configPath); | ||
const outDir = resolvedConfig.compilerOptions.outDir; | ||
if (outDir) { | ||
resolvedConfig.compilerOptions.outDir = resolveConfigDir(configPath, outDir); | ||
} | ||
return resolvedConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License | ||
|
||
import { describe, it, beforeEach, expect, vi } from "vitest"; | ||
import { vol } from "memfs"; | ||
import { resolveConfig } from "../src/util/resolveTsConfig"; | ||
|
||
vi.mock("fs", async () => { | ||
const memfs = await import("memfs"); | ||
return { | ||
...memfs.fs, | ||
}; | ||
}); | ||
|
||
describe("resolveConfig", () => { | ||
beforeEach(() => { | ||
vol.reset(); | ||
}); | ||
|
||
it("should resolve a simple tsconfig.json", () => { | ||
const def = { | ||
compilerOptions: { | ||
target: "ES6", | ||
}, | ||
}; | ||
vol.fromJSON({ | ||
"/project/tsconfig.json": JSON.stringify(def), | ||
}); | ||
|
||
const result = resolveConfig("/project/tsconfig.json"); | ||
expect(result).toEqual(def); | ||
}); | ||
|
||
it("should resolve tsconfig.json with single extend", () => { | ||
vol.fromJSON({ | ||
"/project/tsconfig.base.json": JSON.stringify({ | ||
compilerOptions: { | ||
target: "ES5", | ||
module: "CommonJS", | ||
}, | ||
}), | ||
"/project/tsconfig.json": JSON.stringify({ | ||
extends: "./tsconfig.base.json", | ||
compilerOptions: { | ||
strict: true, | ||
}, | ||
}), | ||
}); | ||
|
||
const result = resolveConfig("/project/tsconfig.json"); | ||
expect(result).toEqual({ | ||
compilerOptions: { | ||
target: "ES5", | ||
module: "CommonJS", | ||
strict: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should resolve tsconfig.json with single extend and conflicting option", () => { | ||
vol.fromJSON({ | ||
"/project/tsconfig.base.json": JSON.stringify({ | ||
compilerOptions: { | ||
target: "ES5", | ||
outDir: "path1", | ||
}, | ||
}), | ||
"/project/tsconfig.json": JSON.stringify({ | ||
extends: "./tsconfig.base.json", | ||
compilerOptions: { | ||
strict: true, | ||
outDir: "path2", | ||
}, | ||
}), | ||
}); | ||
|
||
const result = resolveConfig("/project/tsconfig.json"); | ||
expect(result).toEqual({ | ||
compilerOptions: { | ||
target: "ES5", | ||
strict: true, | ||
outDir: "path2", | ||
}, | ||
}); | ||
}); | ||
|
||
it("should resolve tsconfig.json with multiple extends with conflicting options in parents", () => { | ||
vol.fromJSON({ | ||
"/project/tsconfig.base1.json": JSON.stringify({ | ||
compilerOptions: { | ||
target: "ES5", | ||
outDir: "path1", | ||
}, | ||
}), | ||
"/project/tsconfig.base2.json": JSON.stringify({ | ||
compilerOptions: { | ||
outDir: "path2", | ||
}, | ||
}), | ||
"/project/tsconfig.json": JSON.stringify({ | ||
extends: ["./tsconfig.base1.json", "./tsconfig.base2.json"], | ||
compilerOptions: { | ||
strict: true, | ||
}, | ||
}), | ||
}); | ||
|
||
const result = resolveConfig("/project/tsconfig.json"); | ||
expect(result).toEqual({ | ||
compilerOptions: { | ||
target: "ES5", | ||
strict: true, | ||
outDir: "path2", | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.test.json", | ||
"exclude": ["./test/**/node", "./test/stress"], | ||
"compilerOptions": { | ||
"outDir": "./dist-test/browser", | ||
"noEmit": false | ||
} | ||
"extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"], | ||
"exclude": ["./test/**/node", "./test/stress"] | ||
} |
7 changes: 1 addition & 6 deletions
7
sdk/eventhub/eventhubs-checkpointstore-blob/tsconfig.browser.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
{ | ||
"extends": "./tsconfig.test.json", | ||
"exclude": ["./test/**/node"], | ||
"compilerOptions": { | ||
"outDir": "./dist-test/browser", | ||
"noEmit": false | ||
} | ||
"extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.test.json", | ||
"exclude": ["./test/**/node", "./test/manual*"], | ||
"compilerOptions": { | ||
"outDir": "./dist-test/browser", | ||
"noEmit": false | ||
} | ||
"extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"], | ||
"exclude": ["./test/**/node", "./test/manual*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
{ | ||
"extends": "./tsconfig.test.json", | ||
"exclude": ["./test/**/node"], | ||
"compilerOptions": { | ||
"outDir": "./dist-test/browser", | ||
"noEmit": false | ||
} | ||
"extends": ["./tsconfig.test.json", "../../../tsconfig.browser.base.json"] | ||
} |
Oops, something went wrong.