1 |
|
2 | "use strict";
|
3 | var gulp = require("gulp");
|
4 | var fs = require("fs");
|
5 |
|
6 | var log = require('fancy-log');
|
7 | var colors = require('colors/safe');
|
8 | var json5 = require('json5');
|
9 |
|
10 | var compileWxts = require("../compiler/compile-wxts");
|
11 | var unlink = require("../lib/unlink");
|
12 | var extToGlob = require("../lib/ext-to-glob");
|
13 | var watchLog = require("../log/watch");
|
14 |
|
15 | var WXTS_EXTS = ["wxts"];
|
16 | function 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 |
|
26 |
|
27 | exports.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 |
|
46 |
|
47 | exports.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 | };
|