Skip to content

Commit

Permalink
fix: remove console
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhuish committed Mar 22, 2024
1 parent 1ba7d28 commit ee67edd
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 44 deletions.
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"stub": "unbuild --stub"
},
"dependencies": {
"@babel/core": "^7.24.3",
"@babel/generator": "^7.24.1",
"@babel/traverse": "^7.24.1",
"@babel/types": "^7.24.0",
"@uni-helper/uni-env": "^0.1.1",
"@vue/compiler-sfc": "^3.4.21",
Expand All @@ -58,8 +60,9 @@
},
"devDependencies": {
"@antfu/utils": "^0.7.7",
"@babel/parser": "^7.24.1",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__traverse": "^7.20.5",
"@types/debug": "^4.1.12",
"@types/node": "^20.11.24"
}
Expand Down
24 changes: 18 additions & 6 deletions packages/core/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import generate from '@babel/generator'
import JSON5 from 'json5'
import { parse as YAMLParser } from 'yaml'
import { normalizePath } from 'vite'
import traverse from '@babel/traverse'
import type { PageContext } from './context'
import { debug, parseSFC } from './utils'
import type { DefinePageOptions, PageMetaDatum, PagePath, RouteBlockLang } from './types'
Expand Down Expand Up @@ -98,7 +99,7 @@ async function readPageOptionsFromMacro(path: string, sfc?: SFCDescriptor) {
if (!arg)
return

const options: DefinePageOptions = await runExpressionByTSX({ file: path, arg, imports })
const options: DefinePageOptions = await runExpressionByTSX({ file: path, exp: arg, imports })

return options
}
Expand Down Expand Up @@ -144,7 +145,18 @@ function findMacroWithImports(scriptSetup: SFCScriptBlock | null) {
plugins: [['importAttributes', { deprecatedAssertSyntax: true }]],
})

const stmts = parsed.body
const ast = t.file(parsed)

// 删除代码里的 console
traverse(ast, {
ImportDeclaration: () => {}, // ignore import
CallExpression: (path, _parent) => {
if (path.node.callee.type === 'MemberExpression' && (path.node.callee.object as any).name === 'console')
path.remove()
},
})

const stmts = ast.program.body

const nodes = stmts
.map((raw: t.Node) => {
Expand Down Expand Up @@ -182,10 +194,10 @@ export function findMacro(scriptSetup: SFCScriptBlock | null) {
return findMacroWithImports(scriptSetup).macro
}

async function runExpressionByTSX(options: { file: string, arg: t.Expression, imports: t.ImportDeclaration[] }) {
async function runExpressionByTSX(options: { file: string, exp: t.Expression, imports: t.ImportDeclaration[] }) {
const {
file,
arg,
exp,
imports,
} = options

Expand All @@ -194,7 +206,7 @@ async function runExpressionByTSX(options: { file: string, arg: t.Expression, im
if (!existsSync(tsx))
throw new Error(`[vite-plugin-uni-pages] "tsx" is required for function argument of definePage macro`)

const code = generate(arg).code
const code = generate(exp).code

const cwd = dirname(file)

Expand All @@ -203,7 +215,7 @@ async function runExpressionByTSX(options: { file: string, arg: t.Expression, im
for (const imp of imports)
script += `${generate(imp).code}\n`

script += t.isFunctionExpression(arg) || t.isArrowFunctionExpression(arg)
script += t.isFunctionExpression(exp) || t.isArrowFunctionExpression(exp)
? `Promise.resolve((${code})()).then(res=>console.log(JSON.stringify(res)))`
: `console.log(JSON.stringify(${code}))`

Expand Down
14 changes: 14 additions & 0 deletions packages/playground/src/pages/macros/remove-console.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts" setup>
definePage(() => {
console.log('abc ...') // this is console should be remove
return {
style: {
navigationBarTitleText: 'this is a title'
}
}
})
</script>

<template>
<div>test</div>
</template>
Loading

0 comments on commit ee67edd

Please # to comment.