提交 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({ ...@@ -7,7 +7,6 @@ module.exports = function({
return { return {
visitor: { visitor: {
CallExpression(path, state) { CallExpression(path, state) {
const opts = state.opts const opts = state.opts
if (path.node.callee.object && if (path.node.callee.object &&
...@@ -36,12 +35,14 @@ module.exports = function({ ...@@ -36,12 +35,14 @@ module.exports = function({
type: 'StringLiteral', type: 'StringLiteral',
value: ` at ${file}:${path.node.loc.start.line}` value: ` at ${file}:${path.node.loc.start.line}`
}) })
path.node.arguments = [ args.unshift(t.stringLiteral(path.node.callee.property.name))
t.callExpression( path.replaceWith(t.callExpression(t.identifier(FORMAT_LOG), args))
t.identifier(FORMAT_LOG), // path.node.arguments = [
args // t.callExpression(
) // t.identifier(FORMAT_LOG),
] // args
// )
// ]
} }
} }
} }
......
...@@ -3,16 +3,19 @@ function typof (v) { ...@@ -3,16 +3,19 @@ function typof (v) {
return s.substring(8, s.length - 1) return s.substring(8, s.length - 1)
} }
function isDebugMode () { function isDebugMode () {
return typeof name === 'string' && name === 'uni-app' /* eslint-disable no-undef */
return typeof __channelId__ === 'string' && __channelId__
} }
export default function formatLog () { export default function formatLog () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key] args[_key] = arguments[_key]
} }
if (isDebugMode()) { const type = args.shift()
return args if (isDebugMode()) {
args.push(args.pop().replace('at ', 'uni-app:///'))
return console[type]['apply'](console, args)
} }
var msgs = args.map(function (v) { var msgs = args.map(function (v) {
...@@ -57,5 +60,5 @@ export default function formatLog () { ...@@ -57,5 +60,5 @@ export default function formatLog () {
msg = msgs[0] 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.
先完成此消息的编辑!
想要评论请 注册