-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
124 lines (100 loc) · 2.84 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import fs from 'fs';
import path from 'path';
import del from 'del';
import vfs from 'vinyl-fs';
import { obj as thru } from 'through2';
import gulpPlumber from 'gulp-plumber';
import gulpBabel from 'gulp-babel';
import gulpConcat from 'gulp-concat';
import gulpUglify from 'gulp-uglify';
import babelArrowPlugin from 'babel-plugin-transform-es2015-arrow-functions';
// import * as pkg from './package.json';
let utils, develop, watchers = {}, output, source_cwd, cwd;
export function init(config, core) {
utils = core.utils;
develop = core.args.env() === 'develop';
output = config.output;
source_cwd = path.join(process.cwd(), config.cwd);
buildAll();
let bs = core.globals.get('bs');
if (develop && bs) {
// change/add callback
let modify = (file) => {
let name = path.basename(file, '.json') + '.js';
let contents = core.utils.json(path.join(source_cwd, file));
cwd = contents.cwd || source_cwd;
if (watchers.hasOwnProperty(name)) {
// build current list of files
build(contents, name);
// unwatch old list of files inside json
watchers[name].close();
}
// watch new list of files inside
watchers[name] = bs.watch(contents.files, {
cwd: cwd
})
.on('change', () => {
build(contents, name);
});
};
// delete callback
let unlink = (file) => {
let name = path.basename(file, '.json');
// unwatch files inside json
if (watchers.hasOwnProperty(name)) {
watchers[name].close();
}
// remove json's related js
del([name + '.js', name + '.map'], {
cwd: output
});
};
bs.watch('*.json', {
cwd: source_cwd,
ignoreInitial: false
})
.on('add', modify)
.on('change', modify)
.on('unlink', unlink);
}
}
function applyToFiles(cb) {
let items = fs.readdirSync(source_cwd)
.filter(function (file) {
return file.toString().endsWith('.json');
});
for (let i = 0, len = items.length; i < len; i++) {
cb(items[i]);
}
}
function build(contents, name) {
if (!contents) return false;
let sourcemaps = contents.sourcemaps !== undefined && contents.sourcemaps !== false;
let minimize = contents.minimize !== false || !develop;
if (contents.skip === true) {
sourcemaps = false;
minimize = false;
}
let opts = {
sourcemaps,
cwd: contents.cwd ?
path.join(process.cwd(), contents.cwd) : source_cwd
};
contents.files = utils.filterNonExistingFiles(contents.files, opts.cwd);
return vfs.src(contents.files, opts)
.pipe(gulpPlumber())
.pipe(gulpBabel({ plugins: [babelArrowPlugin] }))
.pipe(gulpConcat(name))
.pipe(minimize ? gulpUglify() : thru())
.pipe(vfs.dest(output, {
sourcemaps: sourcemaps ? '.' : false
}));
}
function buildAll() {
applyToFiles((file) => {
let name = path.basename(file, '.json') + '.js';
let contents = utils.json(path.join(source_cwd, file));
// build current list of files
build(contents, name);
});
}