gulp提高微信小程序开发效率
const fs = require('fs')
const path = require('path')
const gulpLoadPlugins = require('gulp-load-plugins')
const del = require('del')
const runSequence = require('run-sequence')
const inquirer = require('inquirer')
const generatePage = require('generate-weapp-page')
const plugins = gulpLoadPlugins()
const api = require('./src/utils/config')
const env = process.env.NODE_ENV || 'development'
const isProduction = () => env === 'production'
function generateFile (options) {
const files = generatePage({
root: path.resolve(__dirname, './src/pages/'),
name: options.pageName,
scss: options.styleType === 'scss',
css: options.styleType === 'css',
json: options.needConfig
})
files.forEach && files.forEach(file => plugins.util.log('[generate]', file))
return files
}
const filename = path.resolve(__dirname, 'src/app.json')
const now = fs.readFileSync(filename, 'utf8')
const temp = now.split('\n // Dont remove this comment')
if (temp.length !== 2) {
return plugins.util.log('[generate]', 'Append json failed')
}
const result = `${temp[0].trim()},
"pages/${options.pageName}/${options.pageName}"
${temp[1].trim()}
`
fs.writeFileSync(filename, result)
}
* 清除文件
*/
gulp.task('clean', del.bind(null, ['dist/*']))
* 添加eslint规范
*/
gulp.task('lint', () => {
return gulp.src(['*.{js,json}', '**/*.{js,json}', '!node_modules/**', '!dist/**', '!**/bluebird.js'])
.pipe(plugins.eslint())
.pipe(plugins.eslint.format('node_modules/eslint-friendly-formatter'))
.pipe(plugins.eslint.failAfterError())
})
* 编译js文件
*/
gulp.task('compile:js', () => {
return gulp.src(['src/**/*.js'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.babel())
.pipe(plugins.if(isProduction, plugins.uglify()))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
* 编译wxml文件
*/
gulp.task('compile:wxml', () => {
return gulp.src(['src/**/*.wxml'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.if(isProduction, plugins.htmlmin({
collapseWhitespace: true,
// collapseBooleanAttributes: true,
// removeAttributeQuotes: true,
keepClosingSlash: true, // wxml
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})))
.pipe(plugins.rename({ extname: '.wxml' }))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
/**
* 将scss转为css并输出到dist目录
*/
gulp.task('compile:scss', () => {
return gulp.src(['src/**/*.scss'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass())
.pipe(plugins.if(isProduction, plugins.cssnano({ compatibility: '*' })))
.pipe(plugins.rename({ extname: '.wxss' }))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
* 编译json文件
*/
gulp.task('compile:json', () => {
return gulp.src(['src/**/*.json'])
.pipe(plugins.sourcemaps.init())
.pipe(plugins.jsonminify())
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('dist'))
})
* 编译图片文件
*/
gulp.task('compile:img', () => {
return gulp.src(['src/**/*.{jpg,jpeg,png,gif}'])
.pipe(plugins.imagemin())
.pipe(gulp.dest('dist'))
})
* 编译源文件到目标目录
*/
gulp.task('compile', ['clean'], next => {
runSequence([
'compile:js',
'compile:wxml',
'compile:scss',
'compile:json',
'compile:img'
], next)
})
* 复制文件到dist目录
*/
gulp.task('extras', [], () => {
return gulp.src([
'src/**/*.*',
'!src/**/*.js',
'!src/**/*.wxml',
'!src/**/*.scss',
'!src/**/*.json',
'!src/**/*.{jpe?g,png,gif}'
])
.pipe(gulp.dest('dist'))
})
* Build
*/
gulp.task('build', ['lint'], next => runSequence(['compile', 'extras'], next))
* 监听文件变化
*/
gulp.task('watch', ['build'], () => {
gulp.watch('src/**/*.js', ['compile:js'])
gulp.watch('src/**/*.wxml', ['compile:wxml'])
gulp.watch('src/**/*.scss', ['compile:scss'])
gulp.watch('src/**/*.json', ['compile:json'])
gulp.watch('src/**/*.{jpe?g,png,gif}', ['compile:img'])
})
* 执行gulp generate自动生成文件目录,会自动生成一个文件目录,包括wxml,scss,json,js文件,并把页面加到app.json中
*/
gulp.task('generate', next => {
inquirer.prompt([
{
type: 'input',
name: 'pageName',
message: 'Input the page name',
default: 'index'
},
{
type: 'confirm',
name: 'needConfig',
message: 'Do you need a configuration file',
default: false
},
{
type: 'list',
name: 'styleType',
message: 'Select a style framework',
// choices: ['less', 'scss', 'css'],
choices: ['scss'],
default: 'scss'
}
])
.then(options => {
const res = generateFile(options)
if (res) generateJson(options)
})
.catch(err => {
throw new plugins.util.PluginError('generate', err)
})
})
* 默认任务
*/
gulp.task('default', ['watch'])
"lint": "gulp lint",
"dev": "cross-env NODE_ENV=development gulp watch",
"generate": "gulp generate",
"build": "cross-env NODE_ENV=production gulp build"
},
gulp提高微信小程序开发效率的更多相关文章
- 微信小程序开发03-这是一个组件
编写组件 基本结构 接上文:微信小程序开发02-小程序基本介绍 我们今天先来实现这个弹出层: 之前这个组件是一个容器类组件,弹出层可设置载入的html结构,然后再设置各种事件即可,这种组件有一个特点: ...
- 微信小程序开发教程 #043 - 在小程序开发中使用 npm
本文介绍了如何在微信小程序开发中使用 npm 中包的功能,大大提高微信小程序的开发效率,同时也是微信小程序系列教程的视频版更新. 微信小程序在发布之初没有对 npm 的支持功能,这也是目前很多前端开发 ...
- 微信小程序开发中的二三事之网易云信IMSDK DEMO
本文由作者邹永胜授权网易云社区发布. 简介 为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程.用产品的话说就是: 云信 IM 小程序 SDK 的能力 ...
- 让微信小程序开发如鱼得水
关于微信小程序开发一直想写一篇相关的文章总结和记录下,结果拖延症犯了迟迟没有下笔:这不最近天气不错,于是找一个空闲的下午将这篇文章输出下(好像跟天气没啥关系),那我们就开始吧! 注意:本文默认开发 ...
- VSCode 微信小程序 开发环境配置 详细教程
本博客已暂停更新,需要请转新博客http://www.whbwiki.com/231.html 配置 VsCode 微信小程序开发环境并非不用官方的 微信小程序开发者工具 ,而是两者配合适用,可以极大 ...
- 微信小程序开发库grace vs wepy
grace和wepy都是辅助小程序开发的开源库,本文对两者做个对比. 注:本文是作者本人的一些拙见,纯粹的技术讨论,不想引起技术信仰之争,欢迎积极.正向的讨论及建议. 如果你还不了解Grace, 请参 ...
- 微信小程序开发 (资料汇总,谁还没被坑过?希望助你绕过一些坑)
最近帮人家做一个微信小程序,刚好想熟悉一下.由于牵扯到多用户使用系统,以及数据共享,所以自然架构选择了,客户端和服务器的方式. 后台服务器是windows server,后台程序是.Net WebA ...
- 零基础入门微信小程序开发
注:本文来源于:<零基础入门微信小程序开发> 课程介绍 本达人课是一个系列入门教程,目标是从 0 开始带领读者上手实战,课程以微信小程序的核心概念作为主线,介绍配置文件.页面样式文件.Ja ...
- 微信小程序开发学习资料
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
随机推荐
- 基础进阶(一)之HashMap实现原理分析
HashMap实现原理分析 1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二 ...
- [转载] 理解OAuth 2.0
转载自http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...
- Bash 常用快捷键(转)
在mtysql client中按HOME键和END键失效,查了也没有找到原因 使用Bash常用的快捷方式即可. http://blog.csdn.net/mingzhou/article/detail ...
- MySQL解压版配置步骤详细教程
解压mysql到D盘根目录 在解压路径下复制my-default.ini,修改名称为my.ini 在my.ini内容如下 [client]default-character-set=utf8 [mys ...
- 从vultr购买到搭ss看世界
title: 从Vultr购买到搭ss看世界 date: 2017-11-19 12:28:38 categories: 技术 tags: top: 800 password: 写在前面 服务器提 ...
- JavaScript(五):函数(闭包,eval)
1.函数的申明:三种方法: function命令 函数表达式:变量赋值 Function构造函数 //method 1: function命令 function test(){ console.log ...
- struts2 action接收请求参数和类型转换
1,action接收请求参数 在struts2中action是什么?(struts2是一个mvc框架) V:jsp M:action C:action ...
- Numpy数组的基本运算操作
一.算术运算符 In [3]: a = np.arange(0,5) Out[3]array([0, 1, 2, 3, 4]) In [4]: a+4 Out[4]: array([4, 5, 6, ...
- 快速上手使用Maven
maven的相关命令 mvn archetype:create :创建 Maven 项目 mvn compile :编译源代码(编译到target文件夹中) mvn test-compile :编译测 ...
- 搭建一个属于自己的webpack config(-)
搭建一个属于自己的webpack config(-) 前期准备 环境说明 mac 10.12.6 node v8.8.1 npm 5.4.2 全局安装下webpack.webpack-dev-serv ...