Introduce TypeScript Project References (#91)

This commit is contained in:
Benjamin Weggersen 2019-10-17 14:30:28 +02:00 коммит произвёл GitHub
Родитель adee6cd7f4
Коммит d7a963b01f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
25 изменённых файлов: 187 добавлений и 60 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -83,3 +83,4 @@ jspm_packages/
.DS_Store
*.swp
tsconfig.tsbuildinfo

Просмотреть файл

@ -56,7 +56,7 @@ Typically you would wrap your npm scripts inside `backfill`, like this:
{
"name": "package",
"scripts": {
"build": "backfill -- tsc"
"build": "backfill -- tsc -b"
}
}
```
@ -101,7 +101,7 @@ The default configuration object is:
packageRoot: "path/to/package",
clearOutputFolder: false,
logLevel: "info",
hashGlobs: ["**/*", "!**/node_modules/**", `!lib/**`]
hashGlobs: ["**/*", "!**/node_modules/**", "!lib/**", "!tsconfig.tsbuildinfo"]
}
```

Просмотреть файл

@ -7,10 +7,11 @@
"packages/*"
],
"scripts": {
"postinstall": "lerna run --stream prepare",
"postinstall": "yarn update-project-references && lerna run --stream prepare",
"lerna": "lerna",
"test": "lerna run --stream test",
"test": "lerna run test",
"prettier": "prettier \"{.,**}/*.{tsx,ts,js,json,md,yaml}\" --write",
"update-project-references": "node tools/update-project-references.js",
"watch": "lerna run watch --parallel"
},
"dependencies": {
@ -19,13 +20,16 @@
"typescript": "3.6.4"
},
"devDependencies": {
"execa": "^3.1.0",
"husky": "^3.0.4",
"lint-staged": "^9.2.5",
"prettier-package-json": "^2.1.0"
"lodash.merge": "^4.6.2",
"prettier-package-json": "^2.1.0",
"resolve-from": "^5.0.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"pre-commit": "yarn update-project-references && lint-staged"
}
},
"lint-staged": {
@ -34,7 +38,7 @@
"prettier --write",
"git add"
],
"**/*.{js,json,md,yaml}": [
"**/*.{ts,js,json,md,yaml}": [
"prettier --write",
"git add"
]

Просмотреть файл

@ -14,10 +14,10 @@
},
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"test": "jest",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"anymatch": "^3.0.3",

Просмотреть файл

@ -1,8 +1,13 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src/**/*", "typings/*"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [
{ "path": "../cache" },
{ "path": "../config" },
{ "path": "../hasher" },
{ "path": "../logger" },
{ "path": "../utils-dotenv" },
{ "path": "../utils-test" }
]
}

4
packages/cache/package.json поставляемый
Просмотреть файл

@ -11,10 +11,10 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"test": "jest",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"@azure/storage-blob": "12.0.0-preview.3",

10
packages/cache/tsconfig.json поставляемый
Просмотреть файл

@ -1,8 +1,10 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [
{ "path": "../config" },
{ "path": "../logger" },
{ "path": "../utils-test" }
]
}

Просмотреть файл

@ -11,10 +11,10 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"test": "jest",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"backfill-logger": "^2.0.2",

Просмотреть файл

@ -68,7 +68,12 @@ export function createDefaultConfig(fromPath: string = process.cwd()): Config {
producePerformanceLogs: false,
clearOutputFolder: false,
logLevel: "info",
hashGlobs: ["**/*", "!**/node_modules/**", `!${outputFolder}/**`]
hashGlobs: [
"**/*",
"!**/node_modules/**",
`!${outputFolder}/**`,
"!tsconfig.tsbuildinfo"
]
};
}

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [{ "path": "../logger" }, { "path": "../utils-test" }]
}

Просмотреть файл

@ -11,10 +11,10 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"test": "jest",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"@yarnpkg/lockfile": "^1.1.0",

Просмотреть файл

@ -1,8 +1,10 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [
{ "path": "../config" },
{ "path": "../logger" },
{ "path": "../utils-test" }
]
}

Просмотреть файл

@ -11,9 +11,9 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"chalk": "^2.4.1",

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": []
}

Просмотреть файл

