From 35963d0117e17b8d138f3e89dbe84d14f65a519e Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 6 Jan 2020 18:59:41 +0800 Subject: [PATCH] refactor(cli): log --- .../__tests__/console.spec.js | 49 +++++++++++++++++++ .../__tests__/demo.js | 14 ++++++ .../babel-plugin-console/dist/index.js | 15 +++--- packages/vue-cli-plugin-uni/lib/format-log.js | 13 +++-- 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 packages/vue-cli-plugin-hbuilderx/__tests__/console.spec.js create mode 100644 packages/vue-cli-plugin-hbuilderx/__tests__/demo.js diff --git a/packages/vue-cli-plugin-hbuilderx/__tests__/console.spec.js b/packages/vue-cli-plugin-hbuilderx/__tests__/console.spec.js new file mode 100644 index 000000000..e06a4362e --- /dev/null +++ b/packages/vue-cli-plugin-hbuilderx/__tests__/console.spec.js @@ -0,0 +1,49 @@ +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");`) + }) +}) diff --git a/packages/vue-cli-plugin-hbuilderx/__tests__/demo.js b/packages/vue-cli-plugin-hbuilderx/__tests__/demo.js new file mode 100644 index 000000000..a54bd76cf --- /dev/null +++ b/packages/vue-cli-plugin-hbuilderx/__tests__/demo.js @@ -0,0 +1,14 @@ +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) +}) diff --git a/packages/vue-cli-plugin-hbuilderx/packages/babel-plugin-console/dist/index.js b/packages/vue-cli-plugin-hbuilderx/packages/babel-plugin-console/dist/index.js index 4a42087d7..7d2a87d57 100644 --- a/packages/vue-cli-plugin-hbuilderx/packages/babel-plugin-console/dist/index.js +++ b/packages/vue-cli-plugin-hbuilderx/packages/babel-plugin-console/dist/index.js @@ -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 + // ) + // ] } } } diff --git a/packages/vue-cli-plugin-uni/lib/format-log.js b/packages/vue-cli-plugin-uni/lib/format-log.js index f2faf2e53..236683be5 100644 --- a/packages/vue-cli-plugin-uni/lib/format-log.js +++ b/packages/vue-cli-plugin-uni/lib/format-log.js @@ -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) } -- GitLab