Skip to content

Commit

Permalink
fix: empty string on firefox [close #14]
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Dec 18, 2023
1 parent 0ccec58 commit 46ed395
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
32 changes: 22 additions & 10 deletions src/core/transform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export function genConsoleString(genContext: GenContext) {
const { options, originalColumn, originalLine, argType, id } = genContext
let { argsName } = genContext
const { prefix, suffix, disableLaunchEditor, port, disableHighlight } = options
const _prefix = prefix ? `${prefix} \\n` : ''
const _suffix = suffix ? `\\n ${suffix}` : ''
const _prefix = prefix ? `${prefix}\\n` : ''
const _suffix = suffix ? `\\n${suffix}` : ''

const urlObject = new URL(id, 'file://')
const filePath = urlObject.pathname
Expand All @@ -28,17 +28,29 @@ export function genConsoleString(genContext: GenContext) {

let consoleString = ''

if (!disableHighlight && !disableHighlight)
consoleString = `"${_prefix}${lineInfo}${launchEditorString}","${getConsoleStyle(fileType)}","${launchEditorStyle}","\\n",`
if (!disableHighlight && !disableHighlight) {
consoleString = _prefix
? `"${_prefix}${lineInfo}${launchEditorString}","${getConsoleStyle(fileType)}","${launchEditorStyle}","\\n",`
: `"${lineInfo}${launchEditorString}","${getConsoleStyle(fileType)}","${launchEditorStyle}","\\n",`
}

if (disableHighlight && !disableLaunchEditor)
consoleString = `"${_prefix}${launchEditorString}","${launchEditorStyle}","\\n",`
if (disableHighlight && !disableLaunchEditor) {
consoleString = _prefix
? `"${_prefix}${launchEditorString}","${launchEditorStyle}","\\n",`
: `"${launchEditorString}","${launchEditorStyle}","\\n",`
}

if (!disableHighlight && disableLaunchEditor)
consoleString = `"${_prefix}${lineInfo}","${getConsoleStyle(fileType)}","\\n",`
if (!disableHighlight && disableLaunchEditor) {
consoleString = _prefix
? `"${_prefix}${lineInfo}","${getConsoleStyle(fileType)}","\\n",`
: `"${lineInfo}","${getConsoleStyle(fileType)}","\\n",`
}

if (disableHighlight && disableLaunchEditor)
consoleString = `"${_prefix}",`
if (disableHighlight && disableLaunchEditor) {
consoleString = _prefix
? `"${_prefix}","\\n",`
: ''
}

return {
consoleString,
Expand Down
5 changes: 2 additions & 3 deletions src/core/transform/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ export function viteTransform(context: Context) {
id,
})

magicString
.appendLeft(argsStart, consoleString)
.appendRight(argsEnd, `,"${_suffix}"`)
consoleString && magicString.appendLeft(argsStart, consoleString)
_suffix && magicString.appendRight(argsEnd, `,"${_suffix}"`)
}
},
})
Expand Down
5 changes: 2 additions & 3 deletions src/core/transform/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ export async function webpackTransform(context: Context) {
id,
})

