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来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
随机推荐
- Java_数据交换_fastJSON_01_用法入门
一.用法 1.序列化—将Object转为Json对象 Object data=JSON.toJSON( MyObject ); 注:本文的Object可以是Map.List.javaBean等 需求: ...
- 借助 frp 随时随地访问自己的树莓派
前言 看了知乎上的一个「树莓派」是什么以及普通人怎么玩? 的高票回答,双十一时间,果断买了一个树莓派 3. 周一(11.13) 到的货.我目前只想实现一个简单的功能 -- 想从任意位置访问我的树莓派. ...
- Winform C# 简单实现子窗口显示进度条
主窗口代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...
- python定时利用QQ邮件发送天气预报
大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...
- spring4新特性-泛型依赖注入
1 文件结构 2 具体类 2.1两个抽象类,在Service里面写公共的方法,在各自的具体实现类里面写各自的方法 package repo;import model.User;/** * Crea ...
- Cache类缓存
此处主要总结System.Web.Caching.Cache类 该类是用于存储常用信息的类,HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 该 ...
- js 图片转换为base64 (2)
<input type="file" id="testUpload"> <img src="" id="img& ...
- canvas动画之动态绘出六边形
先上 demo: http://en.jsrun.net/W5iKp/show 这两天我一直在研究这个动画,花了大量的时间来想是如何实现的, 一开始我是想在进入 canvas 时按时间来用 lineT ...
- 十四、Hadoop学习笔记————Zookeeper概述与基本概念
顺序一致性:严格按照顺序在zookeeper上执行 原子性:所有事物请求的结果,在整个集群的应用情况一致 单一视图:无论从哪个服务器进入集群,看到的东西都是一致的 可靠性:服务端成功响应后,状态会 一 ...
- 【转】javascript中的LHS与RHS
原文链接:http://www.cnblogs.com/yangxiaoguai132/p/5064625.html 最近在学习javascript过程中,接触了LHS与RHS的概念,刚开始的时候有点 ...