-
-
Notifications
You must be signed in to change notification settings - Fork 69
perf(vite-plugin): replace babel with oxc #407
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
SkandarS0
wants to merge
7
commits into
TanStack:main
Choose a base branch
from
SkandarS0:vite-plugin-from-babel-to-oxc
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
537c73d
perf(vite-plugin): replace babel with oxc
SkandarS0 a007f2b
fix(tests/remove-devtools): preserve formatting from fixtures
SkandarS0 17aedcc
Merge remote-tracking branch 'origin/main' into vite-plugin-from-babeβ¦
SkandarS0 5eeb36c
docs: replace babel with oxc across the skills files
SkandarS0 b486792
chore(deps): update oxc-parser from `0.120.0` to `^0.121.0`
SkandarS0 e5b254f
fix: add '@tanstack/preact-devtools' to the devtoolsPackages list
SkandarS0 b59b156
refactor(types): prefer inferred types over type any in `inject-plugiβ¦
SkandarS0 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
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,78 +1,73 @@ | ||
| import chalk from 'chalk' | ||
| import { normalizePath } from 'vite' | ||
| import { gen, parse, t, trav } from './babel' | ||
| import type { types as Babel } from '@babel/core' | ||
| import type { ParseResult } from '@babel/parser' | ||
|
|
||
| const transform = ( | ||
| ast: ParseResult<Babel.File>, | ||
| filePath: string, | ||
| port: number, | ||
| ) => { | ||
| let didTransform = false | ||
|
|
||
| trav(ast, { | ||
| CallExpression(path) { | ||
| const callee = path.node.callee | ||
| // Match console.log(...) or console.error(...) | ||
| if ( | ||
| callee.type === 'MemberExpression' && | ||
| callee.object.type === 'Identifier' && | ||
| callee.object.name === 'console' && | ||
| callee.property.type === 'Identifier' && | ||
| (callee.property.name === 'log' || callee.property.name === 'error') | ||
| ) { | ||
| const location = path.node.loc | ||
| if (!location) { | ||
| return | ||
| } | ||
| const [lineNumber, column] = [ | ||
| location.start.line, | ||
| location.start.column, | ||
| ] | ||
| const finalPath = `${filePath}:${lineNumber}:${column + 1}` | ||
| const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\n β ` | ||
|
|
||
| const serverLogMessage = t.arrayExpression([ | ||
| t.stringLiteral(logMessage), | ||
| ]) | ||
| const browserLogMessage = t.arrayExpression([ | ||
| // LOG with css formatting specifiers: %c | ||
| t.stringLiteral( | ||
| `%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \n β `, | ||
| ), | ||
| // magenta | ||
| t.stringLiteral('color:#A0A'), | ||
| t.stringLiteral('color:#FFF'), | ||
| // blueBright | ||
| t.stringLiteral('color:#55F'), | ||
| t.stringLiteral('color:#FFF'), | ||
| ]) | ||
|
|
||
| // typeof window === "undefined" | ||
| const checkServerCondition = t.binaryExpression( | ||
| '===', | ||
| t.unaryExpression('typeof', t.identifier('window')), | ||
| t.stringLiteral('undefined'), | ||
| ) | ||
|
|
||
| // ...(isServer ? serverLogMessage : browserLogMessage) | ||
| path.node.arguments.unshift( | ||
| t.spreadElement( | ||
| t.conditionalExpression( | ||
| checkServerCondition, | ||
| serverLogMessage, | ||
| browserLogMessage, | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| didTransform = true | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| return didTransform | ||
| import { Visitor, parseSync } from 'oxc-parser' | ||
| import type { CallExpression, MemberExpression } from 'oxc-parser' | ||
|
|
||
| type Insertion = { | ||
| at: number | ||
| text: string | ||
| } | ||
|
|
||
| const buildLineStarts = (source: string) => { | ||
| const starts = [0] | ||
| for (let i = 0; i < source.length; i++) { | ||
| if (source[i] === '\n') { | ||
| starts.push(i + 1) | ||
| } | ||
| } | ||
| return starts | ||
| } | ||
|
|
||
| const offsetToLineColumn = (offset: number, lineStarts: Array<number>) => { | ||
| // Binary search to find the nearest line start <= offset. | ||
| let low = 0 | ||
| let high = lineStarts.length - 1 | ||
|
|
||
| while (low <= high) { | ||
| const mid = (low + high) >> 1 | ||
| const lineStart = lineStarts[mid] | ||
| if (lineStart === undefined) { | ||
| break | ||
| } | ||
|
|
||
| if (lineStart <= offset) { | ||
| low = mid + 1 | ||
| } else { | ||
| high = mid - 1 | ||
| } | ||
| } | ||
|
|
||
| const lineIndex = Math.max(0, high) | ||
| const lineStart = lineStarts[lineIndex] ?? 0 | ||
|
|
||
| return { | ||
| line: lineIndex + 1, | ||
| column: offset - lineStart + 1, | ||
| } | ||
| } | ||
|
|
||
| const isConsoleMemberExpression = ( | ||
| callee: CallExpression['callee'], | ||
| ): callee is MemberExpression => { | ||
| return ( | ||
| callee.type === 'MemberExpression' && | ||
| callee.computed === false && | ||
| callee.object.type === 'Identifier' && | ||
| callee.object.name === 'console' && | ||
| callee.property.type === 'Identifier' && | ||
| (callee.property.name === 'log' || callee.property.name === 'error') | ||
| ) | ||
| } | ||
|
|
||
| const applyInsertions = (source: string, insertions: Array<Insertion>) => { | ||
| const ordered = [...insertions].sort((a, b) => b.at - a.at) | ||
|
|
||
| let next = source | ||
| for (const insertion of ordered) { | ||
| next = next.slice(0, insertion.at) + insertion.text + next.slice(insertion.at) | ||
| } | ||
|
|
||
| return next | ||
| } | ||
|
|
||
| export function enhanceConsoleLog(code: string, id: string, port: number) { | ||
|
|
@@ -81,21 +76,66 @@ export function enhanceConsoleLog(code: string, id: string, port: number) { | |
| const location = filePath?.replace(normalizePath(process.cwd()), '')! | ||
|
|
||
| try { | ||
| const ast = parse(code, { | ||
| const result = parseSync(filePath ?? id, code, { | ||
| sourceType: 'module', | ||
| plugins: ['jsx', 'typescript'], | ||
| lang: 'tsx', | ||
| range: true, | ||
| }) | ||
| const didTransform = transform(ast, location, port) | ||
| if (!didTransform) { | ||
|
|
||
| if (result.errors.length > 0) { | ||
| return | ||
| } | ||
| return gen(ast, { | ||
| sourceMaps: true, | ||
| retainLines: true, | ||
| filename: id, | ||
| sourceFileName: filePath, | ||
| }) | ||
| } catch (e) { | ||
|
|
||
| const insertions: Array<Insertion> = [] | ||
| const lineStarts = buildLineStarts(code) | ||
|
|
||
| new Visitor({ | ||
| CallExpression(node) { | ||
| if (!isConsoleMemberExpression(node.callee)) { | ||
| return | ||
| } | ||
|
|
||
| const { line, column } = offsetToLineColumn(node.start, lineStarts) | ||
| const finalPath = `${location}:${line}:${column}` | ||
|
|
||
| const serverLogMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(finalPath)}\n β ` | ||
| const browserLogMessage = `%cLOG%c %cGo to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent( | ||
| finalPath, | ||
| )}%c \n β ` | ||
|
Comment on lines
+101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the Lines 101-104 hardcode βοΈ Proposed fix+ const levelLabel =
+ node.callee.property.name === 'error' ? 'ERROR' : 'LOG'
+
- const serverLogMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(finalPath)}\n β `
- const browserLogMessage = `%cLOG%c %cGo to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(
+ const serverLogMessage = `${chalk.magenta(levelLabel)} ${chalk.blueBright(finalPath)}\n β `
+ const browserLogMessage = `%c${levelLabel}%c %cGo to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(
finalPath,
)}%c \n β `π€ Prompt for AI Agents |
||
|
|
||
| const argsArray = | ||
| `[${JSON.stringify(serverLogMessage)}]` + | ||
| ` : [${JSON.stringify(browserLogMessage)},` + | ||
| `${JSON.stringify('color:#A0A')},` + | ||
| `${JSON.stringify('color:#FFF')},` + | ||
| `${JSON.stringify('color:#55F')},` + | ||
| `${JSON.stringify('color:#FFF')}]` | ||
|
|
||
| const injectedPrefix = | ||
| `...(typeof window === 'undefined' ? ${argsArray})` + | ||
| `${node.arguments.length > 0 ? ', ' : ''}` | ||
|
|
||
| const insertionPoint = | ||
| node.arguments[0]?.start !== undefined | ||
| ? node.arguments[0].start | ||
| : node.end - 1 | ||
|
|
||
| insertions.push({ | ||
| at: insertionPoint, | ||
| text: injectedPrefix, | ||
| }) | ||
| }, | ||
| }).visit(result.program) | ||
|
|
||
| if (insertions.length === 0) { | ||
| return | ||
| } | ||
|
|
||
| return { | ||
| code: applyInsertions(code, insertions), | ||
| map: null, | ||
| } | ||
| } catch { | ||
| return | ||
| } | ||
| } | ||
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.
I have also fixed this
//that was there for like a decade