Skip to content

Commit

Permalink
perf: remove @babel/types function to reduce build volume
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Jul 16, 2023
1 parent 7bb654a commit f803b76
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions playgrounds/vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const config = defineConfig({
splitBy: '\n',
preTip: '🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥',
enableFileName: true,
endLine: true,
}),
Inspect(),
],
Expand Down
13 changes: 7 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import traverse from '@babel/traverse'
import { parse } from '@babel/parser'
import generate from '@babel/generator'
import * as t from '@babel/types'
import type { PluginOption } from 'vite'
import { createFilter } from 'vite'
import type { StringLiteral } from '@babel/types'
import { SourceMapConsumer } from 'source-map'
import type { RawSourceMap } from 'source-map'

function stringLiteral(value: string) {
const stringLiteralNode: StringLiteral = {
type: 'StringLiteral',
value,
}
return stringLiteralNode
}
export type EnableFileName = boolean | {
/**
* @default true
Expand Down Expand Up @@ -48,7 +54,7 @@ export interface Options {
}

function generateStrNode(str: string): StringLiteral & { skip: boolean } {
const node = t.stringLiteral(str)
const node = stringLiteral(str)

// @ts-ignore
node.skip = true
Expand Down Expand Up @@ -97,8 +103,8 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {
// @ts-ignore
if (argument.skip)
continue
if (!t.isLiteral(argument)) {
if (t.isIdentifier(argument) && argument.name === 'undefined') {
if (!argument.type.endsWith('Literal')) {
if (argument.type === 'Identifier' && argument.name === 'undefined') {
nodeArguments.splice(i + 1, 0, splitNode)
continue
}
Expand Down Expand Up @@ -133,15 +139,15 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {

combinePreTip = `~ ${relativeFilename} ${combinePreTip}`
}
const startLineTipNode = t.stringLiteral(`line of ${startLine} ${combinePreTip}:\n`)
const startLineTipNode = stringLiteral(`line of ${startLine} ${combinePreTip}:\n`)
nodeArguments.unshift(startLineTipNode)
if (endLine) {
const { line, column } = loc.end
const { line: endLine } = consumer.originalPositionFor({
line,
column,
}) || {}
const endLineTipNode = t.stringLiteral(`\nline of ${endLine} ${combinePreTip}:\n`)
const endLineTipNode = stringLiteral(`\nline of ${endLine} ${combinePreTip}:\n`)
nodeArguments.push(endLineTipNode)
}
}
Expand Down

0 comments on commit f803b76

Please sign in to comment.