-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
223 lines (200 loc) · 6.13 KB
/
utils.mjs
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
216
217
218
219
220
221
222
223
import config from 'config';
import fs from 'fs-extra';
import http from 'http';
import path from 'path';
import resolvePackage from 'resolve';
import url from 'url';
export const readConf = (prop, def) => config.has(prop) ? config.get(prop) : def;
export const packageNameRegex = '@[\\w-.]+/[\\w-.]+|[\\w-.]+';
const WORKER_REXP = /(worker|sw)\d*\b/i;
export const HTTP_CODES = {
INTERNAL_SERVER_ERROR: 500,
NOT_ACCEPTABLE: 406,
NOT_FOUND: 404,
NOT_MODIFIED: 304,
OK: 200,
};
export const pathToURL = filePath => url.pathToFileURL(filePath).href.slice('file://'.length).replace(/^[a-zA-Z]:/, '');
export const urlToPath = urlPath => path.sep === '\\' ? urlPath.replace(/\//g, '\\') : urlPath;
export const getVersion = (dependencies, name) => {
if (!dependencies) return {};
// TODO: resolve with semver
const version = dependencies[name];
if (!version) return {};
const [ major = 0, minor = 0, patch = 0 ] = version.match(/\d+/g);
return {
major: Number(major),
minor: Number(minor),
patch: Number(patch),
};
};
const matchesModule = (filePath, module) =>
filePath.startsWith(`/node_modules/${module}@`) ||
filePath.startsWith(`/node_modules/${module}/`) ||
filePath.startsWith(`/${module}@`) ||
filePath.startsWith(`/${module}/`);
export const isMap = filePath => path.extname(filePath).toLowerCase() === '.map';
// export const isTest = filePath => filePath.startsWith('/test/');
// export const isVendor = filePath => filePath.startsWith('/node_modules/');
export const isPolyfill = filePath => matchesModule(filePath, 'core-js') ||
matchesModule(filePath, 'buffer') ||
matchesModule(filePath, 'base64-js') ||
matchesModule(filePath, 'ieee754') ||
matchesModule(filePath, 'process') ||
matchesModule(filePath, 'regenerator-runtime');
export const isInternal = filePath => filePath.includes('/hq-empty-module.js');
export const isWorker = filePath => WORKER_REXP.test(filePath);
export const isDefaultFavicon = filePath => filePath.endsWith('favicon.ico');
export const isAngularCompiler = filePath => filePath.endsWith('compiler/fesm5/compiler.js');
export const isSource = ext => [
'.pug',
'.html',
'.css',
'.scss',
'.sass',
'.less',
'.js',
'.jsx',
'.mjs',
'.es6',
'.vue',
'.svelte',
'.ts',
'.tsx',
'.coffee',
'.map',
].includes(ext);
export const getResType = ext => {
switch (ext) {
case '.jsx':
case '.ts':
case '.tsx':
case '.es6':
case '.vue':
case '.svelte':
case '.coffee': return '.js';
case '.scss':
case '.sass':
case '.less': return '.css';
case '.pug': return '.html';
default: return ext;
}
};
export const getModulePath = filepath => `/node_modules/${filepath.split('/node_modules/')[1]}`;
export const readJSONSync = (filePath, def = {}) => {
try {
return JSON.parse(fs.readFileSync(filePath), { encoding: 'utf8' });
} catch {
return def;
}
};
const resolveOrModify = (pkgPath, pkg, {
emptyPath,
resolve,
result,
}) => {
const pkgBasename = pkgPath.slice(0, -path.extname(pkgPath).length);
if (typeof pkg.browser[pkgPath] === 'string') {
result.modified = true;
pkg.main = pkg.browser[pkgPath];
} else if (typeof pkg.browser[pkgPath] === 'boolean') {
result.resolved = true;
resolve(emptyPath);
} else if (typeof pkg.browser[`./${pkgPath}`] === 'string') {
result.modified = true;
pkg.main = pkg.browser[`./${pkgPath}`];
} else if (typeof pkg.browser[`./${pkgPath}`] === 'boolean') {
result.resolved = true;
resolve(emptyPath);
} else if (typeof pkg.browser[`./${pkgPath}.js`] === 'string') {
result.modified = true;
pkg.main = pkg.browser[`./${pkgPath}.js`];
} else if (typeof pkg.browser[`./${pkgPath}.js`] === 'boolean') {
result.resolved = true;
resolve(emptyPath);
} else if (typeof pkg.browser[`./${pkgBasename}.js`] === 'string') {
result.modified = true;
pkg.main = pkg.browser[`./${pkgBasename}.js`];
} else if (typeof pkg.browser[`./${pkgBasename}.js`] === 'boolean') {
result.resolved = true;
resolve(emptyPath);
}
};
export const resolvePackageFrom = (basedir, dpath, hqroot) => new Promise((resolve, reject) => {
const emptyPath = path.resolve(hqroot, 'hq-empty-module.js');
const parts = dpath.split('/node_modules/');
const modName = parts[parts.length - 1];
const modPath = modName
.split('/')
.slice(1)
.join('/');
const modResolve = resolvePackage.isCore(modName) ? `${modName}/` : modName;
const result = {
modified: false,
resolved: false,
};
return resolvePackage(
modResolve, {
basedir,
extensions: [
'.js',
'.jsx',
'.mjs',
'.es6',
'.vue',
'.svelte',
'.ts',
'.tsx',
'.coffee',
'.css',
'.scss',
'.sass',
'.less',
'.pug',
'.html',
],
packageFilter(pkg) {
const { main: pkgMain } = pkg;
if (pkg.module) pkg.main = pkg.module;
else if (typeof pkg.exports === 'string') pkg.main = pkg.exports;
if (typeof pkg.browser === 'string') pkg.main = pkg.browser;
else if (typeof pkg.browser === 'object' && pkg.browser) {
if (modPath) {
resolveOrModify(modPath, pkg, {
emptyPath,
resolve,
result,
});
} else if (pkgMain) {
resolveOrModify(pkgMain, pkg, {
emptyPath,
resolve,
result,
});
} else if (pkg.module) {
resolveOrModify(pkg.module, pkg, {
emptyPath,
resolve,
result,
});
}
}
return pkg;
},
pathFilter(pkg, fullPath, relativePath) {
return result.modified ? pkg.main : relativePath;
},
},
(err, p) => {
if (result.resolved) return;
if (err) reject(err);
resolve(p);
},
);
});
export const getPort = port => new Promise((resolve, reject) => {
const server = http.createServer();
server.unref();
server.on('error', reject);
server.listen(port, 'localhost', () => server.close(() => resolve(port)));
}).catch(() => getPort(port + 1));