-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
195 lines (149 loc) · 6.63 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sebastien Dubois @dSebastien
*/
"use strict";
const path = require("path");
const fs = require("fs");
const loaderUtils = require("loader-utils");
const merge = require("merge");
// Helper functions
const _root = path.resolve(__dirname, "./"); // project root folder
function hasProcessFlag(flag){
return process.argv.join("").indexOf(flag) > -1;
}
function root(args){
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [ _root ].concat(args));
}
function loadEntriesData(files, debug){
let entriesData = [];
if(debug === true) {
console.log("json-files-merge-loader: loading entries data for "+files);
}
for(let entry of files){
if(debug === true) {
console.log("json-files-merge-loader: loading entry data for "+entry);
}
if(typeof entry !== "string"){
throw new Error("Entries in the files array of the file given to the json-files-merge-loader must be string objects!");
}
entry = entry.trim();
if(entry.length === 0){
throw new Error("Entries in the files array of the file given to the json-files-merge-loader cannot be empty!");
}
if(entry.startsWith("/")){
throw new Error("Entries in the files array of the file given to the json-files-merge-loader must define a relative path!");
}
if(!entry.endsWith(".json") && !entry.endsWith(".JSON")){
throw new Error("Entries in the files array of the file given to the json-files-merge-loader must be json files (file extension check)");
}
let entryPath = root(entry);
let entryData = undefined;
try{
entryData = fs.readFileSync(entryPath, 'utf8');
if(debug === true) {
console.log("json-files-merge-loader: loaded entry data: "+entryData);
}
} catch(e){
console.error("One of the entries in the files array given to the json-files-merge-loader is not accessible (does not exist, unreadable, ...): " + entryPath);
throw e;
}
if(!entryData){
throw new Error("One of the entries in the files array given to the json-files-merge-loader could not be read: " + entryPath);
}
// try to get a JSON object from the file data
let entryDataAsJSON = {};
try{
entryDataAsJSON = JSON.parse(entryData);
} catch(e){
console.error("One of the entries in the files array given to the json-files-merge-loader could not be parsed as a JSON object; it is probably not well formed! File in error: " + entryPath);
throw e;
}
if(typeof entryDataAsJSON !== 'object'){
throw new Error("One of the entries in the files array given to the json-files-merge-loader is not a JSON file. The json-files-merge-loader can only merge JSON files! File in error: " + entryPath);
}
if(debug === true) {
console.log("json-files-merge-loader: entry data successfully parsed as a JSON object");
}
// let's put the data aside for now
entriesData.push(entryDataAsJSON);
}
return entriesData;
}
module.exports = function (source) {
let debug = false;
if(this.debug && this.debug === true){
debug = true;
}
const config = loaderUtils.getLoaderConfig(this, "jsonMergeLoader");
if(config.debug === true) {
debug = true;
}else if(config.debug === false) {
debug = false;
}
if(debug === true) {
console.log("json-files-merge-loader: debug enabled");
}
// Load the json-files-merge-loader configuration
this.cacheable && this.cacheable(); // the result of this loader can be cached
if(typeof source !== "string"){
throw new Error("The input given to the json-files-merge-loader must be a string!");
}
if(debug === true) {
console.log("json-files-merge-loader: configuration contents: ",source);
}
let sourceAsJSON = {};
try{
sourceAsJSON = JSON.parse(source);
} catch(e){
console.error("The file given to the json-files-merge-loader couldn't be parsed as a JSON object; it is probably not well formed!");
throw e;
}
if(typeof sourceAsJSON !== 'object'){
throw new Error("The file given to the json-files-merge-loader is not a JSON file. The json-files-merge-loader requires a json configuration file!");
}
if(debug === true) {
console.log("json-files-merge-loader: successfully parsed the configuration file as a JSON object");
}
if(!sourceAsJSON.hasOwnProperty("files")){
throw new Error("The file given to the json-files-merge-loader must have a files property!");
}
if(debug === true) {
console.log("json-files-merge-loader: the configuration file contains the 'files' key as required");
}
if(sourceAsJSON.files.constructor !== Array){
throw new Error("The files property in the file given to the json-files-merge-loader must be an array!");
}
if(debug === true) {
console.log("json-files-merge-loader: the 'files' key is an array as required");
}
if(sourceAsJSON.files.length === 0){
throw new Error("The files property in the file given to the json-files-merge-loader must contain at least one entry!");
}
// will hold the data of all entries we load
let entriesData = loadEntriesData(sourceAsJSON.files, debug);
// will hold the result of this loader
let mergedContents = {};
for(let entryData of entriesData){
if(debug === true) {
console.log("json-files-merge-loader: about to merge ("+JSON.stringify(mergedContents)+") with ("+JSON.stringify(entryData)+")");
}
mergedContents = merge(mergedContents, entryData);
if(debug === true) {
console.log("json-files-merge-loader: merge result: "+JSON.stringify(mergedContents));
}
}
if(debug === true) {
console.log("json-files-merge-loader: finished merging all files. End result: " + mergedContents);
console.log("json-files-merge-loader: wrapping the merged contents in a cjs module");
}
// wrap the merged contents in an array
mergedContents = [mergedContents];
// define the module export containing the merged contents
let retVal = "module.exports = " + JSON.stringify(mergedContents, undefined, "\t") + ";";
if(debug === true) {
console.log("json-files-merge-loader: finished wrapping the merged contents in a cjs module: "+retVal);
}
return retVal;
};