UNPKG

1.8 kBJavaScriptView Raw
1///@ts-check
2"use strict";
3var gulp = require("gulp");
4var fs = require("fs");
5
6var log = require('fancy-log');
7var colors = require('colors/safe');
8var json5 = require('json5');
9
10var compileWxts = require("../compiler/compile-wxts");
11var unlink = require("../lib/unlink");
12var extToGlob = require("../lib/ext-to-glob");
13var watchLog = require("../log/watch");
14
15var WXTS_EXTS = ["wxts"];
16function compile(config, file) {
17 var tsconfig = {};
18 if (config.tsconfig) {
19 var buff = fs.readFileSync(config.tsconfig);
20 tsconfig = json5.parse(buff.toString());
21 }
22 return compileWxts(config, file, tsconfig);
23}
24/**
25 * @param {object} config
26 */
27exports.build = function (config) {
28 if (config.tsconfig || fs.existsSync("tsconfig.json")) {
29 return function () {
30 config.tsconfig = config.tsconfig || "tsconfig.json";
31 return compile(config, extToGlob(config, WXTS_EXTS))
32 }
33 } else {
34 log(
35 colors.cyan('wxts:'),
36 colors.gray('`tsconfig.json` was found. Skip WXTS(WeiXin TypesSript) compilation!')
37 );
38 return function (cb) {
39 cb && cb();
40 }
41 }
42};
43
44/**
45 * @param {object} config
46 */
47exports.watch = function (config) {
48 return function (cb) {
49 var glob = extToGlob(config, WXTS_EXTS);
50 watchLog("wxts", glob);
51 gulp.watch(glob, {
52 ignored: config.src + "/*/**.d.ts",
53 })
54 .on("change", function (file) {
55 return compile(config, file);
56 })
57 .on("add", function (file) {
58 return compile(config, file);
59 })
60 .on("unlink", unlink(config.src, config.dist, ".wxs"));
61 cb && cb();
62 };
63};