-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.45 KB
/
index.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
const path = require('path')
const PluginError = require('plugin-error')
const through = require('through2')
const dependency = require('dependency-tree')
const merge = require('deepmerge')
const PLUGIN_NAME = 'gulp-circular-dependency'
let _dependencyStack = []
function travelTree (tree, cwd, callback) {
const dependencies = Object.keys(tree)
if (dependencies.length === 0) _dependencyStack = []
dependencies.forEach(dep => {
const relativePath = path.relative(cwd, dep)
if (_dependencyStack.indexOf(relativePath) > -1) {
_dependencyStack.push(relativePath)
throw new PluginError(
PLUGIN_NAME,
'Circular dependency: ' + _dependencyStack.join(' -> ')
)
} else {
_dependencyStack.push(relativePath)
}
travelTree(tree[dep], cwd, callback)
})
}
module.exports = function gulpCircularDependency (options = {}) {
return through.obj((file, encoding, callback) => {
if (file.isNull()) {
return callback(null, file)
}
const cwd = file.cwd
const filePath = file.path
const opt = merge({
filename: filePath,
directory: cwd,
detective: {
es6: {
mixedImports: true,
},
},
filter: path => {
return !(/node_modules/i.test(path))
},
}, options)
const tree = dependency(opt)
try {
travelTree(tree, cwd, callback)
callback(null, file)
} catch (err) {
callback(err, undefined)
}
})
}