From 5290af3d832e1046b1e828a36a4117d00b21c2da Mon Sep 17 00:00:00 2001
From: yylgit <394720943@qq.com>
Date: Mon, 25 May 2020 16:10:51 +0800
Subject: [PATCH 01/36] Add diffTag
---
src/adaptor/src/alibaba.js | 2 +-
src/adaptor/src/baidu.js | 2 +-
src/adaptor/src/toutiao.js | 2 +-
src/platform/alibaba/converter.js | 4 ++
src/platform/baidu/converter.js | 4 ++
src/platform/diff/diffTag.js | 36 ++++++++++
src/platform/diff/index.js | 5 ++
src/platform/toutiao/converter.js | 4 ++
test/diff/diffTag.js | 103 +++++++++++++++++++++++++++
test/diff/diffTag.test.js | 112 ++++++++++++++++++++++++++++++
10 files changed, 271 insertions(+), 3 deletions(-)
create mode 100644 src/platform/diff/diffTag.js
create mode 100644 src/platform/diff/index.js
create mode 100644 test/diff/diffTag.js
create mode 100644 test/diff/diffTag.test.js
diff --git a/src/adaptor/src/alibaba.js b/src/adaptor/src/alibaba.js
index 456637c..67570a1 100644
--- a/src/adaptor/src/alibaba.js
+++ b/src/adaptor/src/alibaba.js
@@ -22,7 +22,7 @@ function getInstance() {
// eslint-disable-next-line no-undef
const wx = my;
- if (wx.has_ali_hook_flag) return
+ if (wx.has_ali_hook_flag) return;
wx.has_ali_hook_flag = true;
diff --git a/src/adaptor/src/baidu.js b/src/adaptor/src/baidu.js
index 0a354cd..ab4e2fc 100644
--- a/src/adaptor/src/baidu.js
+++ b/src/adaptor/src/baidu.js
@@ -4,7 +4,7 @@ function getInstance() {
// eslint-disable-next-line no-undef
const wx = swan;
- if (wx.has_baidu_hook_flag) return
+ if (wx.has_baidu_hook_flag) return;
wx.has_baidu_hook_flag = true;
diff --git a/src/adaptor/src/toutiao.js b/src/adaptor/src/toutiao.js
index 6f3c3ea..21dc5f9 100644
--- a/src/adaptor/src/toutiao.js
+++ b/src/adaptor/src/toutiao.js
@@ -18,7 +18,7 @@ function ignoreFn(opt) {
function getInstance() {
var wx = tt;
- if (wx.has_toutiao_hook_flag) return
+ if (wx.has_toutiao_hook_flag) return;
wx.has_toutiao_hook_flag = true;
diff --git a/src/platform/alibaba/converter.js b/src/platform/alibaba/converter.js
index 8dfbd0d..a4b4589 100644
--- a/src/platform/alibaba/converter.js
+++ b/src/platform/alibaba/converter.js
@@ -16,6 +16,7 @@ const logger = require('../../utils/logger');
const converter = require('mp-converter').alibaba;
const scopeStyle = require('../../scope/scope-style');
const scopeTemplate = require('../../scope/scope-template');
+const diffTag = require('../diff/index').diffTag('alipay');
function convert(opt = {}) {
const src = opt.source || './src';
@@ -26,6 +27,7 @@ function convert(opt = {}) {
// 处理样式文件
gulp.src(`${src}/**/*.wxss`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxss(match)))
.pipe(through2.obj(function(file, enc, cb) {
const path = file.history[0].replace(file.base, '').replace('.wxss', '');
@@ -65,6 +67,7 @@ function convert(opt = {}) {
// 处理模板文件
gulp.src(`${src}/**/*.wxml`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxml(match)))
.pipe(through2.obj(function(file, enc, cb) {
const path = file.history[0].replace(file.base, '').replace('.wxml', '');
@@ -129,6 +132,7 @@ function convert(opt = {}) {
// 处理脚本文件
gulp.src(`${src}/**/*.js`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.script(match)))
.pipe(through2.obj(function(file, enc, cb) {
const path = file.history[0].replace(file.base, '');
diff --git a/src/platform/baidu/converter.js b/src/platform/baidu/converter.js
index 3fef542..e5c40e6 100644
--- a/src/platform/baidu/converter.js
+++ b/src/platform/baidu/converter.js
@@ -9,6 +9,7 @@ const replace = require('gulp-replace');
const config = require('../../config');
const logger = require('../../utils/logger');
const converter = require('mp-converter').baidu;
+const diffTag = require('../diff/index').diffTag('baidu');
const plugin = require('mp-plugin');
@@ -21,6 +22,7 @@ function convert(opt = {}) {
// 处理wxss
gulp.src(`${src}/**/*.wxss`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxss(match)))
.pipe(rename((path) => {
path.extname = ".css";
@@ -29,6 +31,7 @@ function convert(opt = {}) {
// 处理wxml
gulp.src(`${src}/**/*.wxml`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxml(match)))
.pipe(rename((path) => {
path.extname = ".swan";
@@ -65,6 +68,7 @@ function convert(opt = {}) {
// 处理js
return gulp.src(`${src}/**/*.js`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.script(match)))
.pipe(through2.obj(function(file, enc, cb) {
const path = file.history[0].replace(file.base, '');
diff --git a/src/platform/diff/diffTag.js b/src/platform/diff/diffTag.js
new file mode 100644
index 0000000..ea0e4d5
--- /dev/null
+++ b/src/platform/diff/diffTag.js
@@ -0,0 +1,36 @@
+const replace = require('gulp-replace');
+//
+const platformMap = {
+ wx: {
+ tagName: 'wwto-wx'
+ },
+ baidu: {
+ tagName: 'wwto-baidu'
+ },
+ alipay: {
+ tagName: 'wwto-alipay'
+ },
+ tt: {
+ tagName: 'wwto-tt'
+ }
+};
+
+module.exports = function(platform) {
+ const saveName = platformMap[platform].tagName;
+ const saveTagReg = new RegExp(`\\s*<${saveName}>([\\s\\S]*?)<\\/${saveName}>\\s*`, 'g');
+ let deleteTag = [];
+ Object.keys(platformMap).forEach(item => {
+ if (item !== platform) {
+ const delTagName = platformMap[item].tagName;
+ deleteTag.push(`(\\s*<${delTagName}>[\\s\\S]*?<\\/${delTagName}>\\s*)`);
+ }
+ });
+ deleteTag = new RegExp(deleteTag.join('|'), 'g');
+ return function(text) {
+ return text.replace(saveTagReg, function(match, $1) {
+ return $1;
+ }).replace(deleteTag, function(match) {
+ return '';
+ });
+ };
+};
\ No newline at end of file
diff --git a/src/platform/diff/index.js b/src/platform/diff/index.js
new file mode 100644
index 0000000..c9071e8
--- /dev/null
+++ b/src/platform/diff/index.js
@@ -0,0 +1,5 @@
+const diffTag = require('./diffTag');
+
+module.exports = {
+ diffTag
+};
\ No newline at end of file
diff --git a/src/platform/toutiao/converter.js b/src/platform/toutiao/converter.js
index 0acc851..a4c417a 100644
--- a/src/platform/toutiao/converter.js
+++ b/src/platform/toutiao/converter.js
@@ -9,6 +9,7 @@ const replace = require('gulp-replace');
const config = require('../../config');
const logger = require('../../utils/logger');
const converter = require('mp-converter').toutiao;
+const diffTag = require('../diff/index').diffTag('tt');
function convert(opt = {}) {
const src = opt.source || './src';
@@ -19,6 +20,7 @@ function convert(opt = {}) {
.pipe(gulp.dest(dest));
gulp.src(`${src}/**/*.wxss`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxss(match)))
.pipe(rename((path) => {
path.extname = ".ttss";
@@ -26,6 +28,7 @@ function convert(opt = {}) {
.pipe(gulp.dest(dest));
gulp.src(`${src}/**/*.wxml`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.wxml(match)))
.pipe(rename((path) => {
path.extname = ".ttml";
@@ -50,6 +53,7 @@ function convert(opt = {}) {
});
gulp.src(`${src}/**/*.js`)
+ .pipe(replace(/[\s\S]*/g, diffTag))
.pipe(replace(/[\s\S]*/g, (match) => converter.script(match)))
.pipe(through2.obj(function(file, enc, cb) {
const path = file.history[0].replace(file.base, '');
diff --git a/test/diff/diffTag.js b/test/diff/diffTag.js
new file mode 100644
index 0000000..2d42240
--- /dev/null
+++ b/test/diff/diffTag.js
@@ -0,0 +1,103 @@
+/* eslint-disable no-undef */
+const diffTag = require('../../src/platform/diff/diffTag.js');
+const wxDiff = diffTag('wx');
+const baiduDiff = diffTag('baidu');
+const alipayDiff = diffTag('alipay');
+const ttDiff = diffTag('tt');
+
+const code = `
+
+
+
+
+createCanvasContext
+const name = 'baidu';
+
+
+
+
+
+createCanvasContext
+const name = 'tt';
+
+
+
+
+
+createCanvasContext
+const name = 'alipay';
+
+
+`;
+
+const wxDiffResult = `
+
+createCanvasContext
+const name = 'wx';
+
+
+
+
+
+
+
+`;
+
+const baiduDiffResult = `
+
+
+
+createCanvasContext
+const name = 'baidu';
+
+
+
+
+
+`;
+
+const alipayDiffResult = `
+
+
+
+
+
+
+
+createCanvasContext
+const name = 'alipay';
+
+`;
+
+const ttDiffResult = `
+
+
+
+
+
+createCanvasContext
+const name = 'tt';
+
+
+
+`;
+const wxResult = wxDiff(code);
+debugger;
+console.log('wx', wxResult === wxDiffResult);
+const baiduResult = baiduDiff(code);
+console.log(baiduResult);
+console.log('baidu', baiduResult === baiduDiffResult);
+
+// let alipayResult = alipayDiff(code);
+// console.log(alipayResult)
+// console.log(alipayResult === alipayDiffResult)
+
+// let ttResult = ttDiff(code);
+// console.log(ttResult)
+// debugger
+// console.log(ttResult === ttDiffResult)
\ No newline at end of file
diff --git a/test/diff/diffTag.test.js b/test/diff/diffTag.test.js
new file mode 100644
index 0000000..a2276a8
--- /dev/null
+++ b/test/diff/diffTag.test.js
@@ -0,0 +1,112 @@
+/* eslint-disable no-undef */
+const diffTag = require('../../src/platform/diff/diffTag.js');
+const wxDiff = diffTag('wx');
+const baiduDiff = diffTag('baidu');
+const alipayDiff = diffTag('alipay');
+const ttDiff = diffTag('tt');
+
+function testCode(testName, code01, code02) {
+ test(testName || 'testing: ', () => {
+ expect(code01).toStrictEqual(code02);
+ });
+}
+const code = `
+
+
+createCanvasContext
+const name = 'wx';
+
+
+
+
+
+createCanvasContext
+const name = 'baidu';
+
+
+
+
+
+createCanvasContext
+const name = 'tt';
+
+
+
+
+
+createCanvasContext
+const name = 'alipay';
+
+
+`;
+
+const wxDiffResult = `
+
+createCanvasContext
+const name = 'wx';
+
+
+
+
+
+
+
+`;
+const baiduDiffResult = `
+
+
+
+createCanvasContext
+const name = 'baidu';
+
+
+
+
+
+`;
+
+const alipayDiffResult = `
+
+
+
+
+
+
+
+createCanvasContext
+const name = 'alipay';
+
+`;
+
+const ttDiffResult = `
+
+
+
+
+
+createCanvasContext
+const name = 'tt';
+
+
+
+`;
+testCode(
+ 'wx diffTag test:',
+ wxDiff(code),
+ wxDiffResult
+);
+testCode(
+ 'baidu diffTag test:',
+ baiduDiff(code),
+ baiduDiffResult
+);
+testCode(
+ 'tt diffTag test:',
+ ttDiff(code),
+ ttDiffResult
+);
+testCode(
+ 'alipay diffTag test:',
+ alipayDiff(code),
+ alipayDiffResult
+);
\ No newline at end of file
From f749ae6d49f6c8f8e22ddb6ba4e18a68b18defd9 Mon Sep 17 00:00:00 2001
From: yylgit <394720943@qq.com>
Date: Mon, 25 May 2020 16:11:07 +0800
Subject: [PATCH 02/36] add diffTag
---
src/platform/diff/diffTag.js | 1 -
test/diff/{diffTag.js => diffTag} | 0
2 files changed, 1 deletion(-)
rename test/diff/{diffTag.js => diffTag} (100%)
diff --git a/src/platform/diff/diffTag.js b/src/platform/diff/diffTag.js
index ea0e4d5..9a6a725 100644
--- a/src/platform/diff/diffTag.js
+++ b/src/platform/diff/diffTag.js
@@ -1,4 +1,3 @@
-const replace = require('gulp-replace');
//
const platformMap = {
wx: {
diff --git a/test/diff/diffTag.js b/test/diff/diffTag
similarity index 100%
rename from test/diff/diffTag.js
rename to test/diff/diffTag
From 6dd790cd35ad0c1726a527cfe99f38770e77a072 Mon Sep 17 00:00:00 2001
From: yylgit <394720943@qq.com>
Date: Tue, 26 May 2020 11:29:05 +0800
Subject: [PATCH 03/36] watch and diff
---
bin/index.js | 48 +
lib/index.js | 2 +-
lib/platform/alibaba/converter.js | 2 +-
lib/platform/baidu/converter.js | 2 +-
lib/platform/diff/diffTag.js | 1 +
lib/platform/diff/index.js | 1 +
lib/platform/toutiao/converter.js | 2 +-
lib/platform/wx/converter.js | 1 +
package-lock.json | 9437 +++++++++++++++++++++++++++++
package.json | 1 +
src/index.js | 8 +-
src/platform/diff/diffTag.js | 4 +-
src/platform/wx/converter.js | 49 +
test/diff/diffTag | 68 +-
test/diff/diffTag.test.js | 57 +-
yarn.lock | 7109 ++++++++++++++++++++++
16 files changed, 16753 insertions(+), 39 deletions(-)
create mode 100644 lib/platform/diff/diffTag.js
create mode 100644 lib/platform/diff/index.js
create mode 100644 lib/platform/wx/converter.js
create mode 100644 package-lock.json
create mode 100644 src/platform/wx/converter.js
create mode 100644 yarn.lock
diff --git a/bin/index.js b/bin/index.js
index af41f7a..184b6a3 100755
--- a/bin/index.js
+++ b/bin/index.js
@@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const logger = require('../lib/utils/logger');
const wto = require('../lib/index.js');
+const watch = require('glob-watcher');
function readFile(p) {
let rst = '';
@@ -76,8 +77,11 @@ commander.command('build')
wto.toAlibaba(cmd);
} else if (cmd.platform === 'toutiao') {
wto.toToutiao(cmd);
+ } else if (cmd.platform === 'wx') {
+ wto.toWx(cmd);
} else {
wto.toAll(Object.assign(true, cmd, {
+ wxTarget: [cmd.target, '/wx'].join('/'),
baiduTarget: [cmd.target, '/baidu'].join('/'),
alibabaTarget: [cmd.target, '/alibaba'].join('/'),
toutiaoTarget: [cmd.target, '/toutiao'].join('/')
@@ -85,6 +89,50 @@ commander.command('build')
}
});
+let compileIndex = 0;
+function watchFile(opt, callback) {
+ const src = opt.source || './src';
+ const watcher = watch([src + '/**/**.**']);
+ logger.info('编译中.......,编译索引:' + ++compileIndex);
+ callback();
+ watcher.on('change', function(path, stat) {
+ logger.info('编译中.......,编译索引:' + ++compileIndex);
+ callback();
+ });
+}
+commander.command('watch')
+ .description('dev模式编译项目')
+ .option('-p, --platform ', '目标平台')
+ .option('-s, --source