@ -6,7 +6,7 @@
"main": "lib/index.js",
"scripts": {
"build": "backfill -- yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "backfill -- yarn compile"
},
"devDependencies": {

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"exclude": ["lib"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [{ "path": "../backfill" }]
}

Просмотреть файл

@ -6,7 +6,7 @@
"main": "lib/index.js",
"scripts": {
"build": "backfill -- yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"generate-report": "backfill --generate-performance-report",
"prepare": "backfill -- yarn compile"
},

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"exclude": ["lib"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [{ "path": "../backfill" }, { "path": "../sample-dependency" }]
}

Просмотреть файл

@ -11,9 +11,9 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"dotenv": "^8.1.0",

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": []
}

Просмотреть файл

@ -11,9 +11,9 @@
"main": "lib/index.js",
"scripts": {
"build": "yarn compile",
"compile": "tsc",
"compile": "tsc -b",
"prepare": "yarn compile",
"watch": "tsc -w"
"watch": "tsc -b -w"
},
"dependencies": {
"backfill-logger": "*",

Просмотреть файл

@ -1,8 +1,6 @@
{
"extends": "backfill-utils-tsconfig/tsconfig.default.json",
"exclude": ["__fixtures__", "lib"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [{ "path": "../logger" }]
}

Просмотреть файл

@ -1,5 +1,6 @@
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"inlineSourceMap": true,

Просмотреть файл

@ -0,0 +1,109 @@
const execa = require("execa");
const path = require("path");
const fs = require("fs");
const prettier = require("prettier");
const merge = require("lodash.merge");
const resolveFrom = require("resolve-from");
function getTSConfigPath(location) {
return path.join(process.cwd(), location, "tsconfig.json");
}
function isTSPackage(location) {
return fs.existsSync(getTSConfigPath(location));
}
function getTSConfig(filePath) {
const config = fs.existsSync(filePath)
? JSON.parse(fs.readFileSync(filePath).toString())
: {};
if (config.extends) {
return merge(
getTSConfig(resolveFrom(path.dirname(filePath), config.extends)),
config
);
} else {
return config;
}
}
function isCompositeEnabled(location) {
const configPath = getTSConfigPath(location);
const config = getTSConfig(configPath);
return Boolean(config.compilerOptions && config.compilerOptions.composite);
}
function getAbsoluteLocation(location, posix = false) {
if (posix) {
return path.posix.join(process.cwd(), location);
} else {
return path.join(process.cwd(), location);
}
}
function getRelativeLocation(fromLocation, toLocation, posix = false) {
return path.posix.relative(
getAbsoluteLocation(fromLocation, posix),
getAbsoluteLocation(toLocation, posix)
);
}
function updateTSConfig(path, options) {
const config = fs.existsSync(path)
? JSON.parse(fs.readFileSync(path).toString())
: {};
prettier.resolveConfig(path).then(prettierOptions =>
fs.writeFileSync(
path,
prettier.format(
JSON.stringify({
...config,
...options
}),
{
...prettierOptions,
parser: "json"
}
)
)
);
}
function pathCompare(a, b) {
if (a.path < b.path) {
return -1;
} else if (a.path > b.path) {
return 1;
} else {
return 0;
}
}
function updateTSReferences(workspaceInfo) {
Object.keys(workspaceInfo)
.map(key => workspaceInfo[key])
.filter(({ location }) => isTSPackage(location))
.forEach(({ location, workspaceDependencies }) => {
updateTSConfig(getTSConfigPath(location), {
references: workspaceDependencies
.map(dependency => workspaceInfo[dependency].location)
.filter(isTSPackage)
.filter(isCompositeEnabled)
.map(workspaceLocation => ({
path: getRelativeLocation(location, workspaceLocation, true)
}))
.sort(pathCompare)
});
});
}
const workspaceInfo = JSON.parse(
JSON.parse(
execa
.sync("yarn", ["workspaces", "info", "--json"])
.stdout.toString()
.trim()
).data
);
updateTSReferences(workspaceInfo);

Просмотреть файл

@ -4620,6 +4620,11 @@ lodash.memoize@4.x:
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
@ -6157,6 +6162,11 @@ resolve-from@^4.0.0:
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6"
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==
resolve-from@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==
resolve-url@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"