-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathremoteDepsParser.ts
More file actions
36 lines (34 loc) · 1.2 KB
/
remoteDepsParser.ts
File metadata and controls
36 lines (34 loc) · 1.2 KB
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
/**
*
* Concatenates the remote dependencies into a comma separated string.
* this string will then be passed as an argument to the "importScripts" function
*
* @param {Array.<String>} deps array of string
* @param {Array.<Function>} localDeps array of function
* @returns {String} a string composed by the concatenation of the array
* elements "deps" and "importScripts".
*
* @example
* remoteDepsParser(['http://js.com/1.js', 'http://js.com/2.js']) // importScripts('http://js.com/1.js', 'http://js.com/2.js')
*/
const remoteDepsParser = (deps: string[], localDeps: Function[]) => {
if (deps.length === 0 && localDeps.length === 0) return "";
const depsString = deps.map((dep) => `'${dep}'`).toString();
const depsFunctionString = localDeps
.filter((dep) => typeof dep === "function")
.map((fn) => {
const str = fn.toString();
if (str.trim().startsWith("function")) {
return str;
} else {
const name = fn.name;
return `const ${name} = ${str}`;
}
})
.join(";");
const importString = `importScripts(${depsString});`;
return `${
depsString.trim() === "" ? "" : importString
} ${depsFunctionString}`;
};
export default remoteDepsParser;