-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (94 loc) · 2.88 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
var through = require('through2'),
gutil = require('gulp-util'),
fs = require('fs'),
path = require('path'),
imports = [],
PluginError = gutil.PluginError,
appRoot = path.dirname(module.parent.filename);
const PLUGIN_NAME = 'gulp-importjs';
function importStream(content) {
var stream = through();
stream.write(content);
return stream;
}
function importJS(appDir) {
// Allow passing of a new app root
if(typeof appDir === 'string'){
appRoot = appDir;
}
var stream = through.obj(function(file, enc, callback) {
// Reset the imports object
imports = [];
if (file.isNull()) {
// Do nothing
}
var compiled = null;
if (file.isBuffer()) {
compiled = replaceImports(file.contents.toString());
if (compiled.err) {
return callback(compiled.err);
}
file.contents = new Buffer(compiled.result);
}
if (file.isStream()) {
compiled = replaceImports(file.contents.toString());
if (compiled.err) {
return callback(compiled.err);
}
file.contents = file.contents.pipe(importStream(new Buffer(compiled.result)));
}
this.push(file);
return callback();
});
return stream;
}
//group - escaped tic, escaped quote, then anything, then escaped tic or escaped quote, then semicolon, then (optional) whitespace
var re = /^@import [\'\"](.+)[\'\"];[\s]*$/mi;
/**
* @return {Object} 2 properties : err (optional) and result
*/
function replaceImports(content) {
var err = null;
while ((match = re.exec(content)) !== null) {
match = match.map(function (argument) {
return argument.trim();
});
var fileName = appRoot + match[1].trim();
var match = match[0].toString();
// Check if the file we're trying to import exists
if (fs.existsSync(fileName)) {
// Check if the file was already imported somewhere
if (imports.indexOf(fileName) === -1) {
// Add the file to the imports list
imports.push(fileName);
// Read the imported file
// *** Replace dollar signs so submatches don't get replaced
importContents = fs.readFileSync(fileName, 'utf8').replace(/\$/g, '$$$$');
// Recursively import files
var recursiveResults = replaceImports(importContents);
if (recursiveResults.err) {
err = recursiveResults.err;
break;
}
importContents = recursiveResults.result; //replaceImports(importContents);
// Place the imported content into the file
content = content.replace(re, importContents + "\n", "mi");
}
// The file was already imported, comment out the line
else {
content = content.replace(re, "// Already included: " + fileName + "\n");
}
}
// The file does not exist, print out the error
else {
content = content.replace(re, "// Import file does not exist: " + fileName + "\n");
err = new PluginError('gulp-importjs', ' Import file does not exist: ' + fileName + '\n');
break;
}
}
return {
result : content,
err : err
};
}
module.exports = importJS;