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