Skip to content

Commit

Permalink
feat: compatible with all console methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Feb 8, 2024
1 parent 085af5f commit e92b687
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
transpileDependencies: true,
parallel: false,
configureWebpack: {
plugins: [
require('unplugin-turbo-console/webpack')({
Expand Down
1 change: 1 addition & 0 deletions examples/vue3-cli/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
transpileDependencies: true,
parallel: false,
configureWebpack: {
plugins: [
require('unplugin-turbo-console/webpack')({
Expand Down
10 changes: 0 additions & 10 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
import type { Options } from '../types'

export const PLUGIN_NAME = 'unplugin-turbo-console'
export const NUXT_CONFIG_KEY = 'turboConsole'

export const DETAULT_OPTIONS: Options = {
prefix: '',
suffix: '',
disableLaunchEditor: false,
disableHighlight: false,
port: 3070,
}
31 changes: 31 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { env } from 'node:process'
import type { Options } from '../types'

const DETAULT_OPTIONS: Options = {
prefix: '',
suffix: '',
disableLaunchEditor: false,
disableHighlight: false,
port: 3070,
}

export const BUILD_OPTIONS: Options = {
disableLaunchEditor: true,
disableHighlight: true,
}

export function resolveOptions(options: Options): Options {
let resolved = {
...DETAULT_OPTIONS,
...options,
}

if (env.NODE_ENV === 'production') {
resolved = {
...resolved,
...BUILD_OPTIONS,
}
}

return resolved
}
3 changes: 1 addition & 2 deletions src/core/transform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function genConsoleString(genContext: GenContext) {

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

Expand All @@ -64,6 +64,5 @@ export function isConsoleExpression(node: Node) {
&& node.callee.object.type === 'Identifier'
&& node.callee.object.name === 'console'
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'log'
&& node.arguments?.length > 0
}
20 changes: 5 additions & 15 deletions src/core/transform/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@ import { viteTransform } from './vite'
import { webpackTransform } from './webpack'

export function transformer(context: Context) {
const { code, meta } = context
if (meta!.framework === 'webpack') {
// only works on webpack development mode
if (meta!.webpack.compiler.options.mode === 'development')
return webpackTransform(context)
return code
}
else if (meta!.framework === 'rspack') {
if (meta!.rspack.compiler.options.mode === 'development')
return webpackTransform(context)
return code
}
else {
return viteTransform(context)
}
const { meta } = context
if (['webpack', 'rspack'].includes(meta!.framework))
return webpackTransform(context)

else return viteTransform(context)
}
29 changes: 13 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ import type { UnpluginFactory } from 'unplugin'
import { createUnplugin } from 'unplugin'
import { checkPort, getRandomPort } from 'get-port-please'
import type { Context, Options } from './types'
import { DETAULT_OPTIONS, PLUGIN_NAME } from './core/constants'
import { PLUGIN_NAME } from './core/constants'
import { startServer } from './core/server/index'
import { transformer } from './core/transform/transformer'
import { filter, getEnforce, printInfo } from './core/utils'
import { resolveOptions } from './core/options'

export const unpluginFactory: UnpluginFactory<Options | undefined> = (options = {}, meta) => {
const mergedOptions = {
...DETAULT_OPTIONS,
...options,
}
export const unpluginFactory: UnpluginFactory<Options | undefined> = (rawOptions = {}, meta) => {
const options = resolveOptions(rawOptions)

async function detectPort() {
const isAvailable = await checkPort(mergedOptions.port!)
const isAvailable = await checkPort(options.port!)
if (!isAvailable)
mergedOptions.port = await getRandomPort()
options.port = await getRandomPort()
}

return {
Expand All @@ -27,7 +25,7 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
},
async transform(code, id) {
const context: Context = {
options: mergedOptions,
options,
pluginContext: this,
code,
id,
Expand All @@ -39,7 +37,6 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
return result
},
vite: {
apply: 'serve',
async configureServer(server) {
if (options.disableLaunchEditor)
return
Expand All @@ -49,10 +46,10 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
const _print = server.printUrls
server.printUrls = () => {
_print()
printInfo(mergedOptions.port!)
printInfo(options.port!)
}

startServer(mergedOptions.port)
startServer(options.port)
},
},
async webpack(compiler) {
Expand All @@ -66,8 +63,8 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
if (state.hasErrors())
return

printInfo(mergedOptions.port!)
startServer(mergedOptions.port)
printInfo(options.port!)
startServer(options.port)
})
}
},
Expand All @@ -82,8 +79,8 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (options =
if (state.hasErrors())
return

printInfo(mergedOptions.port!)
startServer(mergedOptions.port)
printInfo(options.port!)
startServer(options.port)
})
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ exports[`options > empty option 1`] = `

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

0 comments on commit e92b687

Please sign in to comment.