magicString
.appendLeft(argsStart, consoleString)
.appendRight(argsEnd, `,"${_suffix}"`)
consoleString && magicString.appendLeft(argsStart, consoleString)
_suffix && magicString.appendRight(argsEnd, `,"${_suffix}"`)
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const filter = createFilter(
[/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
)

const commonStyle = 'padding:2px 5px; border-radius:3px 0 0 3px;'
const commonStyle = 'padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;'

const consoleStyles: Record<string, string> = {
'.js': `${commonStyle}color: #111827; background: #F7DF1E`,
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

exports[`webpack js transform > script 1`] = `
{
"code": "console.log(\\"%cπŸš€ main.js:1%cπŸ”¦ http://localhost:3090/client#Li4vLi4vbWFpbi5qczoxOjE=\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;color: #111827; background: #F7DF1E\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript',\\"\\")",
"code": "console.log(\\"%cπŸš€ main.js:1%cπŸ”¦ http://localhost:3090/client#Li4vLi4vbWFpbi5qczoxOjE=\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;color: #111827; background: #F7DF1E\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript')",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,iQAAY,qBAAkB",
"mappings": "AAAA,mRAAY",
"names": [],
"sources": [
"main.js",
Expand All @@ -27,7 +27,7 @@ exports[`webpack vue transform > script 1`] = `
import { defineComponent } from 'vue';
import HelloWorld from './components/HelloWorld.vue';
console.log(\\"%cπŸš€ App.vue:10%cπŸ”¦ http://localhost:3070/client#Li4vLi4vQXBwLnZ1ZToxMDox\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'App.vue',\\"\\")
console.log(\\"%cπŸš€ App.vue:10%cπŸ”¦ http://localhost:3070/client#Li4vLi4vQXBwLnZ1ZToxMDox\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'App.vue')
export default defineComponent({
name: 'App',
Expand All @@ -39,7 +39,7 @@ export default defineComponent({
",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,+PAAY,YAAS;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;",
"mappings": "AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iRAAY;AACZ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;",
"names": [],
"sources": [
"App.vue",
Expand All @@ -53,7 +53,7 @@ export default defineComponent({
exports[`webpack vue transform > script setup 1`] = `
{
"code": "<script setup lang=\\"ts\\">
console.log(\\"%cπŸš€ App.vue:2%cπŸ”¦ http://localhost:3090/client#Li4vLi4vQXBwLnZ1ZToyOjM=\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'script setup',\\"\\")
console.log(\\"%cπŸš€ App.vue:2%cπŸ”¦ http://localhost:3090/client#Li4vLi4vQXBwLnZ1ZToyOjM=\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'script setup')
</script>
<template>
Expand All @@ -64,7 +64,7 @@ exports[`webpack vue transform > script setup 1`] = `
",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA;AACA,gQAAc,iBAAc;AAC5B;AACA;AACA;AACA;AACA;AACA;AACA;",
"mappings": "AAAA;AACA,kRAAc;AACd;AACA;AACA;AACA;AACA;AACA;AACA;",
"names": [],
"sources": [
"App.vue",
Expand Down
20 changes: 10 additions & 10 deletions test/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

exports[`options > disable all 1`] = `
{
"code": "console.log(\\"\\",'hello javascript',\\"\\")",
"code": "console.log('hello javascript')",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,eAAY,qBAAkB",
"mappings": "AAAA",
"names": [],
"sources": [
"/home/runner/work/unplugin-turbo-console/src/App.vue",
Expand All @@ -18,10 +18,10 @@ exports[`options > disable all 1`] = `

exports[`options > disable highlight editor 1`] = `
{
"code": "console.log(\\"%cπŸ”¦ http://localhost:undefined/client#Li4vLi4vaG9tZS9ydW5uZXIvd29yay91bnBsdWdpbi10dXJiby1jb25zb2xlL3NyYy9BcHAudnVlOjE6MQ==\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript',\\"\\")",
"code": "console.log(\\"%cπŸ”¦ http://localhost:undefined/client#Li4vLi4vaG9tZS9ydW5uZXIvd29yay91bnBsdWdpbi10dXJiby1jb25zb2xlL3NyYy9BcHAudnVlOjE6MQ==\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript')",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,mOAAY,qBAAkB",
"mappings": "AAAA,mOAAY",
"names": [],
"sources": [
"/home/runner/work/unplugin-turbo-console/src/App.vue",
Expand All @@ -34,10 +34,10 @@ exports[`options > disable highlight editor 1`] = `

exports[`options > disable launch editor 1`] = `
{
"code": "console.log(\\"%cπŸš€ App.vue:1\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;color: #fff; background: #4FC08D\\",\\"\\\\n\\",'hello javascript',\\"\\")",
"code": "console.log(\\"%cπŸš€ App.vue:1\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;color: #fff; background: #4FC08D\\",\\"\\\\n\\",'hello javascript')",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,gHAAY,qBAAkB",
"mappings": "AAAA,kIAAY",
"names": [],
"sources": [
"/home/runner/work/unplugin-turbo-console/src/App.vue",
Expand All @@ -50,10 +50,10 @@ exports[`options > disable launch editor 1`] = `

exports[`options > empty option 1`] = `
{
"code": "console.log(\\"%cπŸš€ App.vue:1%cπŸ”¦ http://localhost:undefined/client#Li4vLi4vaG9tZS9ydW5uZXIvd29yay91bnBsdWdpbi10dXJiby1jb25zb2xlL3NyYy9BcHAudnVlOjE6MQ==\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript',\\"\\")",
"code": "console.log(\\"%cπŸš€ App.vue:1%cπŸ”¦ http://localhost:undefined/client#Li4vLi4vaG9tZS9ydW5uZXIvd29yay91bnBsdWdpbi10dXJiby1jb25zb2xlL3NyYy9BcHAudnVlOjE6MQ==\\",\\"padding:2px 5px; border-radius:3px 0 0 3px;margin-bottom:5px;color: #fff; background: #4FC08D\\",\\"background: #00DC8250;padding:2px 5px;border-radius:0 3px 3px 0;margin-bottom:5px\\",\\"\\\\n\\",'hello javascript')",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,+TAAY,qBAAkB",
"mappings": "AAAA,iVAAY",
"names": [],
"sources": [
"/home/runner/work/unplugin-turbo-console/src/App.vue",
Expand All @@ -66,10 +66,10 @@ exports[`options > empty option 1`] = `

exports[`options > perfix suffix 1`] = `
{
"code": "console.log(\\"xxxx \\\\n\\",'hello javascript',\\"\\\\n yyy\\")",
"code": "console.log(\\"xxxx\\\\n\\",\\"\\\\n\\",'hello javascript',\\"\\\\nyyy\\")",
"map": SourceMap {
"file": undefined,
"mappings": "AAAA,sBAAY,2BAAkB",
"mappings": "AAAA,0BAAY,0BAAkB",
"names": [],
"sources": [
"/home/runner/work/unplugin-turbo-console/src/App.vue",
Expand Down

0 comments on commit 46ed395

Please sign in to comment.