Skip to content

Commit 1481998

Browse files
author
Ives van Hoorne
committed
Working version
1 parent 3637bc9 commit 1481998

File tree

4 files changed

+127
-37
lines changed

4 files changed

+127
-37
lines changed

src/index.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,61 @@ import * as path from "path";
44
import installDependencies from "./dependencies/install-dependencies";
55
import parseDependency from "./dependencies/parse-dependency";
66

7+
import findPackageInfos from "./packages/find-package-infos";
78
import findRequires from "./packages/find-requires";
89

910
import getHash from "./utils/get-hash";
1011

1112
export async function http(req: Request, res: Response) {
12-
const dependency = await parseDependency(req.url);
13-
const hash = getHash(dependency);
13+
try {
14+
const dependency = await parseDependency(req.url);
15+
const hash = getHash(dependency);
1416

15-
const a = Date.now();
17+
const a = Date.now();
1618

17-
if (!hash) {
18-
return;
19-
}
19+
if (!hash) {
20+
return;
21+
}
22+
23+
const packagePath = path.join("/tmp", hash);
24+
25+
await installDependencies(dependency, packagePath);
2026

21-
const packagePath = path.join("/tmp", hash);
27+
const packageInfos = await findPackageInfos(dependency.name, packagePath);
2228

23-
await installDependencies(dependency, packagePath);
29+
const aliases = Object.keys(packageInfos).reduce(
30+
(total, name) => ({
31+
...total,
32+
[name]: packageInfos[name].main,
33+
...packageInfos[name].aliases,
34+
}),
35+
{},
36+
);
2437

25-
const requires = await findRequires(dependency.name, packagePath);
38+
const { contents, aliases: newAliases } = await findRequires(
39+
dependency.name,
40+
packagePath,
41+
packageInfos,
42+
aliases,
43+
);
2644

27-
console.log("Done - " + (Date.now() - a) + " - " + packagePath);
28-
res.json(requires);
45+
const packages = Object.keys(packageInfos).reduce(
46+
(total, packageName) => ({
47+
...total,
48+
[packageName]: packageInfos[packageName].package,
49+
}),
50+
{},
51+
);
52+
53+
console.log("Done - " + (Date.now() - a) + " - " + packagePath);
54+
res.json({
55+
aliases: newAliases,
56+
contents,
57+
dependency,
58+
});
59+
} catch (e) {
60+
res.status(500).json({ error: e.message });
61+
}
2962
}
3063

