-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
140 lines (112 loc) · 3.85 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Fetch depencies
const glob = require('glob');
const globParent = require('glob-parent');
const path = require('path');
let directories = [];
/**
* class WebpackWatchedGlobEntries
*/
class WebpackWatchedGlobEntries {
/**
*
* @param globs
* @param globOptions
* @param pluginOptions_
* @returns {Function}
*/
static getEntries(globs, globOptions, pluginOptions_) {
// Type check pluginOptions_
if (typeof pluginOptions_ !== 'undefined' && typeof pluginOptions_ !== 'object') {
throw new TypeError('pluginOptions_ must be an object');
}
// Options defaults
const pluginOptions = Object.assign({
basename_as_entry_id: false
}, pluginOptions_);
return function() {
// Check if globs are provided properly
if (typeof globs !== 'string' && !Array.isArray(globs)) {
throw new TypeError('globOptions must be a string or an array of strings');
}
// Check globOptions if provided properly
if (globOptions && typeof globOptions !== 'object') {
throw new TypeError('globOptions must be an object');
}
// Make entries an array
if (!Array.isArray(globs)) {
globs = [globs];
}
//
let globbedFiles = {};
// Map through the globs
globs.forEach(function(globString) {
let base = globParent(globString, {});
// Dont add if its already in the directories
if (directories.indexOf(base) === -1) {
directories.push(base);
}
// Get the globbedFiles
let files = WebpackWatchedGlobEntries.getFiles(globString, globOptions, pluginOptions.basename_as_entry_name);
// Set the globbed files
globbedFiles = Object.assign(files, globbedFiles);
});
return globbedFiles;
};
}
/**
* Create webpack file entry object
* @param globString
* @param globOptions
* @param basename_as_entry_name
* @returns {Object}
*/
static getFiles(globString, globOptions, basename_as_entry_name) {
const files = {};
let base = globParent(globString, {});
glob.sync(globString, globOptions).forEach(function(file) {
// Format the entryName
let entryName = path
.relative(base, file)
.replace(path.extname(file), '')
.split(path.sep)
.join('/');
if (basename_as_entry_name) {
entryName = path.basename(entryName);
}
// Add the entry to the files obj
files[entryName] = file;
});
return files;
}
/**
* Install Plugin
* @param {Object} compiler
*/
apply(compiler) {
if (compiler.hooks) {
// Support Webpack >= 4
compiler.hooks.afterCompile.tapAsync(this.constructor.name, this.afterCompile.bind(this));
} else {
// Support Webpack < 4
compiler.plugin('after-compile', this.afterCompile);
}
}
/**
* After compiling, give webpack the globbed files
* @param {Object} compilation
* @param {Function} callback
*/
afterCompile(compilation, callback) {
if (Array.isArray(compilation.contextDependencies)) {
// Support Webpack < 4
compilation.contextDependencies = compilation.contextDependencies.concat(directories);
} else {
// Support Webpack >= 4
for (const directory of directories) {
compilation.contextDependencies.add(path.normalize(directory));
}
}
callback();
}
}
module.exports = WebpackWatchedGlobEntries;