提交 35963d01 编写于 作者: fxy060608's avatar fxy060608

refactor(cli): log

上级 704b6827
const path = require('path')
const {
transformSync
} = require('@babel/core')
const options = {
filename: '/index.vue',
configFile: false,
minified: true,
plugins: [path.resolve(__dirname, '../packages/babel-plugin-console/dist/index.js')]
}
describe('console', () => {
it('log', () => {
expect(transformSync(`console.log('123')`, options).code)
.toBe(`__f__("log","123"," at /index.vue:1");`)
expect(transformSync(`console.log('123',a,{a:1,b:2})`, options).code)
.toBe(`__f__("log","123",a,{a:1,b:2}," at /index.vue:1");`)
})
it('debug', () => {
expect(transformSync(`console.log('123')`, options).code)
.toBe(`__f__("log","123"," at /index.vue:1");`)
expect(transformSync(`console.log('123',a,{a:1,b:2})`, options).code)
.toBe(`__f__("log","123",a,{a:1,b:2}," at /index.vue:1");`)
})
it('info', () => {
expect(transformSync(`console.info('123')`, options).code)
.toBe(`__f__("info","123"," at /index.vue:1");`)
expect(transformSync(`console.info('123',a,{a:1,b:2})`, options).code)
.toBe(`__f__("info","123",a,{a:1,b:2}," at /index.vue:1");`)
})
it('warn', () => {
expect(transformSync(`console.warn('123')`, options).code)
.toBe(`__f__("warn","123"," at /index.vue:1");`)
expect(transformSync(`console.warn('123',a,{a:1,b:2})`, options).code)
.toBe(`__f__("warn","123",a,{a:1,b:2}," at /index.vue:1");`)
})
it('error', () => {
expect(transformSync(`console.error('123')`, options).code)
.toBe(`__f__("error","123"," at /index.vue:1");`)
expect(transformSync(`console.error('123',a,{a:1,b:2})`, options).code)
.toBe(`__f__("error","123",a,{a:1,b:2}," at /index.vue:1");`)
})
})
const path = require('path')
const {
transform
} = require('@babel/core')
transform(`console.log('123')`, {
filename: '/index.vue',
configFile: false,
minified: true,
plugins: [path.resolve(__dirname, '../packages/babel-plugin-console/dist/index.js')]
}, function (err, result) {
console.log(err, result)
})
......@@ -7,7 +7,6 @@ module.exports = function({
return {
visitor: {
CallExpression(path, state) {
const opts = state.opts
if (path.node.callee.object &&
......@@ -36,12 +35,14 @@ module.exports = function({
type: 'StringLiteral',
value: ` at ${file}:${path.node.loc.start.line}`
})
path.node.arguments = [
t.callExpression(
t.identifier(FORMAT_LOG),
args
)
]
args.unshift(t.stringLiteral(path.node.callee.property.name))
path.replaceWith(t.callExpression(t.identifier(FORMAT_LOG), args))
// path.node.arguments = [
// t.callExpression(
// t.identifier(FORMAT_LOG),
// args
// )
// ]
}
}
}
......
......@@ -3,16 +3,19 @@ function typof (v) {
return s.substring(8, s.length - 1)
}
function isDebugMode () {
return typeof name === 'string' && name === 'uni-app'
function isDebugMode () {
/* eslint-disable no-undef */
return typeof __channelId__ === 'string' && __channelId__
}
export default function formatLog () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key]
}
if (isDebugMode()) {
return args
const type = args.shift()
if (isDebugMode()) {
args.push(args.pop().replace('at ', 'uni-app:///'))
return console[type]['apply'](console, args)
}
var msgs = args.map(function (v) {
......@@ -57,5 +60,5 @@ export default function formatLog () {
msg = msgs[0]
}
return msg
console[type](msg)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册