Created
March 14, 2019 08:53
-
-
Save WillieYang/d446156130faf0dd4a3a479042df4f7c to your computer and use it in GitHub Desktop.
Using gulp to execute different tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gulpfile.js | |
let path = require('path') | |
let gulp = require('gulp') | |
let cleanCSS = require('gulp-clean-css') | |
let cssWrap = require('gulp-css-wrap') | |
let replace = require('gulp-replace'); | |
let cn = require('./i18n/src/cn'); | |
gulp.task('css-wrap', function () { | |
return gulp.src(path.resolve('./theme/default/index.css')) | |
/* 找需要添加命名空间的css文件,支持正则表达式 */ | |
.pipe(cssWrap({ | |
selector: '.theme-default' /* 添加的命名空间 */ | |
})) | |
.pipe(cleanCSS()) | |
.pipe(gulp.dest('src/assets/css/theme/default')) /* 存放的目录 */ | |
}) | |
// custom match | |
gulp.task('i18n', function(){ | |
gulp.src(path.resolve('./src/layout/sysadminLayout.js')) | |
.pipe(replace(/'[\u4e00-\u9fa5]+/g, function(match) { | |
let template; | |
for (let key in cn) { | |
let value = cn[key] | |
if(value == match) { template = key } | |
} | |
if (template) { | |
let str = `i18n.t('${template}')` | |
console.log(`i18n: ${template}`) | |
return str | |
} else { | |
return match | |
} | |
})) | |
.pipe(gulp.dest('i18n/dest')); | |
}); | |
// l for layout match | |
gulp.task('i18n-l', function(){ | |
gulp.src(path.resolve('./src/components/base/service-log.vue')) | |
.pipe(replace(/["][\u4e00-\u9fa5]+["]/g, function(match) { | |
let template; | |
let tmp = match.split('"').join(''); | |
for (let key in cn) { | |
let value = cn[key] | |
if(value == tmp) { template = key } | |
} | |
if (template) { | |
let str = `"$t('${template}')"` | |
console.log(`"$t('${template}')"`) | |
return str | |
} else { | |
return match | |
} | |
})) | |
.pipe(gulp.dest('i18n/dest/components/base')); | |
}); | |
// t for template match | |
gulp.task('i18n-t', function(){ | |
gulp.src(path.resolve('./src/components/base/service-log.vue')) | |
.pipe(replace(/[>][\u4e00-\u9fa5]+[<]/g, function(match) { | |
let template; | |
let tmp = match.split('>').pop().split('<')[0]; | |
for (let key in cn) { | |
let value = cn[key] | |
if(value == tmp) { template = key } | |
} | |
if (template) { | |
// let str = `i18n.t('${template}')` | |
let str = `>{{ $t('${template}') }}<` | |
console.log(`i18n: ${template}`) | |
return str | |
} else { | |
return match | |
} | |
})) | |
.pipe(gulp.dest('i18n/dest/components/base')); | |
}); | |
// g for globally match | |
gulp.task('i18n-g', function(){ | |
gulp.src(path.resolve('./src/base/sockjsMsg.js')) | |
.pipe(replace(/[\u4e00-\u9fa5]+/g, function(match) { | |
console.log(`Matched: ${match}`) | |
for (let key in cn) { | |
let value = cn[key] | |
if(value == match) { | |
console.log(`Existed: ${value}`) | |
break; | |
} | |
} | |
console.log('\n') | |
})) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment