diff --git a/playgrounds/vue/vite.config.ts b/playgrounds/vue/vite.config.ts index 354375b..f66db95 100644 --- a/playgrounds/vue/vite.config.ts +++ b/playgrounds/vue/vite.config.ts @@ -10,6 +10,7 @@ const config = defineConfig({ splitBy: '\n', preTip: '🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥', enableFileName: true, + endLine: true, }), Inspect(), ], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4ebc8b0..cdfbf12 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -314,7 +314,7 @@ packages: resolution: {integrity: sha512-NieM3pVIYW2SwGzKoqfPrQsf4xGs9M9AIG3ThppsSRmO+m7eQhmI6amajKMUeIO37wFfsvnvcxQFx6x6iqxDnA==} engines: {node: '>=6.9.0'} dependencies: - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 '@jridgewell/gen-mapping': 0.3.3 '@jridgewell/trace-mapping': 0.3.18 jsesc: 2.5.2 @@ -438,7 +438,7 @@ packages: engines: {node: '>=6.0.0'} hasBin: true dependencies: - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 /@babel/plugin-syntax-typescript@7.21.4: resolution: {integrity: sha512-xz0D39NvhQn4t4RNsHmDnnsaQizIlUkdtYvLs8La1BlfjQ6JEwxkJGeqJMW2tAXx+q6H+WFuUTXNdYVpEya0YA==} @@ -493,7 +493,7 @@ packages: '@babel/helper-hoist-variables': 7.22.5 '@babel/helper-split-export-declaration': 7.22.5 '@babel/parser': 7.22.5 - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 debug: 4.3.4 globals: 11.12.0 transitivePeerDependencies: @@ -525,6 +525,7 @@ packages: '@babel/helper-string-parser': 7.22.5 '@babel/helper-validator-identifier': 7.22.5 to-fast-properties: 2.0.0 + dev: true /@babel/types@7.22.5: resolution: {integrity: sha512-zo3MIHGOkPOfoRXitsgHLjEXmlDaD/5KU1Uzuc9GNiZPhSqVxVRtxuPaSBZDsYZ9qV88AjtMtWW7ww98loJ9KA==} @@ -1493,13 +1494,13 @@ packages: /@types/babel__generator@7.6.4: resolution: {integrity: sha512-tFkciB9j2K755yrTALxD44McOrk+gfpIpvC3sxHjRawj6PfnQxrse4Clq5y/Rq+G3mrBurMax/lG8Qn2t9mSsg==} dependencies: - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 dev: true /@types/babel__traverse@7.20.1: resolution: {integrity: sha512-MitHFXnhtgwsGZWtT68URpOvLN4EREih1u3QtQiN4VdAxWKRVvGCSvw/Qth0M0Qq3pJpnGOu5JaM/ydK7OGbqg==} dependencies: - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 dev: true /@types/chai-subset@1.3.3: @@ -6196,7 +6197,7 @@ packages: dependencies: '@babel/core': 7.22.5 '@babel/standalone': 7.22.5 - '@babel/types': 7.21.4 + '@babel/types': 7.22.5 defu: 6.1.2 jiti: 1.18.2 mri: 1.2.0 diff --git a/src/index.ts b/src/index.ts index 373ccf7..9a6aac4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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 @@ -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 } @@ -133,7 +139,7 @@ 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 @@ -141,7 +147,7 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption { line, column, }) || {} - const endLineTipNode = t.stringLiteral(`\nline of ${endLine} ${combinePreTip}:\n`) + const endLineTipNode = stringLiteral(`\nline of ${endLine} ${combinePreTip}:\n`) nodeArguments.push(endLineTipNode) } }