UNPKG

1.63 kBJavaScriptView Raw
1///@ts-check
2'use strict';
3var path = require('path');
4var gulp = require('gulp');
5
6var extToSrc = require('../lib/ext-to-glob');
7var unlink = require('../lib/unlink');
8var compileWxss = require('../compiler/compile-wxss');
9var watchLog = require('../log/watch');
10
11var WXSS_EXTS = ['scss', 'sass', 'css', 'wxss'];
12
13/**
14 * @param {object} config
15 */
16function compile(config) {
17 return compileWxss(config, extToSrc(config, WXSS_EXTS));
18}
19
20/**
21 * @param {object} config
22 */
23exports.build = function (config) {
24 return function (cb) {
25 compile(config);
26 cb && cb();
27 };
28}
29
30function isAsset(file, config) {
31 return config.assets && file.startsWith(path.join(config.src, config.assets));
32}
33
34/**
35 * @param {object} config
36 */
37exports.watch = function (config) {
38 var update = function (file) {
39 // console.warn(file,JSON.stringify(arguments))
40 if (isAsset(file, config)) {
41 return compile(config);//依赖资源文件更改全部编译
42 } else {
43 return compileWxss(config, file);
44 }
45 };
46
47 return function (cb) {
48 var glob = extToSrc(config, WXSS_EXTS, true);
49 watchLog('wxss', glob)
50 gulp.watch(glob, {})
51 .on('change', update)
52 .on('add', update)
53 .on('unlink', function (file) {
54 if (isAsset(file, config)) {
55 return compile(config);
56 } else {
57 return unlink(config.src, config.dist, '.wxss')(file);
58 }
59 });
60 cb && cb();
61 }
62}
\No newline at end of file