-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Chore: Experimenting with Lage as a replacement for turbo #1641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
SBoudrias
wants to merge
1
commit into
main
Choose a base branch
from
experiment/lage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or 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,41 @@ | ||
| import { ESLint } from 'eslint'; | ||
|
|
||
| /** @type {ESLint} */ | ||
| let eslintInstance = null; | ||
|
|
||
| /** caches an ESLint instance for the worker */ | ||
| function getEslintInstance(target) { | ||
| if (!eslintInstance) { | ||
| eslintInstance = new ESLint({ | ||
| fix: false, | ||
| cache: false, | ||
| cwd: target.cwd, | ||
| }); | ||
| } | ||
| return eslintInstance; | ||
| } | ||
|
|
||
| /** Workers should have a run function that gets called per package task */ | ||
| async function run(data) { | ||
| const { target } = data; | ||
| const eslint = getEslintInstance(target); | ||
|
|
||
| // You can also use "options" to pass different files pattern to lint | ||
| // e.g. data.options.files; you'll need to then configure this inside | ||
| // lage.config.js's pipeline | ||
| const files = 'src/**/*.ts'; | ||
| const results = await eslint.lintFiles(files); | ||
| const formatter = await eslint.loadFormatter('stylish'); | ||
| const resultText = formatter.format(results); | ||
|
|
||
| // Output results to stdout | ||
| process.stdout.write(resultText + '\n'); | ||
| if (results.some((r) => r.errorCount > 0)) { | ||
| // throw an error to indicate that this task has failed | ||
| throw new Error(`Linting failed with errors`); | ||
| } | ||
| } | ||
|
|
||
| // The module export is picked up by `lage` to run inside a worker, and the | ||
| // module's state is preserved from target run to target run. | ||
| export default run; |
This file contains hidden or 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,17 @@ | ||
| { | ||
| "name": "@repo/lage-workers", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "engines": { | ||
| "node": ">=18" | ||
| }, | ||
| "exports": { | ||
| "./eslint-worker": "./eslint-worker.js", | ||
| "./tsc-worker": "./tsc-worker.js" | ||
| }, | ||
| "dependencies": { | ||
| "eslint": "^9.18.0", | ||
| "typescript": "^5.7.3" | ||
| } | ||
| } |
This file contains hidden or 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,128 @@ | ||
| import ts from 'typescript'; | ||
| import path from 'node:path'; | ||
| import { existsSync } from 'node:fs'; | ||
|
|
||
| // Save the previously run ts.program to be fed inside the next call | ||
| let oldProgram; | ||
|
|
||
| let compilerHost; | ||
|
|
||
| /** this is the patch to ts.compilerHost that retains sourceFiles in a Map **/ | ||
| function createCompilerHost(compilerOptions) { | ||
| const host = ts.createCompilerHost(compilerOptions, true); | ||
| const sourceFiles = new Map(); | ||
| const originalGetSourceFile = host.getSourceFile; | ||
|
|
||
| // monkey patch host to cache source files | ||
| host.getSourceFile = ( | ||
| fileName, | ||
| languageVersion, | ||
| onError, | ||
| shouldCreateNewSourceFile, | ||
| ) => { | ||
| if (sourceFiles.has(fileName)) { | ||
| return sourceFiles.get(fileName); | ||
| } | ||
|
|
||
| const sourceFile = originalGetSourceFile( | ||
| fileName, | ||
| languageVersion, | ||
| onError, | ||
| shouldCreateNewSourceFile, | ||
| ); | ||
|
|
||
| sourceFiles.set(fileName, sourceFile); | ||
|
|
||
| return sourceFile; | ||
| }; | ||
|
|
||
| return host; | ||
| } | ||
|
|
||
| async function tsc(data) { | ||
| const { target } = data; // Lage target data | ||
|
|
||
| const tsconfigJsonFile = path.join(target.cwd, 'tsconfig.json'); | ||
|
|
||
| if (!existsSync(tsconfigJsonFile)) { | ||
| console.log(`this package (${target.cwd}) has no tsconfig.json, skipping work!`); | ||
| return; | ||
| } | ||
|
|
||
| // Parse tsconfig | ||
| const configParserHost = parseConfigHostFromCompilerHostLike(compilerHost ?? ts.sys); | ||
| const parsedCommandLine = ts.getParsedCommandLineOfConfigFile( | ||
| tsconfigJsonFile, | ||
| {}, | ||
| configParserHost, | ||
| ); | ||
| if (!parsedCommandLine) { | ||
| throw new Error('Could not parse tsconfig.json'); | ||
| } | ||
| const compilerOptions = parsedCommandLine.options; | ||
|
|
||
| // Creating compilation host program | ||
| compilerHost = compilerHost ?? createCompilerHost(compilerOptions); | ||
|
|
||
| // The re-use of oldProgram is a trick we all learned from gulp-typescript, credit to ivogabe | ||
| // @see https://github.com/ivogabe/gulp-typescript | ||
| const program = ts.createProgram( | ||
| parsedCommandLine.fileNames, | ||
| compilerOptions, | ||
| compilerHost, | ||
| oldProgram, | ||
| ); | ||
|
|
||
| oldProgram = program; | ||
|
|
||
| const errors = { | ||
| semantics: program.getSemanticDiagnostics(), | ||
| declaration: program.getDeclarationDiagnostics(), | ||
| syntactic: program.getSyntacticDiagnostics(), | ||
| global: program.getGlobalDiagnostics(), | ||
| }; | ||
|
|
||
| const allErrors = []; | ||
|
|
||
| try { | ||
| program.emit(); | ||
| } catch (error) { | ||
| console.log(error.messageText); | ||
| throw new Error('Encountered errors while emitting'); | ||
| } | ||
|
|
||
| let hasErrors = false; | ||
|
|
||
| for (const kind of Object.keys(errors)) { | ||
| for (const diagnostics of errors[kind]) { | ||
| hasErrors = true; | ||
| allErrors.push(diagnostics); | ||
| } | ||
| } | ||
|
|
||
| if (hasErrors) { | ||
| console.log(ts.formatDiagnosticsWithColorAndContext(allErrors, compilerHost)); | ||
| throw new Error('Failed to compile'); | ||
| } else { | ||
| console.log('Compiled successfully\n'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| function parseConfigHostFromCompilerHostLike(host) { | ||
| return { | ||
| fileExists: (f) => host.fileExists(f), | ||
| readDirectory(root, extensions, excludes, includes, depth) { | ||
| return host.readDirectory(root, extensions, excludes, includes, depth); | ||
| }, | ||
| readFile: (f) => host.readFile(f), | ||
| useCaseSensitiveFileNames: host.useCaseSensitiveFileNames, | ||
| getCurrentDirectory: host.getCurrentDirectory, | ||
| onUnRecoverableConfigFileDiagnostic: (d) => { | ||
| throw new Error(ts.flattenDiagnosticMessageText(d.messageText, '\n')); | ||
| }, | ||
| trace: host.trace, | ||
| }; | ||
| } | ||
|
|
||
| export default tsc; |
This file contains hidden or 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 hidden or 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,23 @@ | ||
| module.exports = { | ||
| pipeline: { | ||
| attw: { | ||
| dependsOn: ['tsc'], | ||
| outputs: [], | ||
| }, | ||
| tsc: { | ||
| type: 'worker', | ||
| options: { | ||
| worker: require.resolve('@repo/lage-workers/tsc-worker'), | ||
| }, | ||
| dependsOn: ['^tsc'], | ||
| outputs: ['dist/**'], | ||
| }, | ||
| lint: { | ||
| type: 'worker', | ||
| options: { | ||
| worker: require.resolve('@repo/lage-workers/eslint-worker'), | ||
| }, | ||
| }, | ||
| }, | ||
| npmClient: 'yarn', | ||
| }; | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dummy drop to try, but I think here we want to run
tshy.