UNPKG

4.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getNewUserRequest = getNewUserRequest;
7exports.getExposes = getExposes;
8exports.contextify = contextify;
9exports.stringifyRequest = stringifyRequest;
10exports.interpolateName = interpolateName;
11
12var _path = _interopRequireDefault(require("path"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function getNewUserRequest(request) {
17 const splittedRequest = request.split("!");
18 const lastPartRequest = splittedRequest.pop().split("?", 2);
19
20 const pathObject = _path.default.parse(lastPartRequest[0]);
21
22 pathObject.base = `${_path.default.basename(pathObject.base, pathObject.ext)}-exposed${pathObject.ext}`;
23 lastPartRequest[0] = _path.default.format(pathObject);
24 splittedRequest.push(lastPartRequest.join("?"));
25 return splittedRequest.join("!");
26}
27
28function splitCommand(command) {
29 const result = command.split("|").map(item => item.split(" ")).reduce((acc, val) => acc.concat(val), []);
30
31 for (const item of result) {
32 if (!item) {
33 throw new Error(`Invalid command "${item}" in "${command}" for expose. There must be only one separator: " ", or "|".`);
34 }
35 }
36
37 return result;
38}
39
40function parseBoolean(string, defaultValue = null) {
41 if (typeof string === "undefined") {
42 return defaultValue;
43 }
44
45 switch (string.toLowerCase()) {
46 case "true":
47 return true;
48
49 case "false":
50 return false;
51
52 default:
53 return defaultValue;
54 }
55}
56
57function resolveExposes(item) {
58 let result;
59
60 if (typeof item === "string") {
61 const splittedItem = splitCommand(item.trim());
62
63 if (splittedItem.length > 3) {
64 throw new Error(`Invalid "${item}" for exposes`);
65 }
66
67 result = {
68 globalName: splittedItem[0],
69 moduleLocalName: splittedItem[1],
70 override: typeof splittedItem[2] !== "undefined" ? parseBoolean(splittedItem[2], false) : // eslint-disable-next-line no-undefined
71 undefined
72 };
73 } else {
74 result = item;
75 }
76
77 const nestedGlobalName = typeof result.globalName === "string" ? result.globalName.split(".") : result.globalName;
78 return { ...result,
79 globalName: nestedGlobalName
80 };
81}
82
83function getExposes(items) {
84 let result;
85 const exposeItems = typeof items === "string" && items.includes(",") ? items.split(",") : items;
86
87 if (typeof exposeItems === "string") {
88 result = [resolveExposes(exposeItems)];
89 } else {
90 result = [].concat(exposeItems).map(item => resolveExposes(item));
91 }
92
93 return result;
94}
95
96function contextify(context, request) {
97 return request.split("!").map(r => {
98 const splitPath = r.split("?");
99
100 if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
101 splitPath[0] = _path.default.win32.relative(context, splitPath[0]);
102
103 if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
104 splitPath[0] = splitPath[0].replace(/\\/g, "/");
105 }
106 }
107
108 if (/^\//.test(splitPath[0])) {
109 splitPath[0] = _path.default.posix.relative(context, splitPath[0]);
110 }
111
112 if (!/^(\.\.\/|\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
113 splitPath[0] = `./${splitPath[0]}`;
114 }
115
116 return splitPath.join("?");
117 }).join("!");
118}
119
120function isAbsolutePath(str) {
121 return _path.default.posix.isAbsolute(str) || _path.default.win32.isAbsolute(str);
122}
123
124function stringifyRequest(loaderContext, request) {
125 const splitted = request.split("!");
126 const context = loaderContext.context || loaderContext.options && loaderContext.options.context;
127 return JSON.stringify(splitted.map(part => {
128 // First, separate singlePath from query, because the query might contain paths again
129 const splittedPart = part.match(/^(.*?)(\?.*)/);
130 const query = splittedPart ? splittedPart[2] : "";
131 let singlePath = splittedPart ? splittedPart[1] : part;
132
133 if (isAbsolutePath(singlePath) && context) {
134 singlePath = _path.default.relative(context, singlePath);
135 }
136
137 return singlePath.replace(/\\/g, "/") + query;
138 }).join("!"));
139}
140
141function interpolateName(loaderContext, filename) {
142 let basename = "file";
143
144 if (loaderContext.resourcePath) {
145 const parsed = _path.default.parse(loaderContext.resourcePath);
146
147 if (parsed.dir) {
148 basename = parsed.name;
149 }
150 }
151
152 return filename.replace(/\[name\]/gi, () => basename);
153}
\No newline at end of file