| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- const path = require('path')
- const fs = require('fs')
- const gulp = require('gulp')
- const clean = require('gulp-clean')
- const sass = require('gulp-sass')(require('sass'));
- const less = require('gulp-less')
- const rename = require('gulp-rename')
- const gulpif = require('gulp-if')
- const sourcemaps = require('gulp-sourcemaps')
- const webpack = require('webpack')
- const gulpInstall = require('gulp-install')
- const gulpReplace = require('gulp-replace')
- const config = require('./config')
- const checkComponents = require('./checkcomponents')
- const checkWxss = require('./checkwxss')
- const _ = require('./utils')
- const jsConfig = config.js || {}
- const wxssConfig = config.wxss || {}
- const srcPath = config.srcPath
- const distPath = config.distPath
- const IGNORE_PATH = [
- './**/_*.scss',
- 'node_modules/**/*',
- 'miniprogram_npm/**/*'
- ]
- const sass2wxss = (PATH) => {
- return gulp
- .src(srcPath, {
- ignore: IGNORE_PATH,
- since: gulp.lastRun(sass2wxss)
- })
- .pipe(
- replace(/@import(.+?);/g, ($1, $2) =>
- /*! 这种注释不会被clean-css 处理 */
- $2.includes('./_') ? $1 : `/*! ${$1} */`
- )
- )
- .pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
- .pipe(replace(/\/\*!\s(@import\s+.+?;)\s\*\//g, ($1, $2) => $2))
- .pipe(rename({ extname: '.wxss' }))
- .pipe(gulp.dest('./'))
- }
-
- // gulp.task('transform sass to wxss', sass2wxss)
- /**
- * 获取 wxss sass流
- */
- function wxssSass(wxssFileList) {
- if (!wxssFileList.length) return false
- return gulp.src(wxssFileList, {cwd: srcPath, base: srcPath})
- // .pipe(gulpReplace(/@import\s*["|'][^"|']+["|'];?/gi, function (match) {
- // return `/**R**${match}**R**/`;
- // }))
- .pipe(sass())
- // 使@import引入样式不被转化
- // .pipe(gulpReplace(/\/\*{2}R\*{2}(@import\s*["|'][^"|']+["|'];?)\*{2}R\*{2}\//gi, function (match, p1) {
- // return p1.replace(/\.s+css/gi, '.wxss');
- // }))
- .pipe(rename({extname: '.wxss'}))
- .pipe(gulpif(wxssConfig.less && wxssConfig.sourcemap, sourcemaps.write('./')))
- .pipe(_.logger(wxssConfig.less ? 'generate' : undefined))
- .pipe(gulp.dest(distPath))
- }
- /**
- * 获取 wxss 流
- */
- function wxss(wxssFileList) {
- if (!wxssFileList.length) return false
- return gulp.src(wxssFileList, {cwd: srcPath, base: srcPath})
- .pipe(checkWxss.start()) // 开始处理 import
- .pipe(gulpif(wxssConfig.less && wxssConfig.sourcemap, sourcemaps.init()))
- .pipe(gulpif(wxssConfig.less, less({paths: [srcPath]})))
- .pipe(checkWxss.end()) // 结束处理 import
- .pipe(rename({extname: '.wxss'}))
- .pipe(gulpif(wxssConfig.less && wxssConfig.sourcemap, sourcemaps.write('./')))
- .pipe(_.logger(wxssConfig.less ? 'generate' : undefined))
- .pipe(gulp.dest(distPath))
- }
- /**
- * 获取 js 流
- */
- function js(jsFileMap, scope) {
- const webpackConfig = config.webpack
- const webpackCallback = (err, stats) => {
- if (!err) {
- // eslint-disable-next-line no-console
- console.log(stats.toString({
- assets: true,
- cached: false,
- colors: true,
- children: false,
- errors: true,
- warnings: true,
- version: true,
- modules: false,
- publicPath: true,
- }))
- } else {
- // eslint-disable-next-line no-console
- console.log(err)
- }
- }
- webpackConfig.entry = jsFileMap
- webpackConfig.output.path = distPath
- if (scope.webpackWatcher) {
- scope.webpackWatcher.close()
- scope.webpackWatcher = null
- }
- if (config.isWatch) {
- scope.webpackWatcher = webpack(webpackConfig).watch({
- ignored: /node_modules/,
- }, webpackCallback)
- } else {
- webpack(webpackConfig).run(webpackCallback)
- }
- }
- /**
- * 拷贝文件
- */
- function copy(copyFileList) {
- if (!copyFileList.length) return false
- return gulp.src(copyFileList, {cwd: srcPath, base: srcPath})
- .pipe(_.logger())
- .pipe(gulp.dest(distPath))
- }
- /**
- * 安装依赖包
- */
- function install() {
- return gulp.series(async () => {
- const demoDist = config.demoDist
- const demoPackageJsonPath = path.join(demoDist, 'package.json')
- const packageJson = _.readJson(path.resolve(__dirname, '../package.json'))
- const dependencies = packageJson.dependencies || {}
- await _.writeFile(demoPackageJsonPath, JSON.stringify({dependencies}, null, '\t')) // write dev demo's package.json
- }, () => {
- const demoDist = config.demoDist
- const demoPackageJsonPath = path.join(demoDist, 'package.json')
- return gulp.src(demoPackageJsonPath)
- .pipe(gulpInstall({production: true}))
- })
- }
- class BuildTask {
- constructor(id, entry) {
- if (!entry) return
- this.id = id
- this.entries = Array.isArray(config.entry) ? config.entry : [config.entry]
- this.copyList = Array.isArray(config.copy) ? config.copy : []
- this.componentListMap = {}
- this.cachedComponentListMap = {}
- this.init()
- }
- init() {
- const id = this.id
- /**
- * 清空目标目录
- */
- gulp.task(`${id}-clean-dist`, () => gulp.src(distPath, {read: false, allowEmpty: true}).pipe(clean()))
- /**
- * 拷贝 demo 到目标目录
- */
- let isDemoExists = false
- gulp.task(`${id}-demo`, gulp.series(async () => {
- const demoDist = config.demoDist
- isDemoExists = await _.checkFileExists(path.join(demoDist, 'project.config.json'))
- }, done => {
- if (!isDemoExists) {
- const demoSrc = config.demoSrc
- const demoDist = config.demoDist
- return gulp.src('**/*', {cwd: demoSrc, base: demoSrc})
- .pipe(gulp.dest(demoDist))
- }
- return done()
- }))
- /**
- * 安装依赖包
- */
- gulp.task(`${id}-install`, install())
- /**
- * 检查自定义组件
- */
- gulp.task(`${id}-component-check`, async () => {
- const entries = this.entries
- const mergeComponentListMap = {}
- for (let i = 0, len = entries.length; i < len; i++) {
- let entry = entries[i]
- entry = path.join(srcPath, `${entry}.json`)
- const newComponentListMap = await checkComponents(entry)
- _.merge(mergeComponentListMap, newComponentListMap)
- }
- this.cachedComponentListMap = this.componentListMap
- this.componentListMap = mergeComponentListMap
- })
- /**
- * 写 json 文件到目标目录
- */
- gulp.task(`${id}-component-json`, done => {
- const jsonFileList = this.componentListMap.jsonFileList
- if (jsonFileList && jsonFileList.length) return copy(jsonFileList)
- return done()
- })
- /**
- * 拷贝 wxml 文件到目标目录
- */
- gulp.task(`${id}-component-wxml`, done => {
- const wxmlFileList = this.componentListMap.wxmlFileList
- if (wxmlFileList &&
- wxmlFileList.length &&
- !_.compareArray(this.cachedComponentListMap.wxmlFileList, wxmlFileList)) {
- console.log('11')
- return copy(wxmlFileList)
- }
- return done()
- })
- /**
- * 生成 wxss 文件到目标目录
- */
- gulp.task(`${id}-component-wxss`, done => {
- const wxssFileList = this.componentListMap.wxssFileList
- if (wxssFileList &&
- wxssFileList.length &&
- !_.compareArray(this.cachedComponentListMap.wxssFileList, wxssFileList)) {
- return wxssSass(wxssFileList, srcPath, distPath)
- }
- return done()
- })
- /**
- * 拷贝 wxs 文件到目标目录
- */
- gulp.task(`${id}-component-wxs`, done => {
- const wxsFileList = this.componentListMap.wxsFileList
- if (wxsFileList &&
- wxsFileList.length &&
- !_.compareArray(this.cachedComponentListMap.wxsFileList, wxsFileList)) {
- console.log('22')
- return copy(wxsFileList)
- }
- return done()
- })
- /**
- * 生成 js 文件到目标目录
- */
- gulp.task(`${id}-component-js`, done => {
- const jsFileList = this.componentListMap.jsFileList
- if (jsFileList &&
- jsFileList.length &&
- !_.compareArray(this.cachedComponentListMap.jsFileList, jsFileList)) {
- if (jsConfig.webpack) {
- js(this.componentListMap.jsFileMap, this)
- } else {
- return copy(jsFileList)
- }
- }
- return done()
- })
- /**
- * 拷贝相关资源到目标目录
- */
- gulp.task(`${id}-copy`, gulp.parallel(done => {
- const copyList = this.copyList
- const copyFileList = copyList.map(copyFilePath => {
- try {
- if (fs.statSync(path.join(srcPath, copyFilePath)).isDirectory()) {
- return path.join(copyFilePath, '**/*.!(wxss)')
- } else {
- return copyFilePath
- }
- } catch (err) {
- // eslint-disable-next-line no-console
- console.error(err)
- return null
- }
- }).filter(copyFilePath => !!copyFilePath)
- if (copyFileList.length) return copy(copyFileList)
- return done()
- }, done => {
- const copyList = this.copyList
- const copyFileList = copyList.map(copyFilePath => {
- try {
- if (fs.statSync(path.join(srcPath, copyFilePath)).isDirectory()) {
- return path.join(copyFilePath, '**/*.wxss')
- } else if (copyFilePath.slice(-5) === '.wxss') {
- return copyFilePath
- } else {
- return null
- }
- } catch (err) {
- // eslint-disable-next-line no-console
- console.error(err)
- return null
- }
- }).filter(copyFilePath => !!copyFilePath)
- if (copyFileList.length) return wxss(copyFileList, srcPath, distPath)
- return done()
- }))
- /**
- * 监听 js 变化
- */
- gulp.task(`${id}-watch-js`, done => {
- if (!jsConfig.webpack) {
- return gulp.watch(this.componentListMap.jsFileList, {cwd: srcPath, base: srcPath}, gulp.series(`${id}-component-js`))
- }
- return done()
- })
- /**
- * 监听 json 变化
- */
- gulp.task(`${id}-watch-json`, () => gulp.watch(this.componentListMap.jsonFileList, {cwd: srcPath, base: srcPath}, gulp.series(`${id}-component-check`, gulp.parallel(`${id}-component-wxml`, `${id}-component-wxss`, `${id}-component-js`, `${id}-component-json`))))
- /**
- * 监听 wxml 变化
- */
- gulp.task(`${id}-watch-wxml`, () => {
- this.cachedComponentListMap.wxmlFileList = null
- return gulp.watch(this.componentListMap.wxmlFileList, {cwd: srcPath, base: srcPath}, gulp.series(`${id}-component-wxml`))
- })
- /**
- * 监听 wxss 变化
- */
- gulp.task(`${id}-watch-wxss`, () => {
- this.cachedComponentListMap.wxssFileList = null
- return gulp.watch('**/*.wxss', {cwd: srcPath, base: srcPath}, gulp.series(`${id}-component-wxss`))
- })
- /**
- * 监听相关资源变化
- */
- gulp.task(`${id}-watch-copy`, () => {
- const copyList = this.copyList
- const copyFileList = copyList.map(copyFilePath => {
- try {
- if (fs.statSync(path.join(srcPath, copyFilePath)).isDirectory()) {
- return path.join(copyFilePath, '**/*')
- } else {
- return copyFilePath
- }
- } catch (err) {
- // eslint-disable-next-line no-console
- console.error(err)
- return null
- }
- }).filter(copyFilePath => !!copyFilePath)
- const watchCallback = filePath => copy([filePath])
- return gulp.watch(copyFileList, {cwd: srcPath, base: srcPath})
- .on('change', watchCallback)
- .on('add', watchCallback)
- .on('unlink', watchCallback)
- })
- /**
- * 监听 demo 变化
- */
- gulp.task(`${id}-watch-demo`, () => {
- const demoSrc = config.demoSrc
- const demoDist = config.demoDist
- const watchCallback = filePath => gulp.src(filePath, {cwd: demoSrc, base: demoSrc})
- .pipe(gulp.dest(demoDist))
- return gulp.watch('**/*', {cwd: demoSrc, base: demoSrc})
- .on('change', watchCallback)
- .on('add', watchCallback)
- .on('unlink', watchCallback)
- })
- /**
- * 监听安装包列表变化
- */
- gulp.task(`${id}-watch-install`, () => gulp.watch(path.resolve(__dirname, '../package.json'), install()))
- /**
- * 构建相关任务
- */
- gulp.task(`${id}-build`, gulp.series(`${id}-clean-dist`, `${id}-component-check`, gulp.parallel(`${id}-component-wxml`,`${id}-component-wxs`,`${id}-component-wxss`, `${id}-component-js`, `${id}-component-json`, `${id}-copy`)))
- gulp.task(`${id}-watch`, gulp.series(`${id}-build`, `${id}-demo`, `${id}-install`, gulp.parallel(`${id}-watch-wxml`, `${id}-watch-wxss`, `${id}-watch-js`, `${id}-watch-json`, `${id}-watch-copy`, `${id}-watch-install`, `${id}-watch-demo`)))
- gulp.task(`${id}-dev`, gulp.series(`${id}-build`, `${id}-demo`, `${id}-install`))
- gulp.task(`${id}-default`, gulp.series(`${id}-build`))
- }
- }
- module.exports = BuildTask
|