3164
if (process.env.LOCAL) {
@@ -36,6 +69,6 @@ if (process.env.LOCAL) {
3669
app.get("/*", http);
3770

3871
app.listen(8080, () => {
39-
console.log("listening");
72+
console.log("Listening on port 8080");
4073
});
4174
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface IPackage {
1313
export interface IPackageInfo {
1414
package: IPackage;
1515
main: string | undefined;
16-
name: string;
16+
aliases: { [key: string]: string };
1717
}
1818

1919
function getDirectories(path: string) {

src/packages/find-requires.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { fs } from "mz";
22
import { dirname, join } from "path";
33

4-
import findAliases from "./find-aliases";
4+
import { IPackageInfo } from "./find-package-infos";
55
import resolveRequiredFiles from "./resolve-required-files";
66
import extractRequires from "./utils/extract-requires";
77
import getAliasedPath from "./utils/get-aliased-path";
88
import nodeResolvePath from "./utils/node-resolve-path";
99

1010
interface IAliases {
11-
[alias: string]: string;
11+
[alias: string]: string | false | null;
1212
}
1313

1414
function rewritePath(
@@ -27,7 +27,16 @@ function rewritePath(
2727
return null;
2828
}
2929

30-
return aliases[newPath] || newPath;
30+
if (aliases[path]) {
31+
return aliases[path];
32+
}
33+
34+
const nodeResolvedNewPath = nodeResolvePath(newPath);
35+
if (nodeResolvedNewPath && aliases[nodeResolvedNewPath]) {
36+
return aliases[nodeResolvedNewPath];
37+
}
38+
39+
return newPath;
3140
}
3241

3342
function buildRequireObject(
@@ -65,6 +74,7 @@ function buildRequireObject(
6574
aliases,
6675
);
6776

77+
// If something was added to the total
6878
if (requiredContents !== existingContents) {
6979
return { ...total, ...requiredContents };
7080
}
@@ -80,7 +90,7 @@ function getRequiresFromFile(
8090
) {
8191
const resolvedPath = nodeResolvePath(filePath);
8292
if (!resolvedPath) {
83-
console.log('Warning: could not find "' + filePath + '"');
93+
// console.log('Warning: could not find "' + filePath + '"');
8494
return null;
8595
}
8696

@@ -97,11 +107,11 @@ function getRequiresFromFile(
97107
export default async function findRequires(
98108
packageName: string,
99109
rootPath: string,
110+
packageInfos: { [dep: string]: IPackageInfo },
111+
aliases: IAliases,
100112
) {
101-
const packageInfos = await findAliases(packageName, rootPath);
102-
103113
if (!packageInfos[packageName]) {
104-
return;
114+
return { contents: {}, aliases: {} };
105115
}
106116

107117
const packagePath = join(rootPath, "node_modules", packageName);
@@ -111,17 +121,7 @@ export default async function findRequires(
111121
packageInfos[packageName],
112122
);
113123

114-
const aliases = Object.keys(packageInfos).reduce((total, name) => {
115-
const browserField = packageInfos[name].package.browser;
116-
const browserAliases = typeof browserField === "object" ? browserField : {};
117-
return {
118-
...total,
119-
[name]: packageInfos[name].main,
120-
...browserAliases,
121-
};
122-
}, {});
123-
124-
let files = {};
124+
let files: { [path: string]: string } = {};
125125

126126
for (const file of requiredFiles) {
127127
if (file) {
@@ -135,5 +135,23 @@ export default async function findRequires(
135135
}
136136
}
137137

138-
return files;
138+
const nodeModulesPath = join(rootPath, "node_modules") + "/";
139+
const relativeFiles = Object.keys(files).reduce(
140+
(total, next) => ({
141+
...total,
142+
[next.replace(nodeModulesPath, "")]: files[next],
143+
}),
144+
{},
145+
);
146+
147+
const relativeAliases = Object.keys(aliases).reduce((total, next) => {
148+
const aliasPath = aliases[next];
149+
return {
150+
...total,
151+
[next.replace(nodeModulesPath, "")]:
152+
aliasPath && aliasPath.replace(nodeModulesPath, ""),
153+
};
154+
}, {});
155+
156+
return { contents: relativeFiles, aliases: relativeAliases };
139157
}

src/packages/resolve-required-files.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import { flatten } from "lodash";
22
import { fs } from "mz";
33
import { basename, dirname, join } from "path";
4-
import { IPackageInfo } from "./find-aliases";
4+
import { IPackageInfo } from "./find-package-infos";
55

6-
const BLACKLISTED_DIRS = ["umd", "tests"];
6+
const BLACKLISTED_DIRS = [
7+
"demo",
8+
"docs",
9+
"benchmark",
10+
"es6",
11+
"es",
12+
"flow-typed",
13+
"src",
14+
"bundles",
15+
"examples",
16+
"scripts",
17+
"tests",
18+
"test",
19+
"testing",
20+
"umd",
21+
"min",
22+
"node_modules",
23+
];
724

825
async function getFilePathsInDirectory(path: string): Promise<string[]> {
926
const entries = await fs.readdir(path);
@@ -32,6 +49,30 @@ async function getFilePathsInDirectory(path: string): Promise<string[]> {
3249
return files;
3350
}
3451

52+
const DISALLOWED_EXTENSIONS = ["min.js", "umd.js", "node.js", "test.js"];
53+
const ALLOWED_EXTENSIONS = [
54+
"json",
55+
"js",
56+
"css",
57+
"scss",
58+
"styl",
59+
"less",
60+
"vue",
61+
"html",
62+
];
63+
64+
function isValidFile(filePath: string) {
65+
if (DISALLOWED_EXTENSIONS.some(ex => filePath.endsWith(ex))) {
66+
return false;
67+
}
68+
69+
if (ALLOWED_EXTENSIONS.some(ex => filePath.endsWith(ex))) {
70+
return true;
71+
}
72+
73+
return false;
74+
}
75+
3576
const FALLBACK_DIRS = ["dist", "lib", "build"];
3677

3778
export default async function resolveRequiredFiles(
@@ -56,7 +97,5 @@ export default async function resolveRequiredFiles(
5697

5798
const files = await getFilePathsInDirectory(entryDir);
5899

59-
return files
60-
.filter(p => p.endsWith(".js"))
61-
.filter(p => !p.endsWith(".min.js"));
100+
return files.filter(isValidFile);
62101
}

0 commit comments

Comments
 (0)