This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
forked from edrlab/thorium-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeExternals.js
215 lines (182 loc) · 7.41 KB
/
nodeExternals.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// const nodeExternals = require("webpack-node-externals");
// module.exports = nodeExternals;
// https://raw.githubusercontent.com/liady/webpack-node-externals/master/index.js
var fs = require("fs");
var path = require("path");
var scopedModuleRegex = new RegExp('@[a-zA-Z0-9][\\w-.]+\/[a-zA-Z0-9][\\w-.]+([a-zA-Z0-9.\/]+)?', 'g');
var atPrefix = new RegExp('^@', 'g');
function contains(arr, val) {
return arr && arr.indexOf(val) !== -1;
}
function readDir(dirName) {
try {
return fs.readdirSync(dirName).map(function(module) {
if (atPrefix.test(module)) {
// reset regexp
atPrefix.lastIndex = 0;
try {
return fs.readdirSync(path.join(dirName, module)).map(function(scopedMod) {
return module + '/' + scopedMod;
});
} catch (e) {
return [module];
}
}
return module
}).reduce(function(prev, next) {
return prev.concat(next);
}, []);
} catch (e) {
console.log(e);
return [];
}
}
function readFromPackageJson() {
var packageJson;
try {
var packageJsonString = fs.readFileSync(path.join(process.cwd(), './package.json'), { encoding: 'utf8' });
packageJson = JSON.parse(packageJsonString);
} catch (e){
return [];
}
var sections = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'];
var deps = {};
sections.forEach(function(section){
Object.keys(packageJson[section] || {}).forEach(function(dep){
deps[dep] = true;
});
});
return Object.keys(deps);
}
function containsPattern(arr, val) {
return arr && arr.some(function(pattern){
if(pattern instanceof RegExp){
return pattern.test(val);
} else if (typeof pattern === 'function') {
return pattern(val);
} else {
return pattern == val;
}
});
}
function getModuleName(request, includeAbsolutePaths) {
var req = request;
var delimiter = '/';
if (includeAbsolutePaths) {
req = req.replace(/^.*?\/node_modules\//, '');
}
// check if scoped module
if (scopedModuleRegex.test(req)) {
// reset regexp
scopedModuleRegex.lastIndex = 0;
return req.split(delimiter, 2).join(delimiter);
}
return req.split(delimiter)[0];
}
module.exports = function nodeExternals(options) {
options = options || {};
var processName = options.processName || '??';
var alias = options.alias || {};
var whitelist = [].concat(options.whitelist || []);
var binaryDirs = [].concat(options.binaryDirs || ['.bin']);
var importType = options.importType || 'commonjs';
var modulesDir = options.modulesDir || 'node_modules';
var modulesFromFile = !!options.modulesFromFile;
var includeAbsolutePaths = !!options.includeAbsolutePaths;
// helper function
function isNotBinary(x) {
return !contains(binaryDirs, x);
}
// create the node modules list
var nodeModules = modulesFromFile ? readFromPackageJson() : readDir(modulesDir).filter(isNotBinary);
const alreadyProcessed = [];
// return an externals function
return function(context, request, callback){
let forceDebug = false;
const debug = true;
const isR2 = /^r2-.+-js/.test(request); // EXTERNAL
const isRDesk = request.indexOf("readium-desktop/") === 0; // BUNDLE
// EXTERNAL (built-ins)
const isElectron = request.indexOf("electron") === 0;
const isNode = request.indexOf("fs") === 0 ||
request.indexOf("path") === 0;
const isRelative = request.indexOf(".") === 0;
const isRelativeInNodeModules = isRelative && context.indexOf("/node_modules/") >= 0;
const isWebPack = request.indexOf("webpack") >= 0 ||
isRelative && context.indexOf("/node_modules/webpack") >= 0;
const isCSSLoader = request.indexOf("css-loader") >= 0 || request.indexOf("css-hot-loader") >= 0;
const isCSS = request.endsWith(".css");
const isSVG = request.endsWith(".svg");
const token = isRelative ? context + request : request;
const isAlready = alreadyProcessed.indexOf(token) >= 0;
if (!isAlready) {
alreadyProcessed.push(token);
}
const isR2Alias = /^@r2-.+-js/.test(request); // EXTERNAL
if (isR2Alias || isRelativeInNodeModules || isWebPack || isSVG || isCSS || isCSSLoader) {
forceDebug = true;
}
// doesn't necessarily mean that WebPack will bundle, just that it won't externalize
const makeBundle = isWebPack || isSVG || isCSS || isCSSLoader ||
(!isR2 && !isR2Alias && !isRelativeInNodeModules &&
(isRDesk || isRelative || isElectron || isNode ||
request === "xxxpouchdb-core" // No need to force-bundle, as we now fixed di.ts to test for "default" property
));
let makeExternal = !makeBundle;
if (makeExternal &&
!isR2 && !isR2Alias && !isRelativeInNodeModules) {
const moduleName = getModuleName(request, includeAbsolutePaths);
makeExternal = contains(nodeModules, moduleName) && !containsPattern(whitelist, request);
if (forceDebug ||
(debug &&
!makeExternal &&
!isAlready)) {
console.log(processName + "__ LOOKUP: [" + request + "] " + context + " (" + moduleName + ")");
}
}
if (makeExternal) {
if (forceDebug ||
(debug &&
!isAlready &&
!isR2 && !isR2Alias)) {
console.log(processName + "__ EXTERNAL: [" + request + "] " + context);
}
// mark this module as external
// https://webpack.github.io/docs/configuration.html#externals
// https://webpack.js.org/configuration/externals/
// MAKES NO DIFFERENCE
// if (request === "pouchdb-core") {
// console.log("pouchdb-core CommonJS2");
// return callback(null, "commonjs2 " + request);
// }
let request_ = request;
if (isR2Alias && alias) {
const iSlash = request.indexOf("/");
const key = request.substr(0, (iSlash >= 0) ? iSlash : request.length);
if (alias[key]) {
request_ = request.replace(key, alias[key]);
if (forceDebug || debug) {
console.log(processName + "__ ALIAS: [" + request + "] => " + request_);
}
}
} else if (isRelativeInNodeModules) {
request_ = request_.replace("./", "");
request_ = context + "/" + request_;
request_ = request_.replace(/^.*?\/node_modules\//, '');
if (forceDebug || debug) {
console.log(processName + "__ RELATIVE: [" + request + "] => " + request_);
}
}
return callback(null, importType + " " + request_);
};
if (forceDebug ||
(debug &&
!isAlready &&
!isRDesk &&
!isElectron &&
!isNode)) {
console.log(processName + "__ BUNDLE: [" + request + "] " + context);
}
callback();
}
}