mp-jsx - update gulp task

This commit is contained in:
dntzhang 2019-03-30 22:46:44 +08:00
parent a9c7a433af
commit a28264707f
2 changed files with 38 additions and 24 deletions

View File

@ -1,8 +1,9 @@
let gulp = require('gulp')
let path = require('path')
let tap = require('gulp-tap')
let fs = require('fs')
let jsx2wxml = require('./_scripts/jsx2wxml')
var gulp = require('gulp')
var path = require('path')
var tap = require('gulp-tap')
var fs = require('fs')
var jsx2wxml = require('./_scripts/jsx2wxml')
var watch = require('gulp-watch');
function buildComponent(code) {
return `
@ -12,7 +13,7 @@ class WeElement {
}
}`}
const baseOptions = {
var baseOptions = {
isRoot: false,
isApp: false,
sourcePath: __dirname,
@ -22,37 +23,49 @@ const baseOptions = {
}
gulp.task('watch', () => {
gulp.watch(['./**/*.jsx','!./node_modules/**','!./_scripts/**'], () => {
gulp.start('compile')
watch(['./**/*.jsx', '!./node_modules/**', '!./_scripts/**'], { events: ['add', 'change'] }, (evt, type) => {
var contents = fs.readFileSync(evt.path)
compile({
path: evt.path,
contents: contents.toString()
})
})
})
//加 cache同样的字符串返回同样的结果
gulp.task('compile', () => {
return gulp
.src(['./**/*.jsx','!./node_modules/**','!./_scripts/**'])
.src(['./**/*.jsx', '!./node_modules/**', '!./_scripts/**'])
.pipe(
tap(file => {
let dir = path.dirname(file.path)
let arr = dir.split(/\\|\//)
let name = arr[arr.length - 1]
console.log('【编译文件】' + file.path)
let template = jsx2wxml.default({
...baseOptions,
code: buildComponent(file.contents.toString())
}).template
console.log('【编译完成】' + file.path)
file.contents = new Buffer(template)
fs.writeFileSync(dir + '/' + name + '.wxml', file.contents.toString())
console.log('【写入文件】' + name + '.wxml')
console.log(1111)
compile({
path: file.path,
contents: file.contents.toString()
})
})
)
})
function compile(file) {
var dir = path.dirname(file.path)
var arr = dir.split(/\\|\//)
var name = arr[arr.length - 1]
console.log('【编译文件】' + file.path)
var template = jsx2wxml.default({
...baseOptions,
code: buildComponent(file.contents)
}).template
console.log('【编译完成】' + file.path)
fs.writeFileSync(dir + '/' + name + '.wxml', template)
console.log('【写入文件】' + name + '.wxml')
}
gulp.task('default', ['compile', 'watch'])
console.log('【开始编译】...')
gulp.start('default')

View File

@ -20,6 +20,7 @@
"babel-types": "^6.26.0",
"gulp": "^3.9.1",
"gulp-tap": "^1.0.1",
"gulp-watch": "^5.0.1",
"lodash": "^4.17.11"
}
}