olum-compiler 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/copySrc.js +8 -2
- package/lib/alias.js +109 -0
- package/lib/changes.txt +1 -0
- package/lib/helpers.js +19 -6
- package/lib/importMap.js +5 -2
- package/lib/parser.js +6 -2
- package/lib/toJs.js +11 -1
- package/package.json +1 -1
- package/tests/compiler.test.js +214 -4
package/bin/copySrc.js
CHANGED
|
@@ -4,6 +4,7 @@ const { copy, compileRoutes, ls, isFile } = require("../lib/helpers");
|
|
|
4
4
|
const importMap = require("../lib/importMap");
|
|
5
5
|
const usage = require("../lib/usage");
|
|
6
6
|
const splitJsBarrels = require("../lib/jsBarrels");
|
|
7
|
+
const resolveAliases = require("../lib/alias");
|
|
7
8
|
|
|
8
9
|
const cwd = process.cwd();
|
|
9
10
|
const projectRoot = cwd.replace(/\/module$/, "");
|
|
@@ -45,8 +46,10 @@ function injectIndexHtml(indexHtmlPath) {
|
|
|
45
46
|
|
|
46
47
|
function splitCopiedJs() {
|
|
47
48
|
ls(srcDest, { absolute: true }).forEach((file) => {
|
|
48
|
-
if (isFile(file) && file.endsWith(".js"))
|
|
49
|
+
if (isFile(file) && file.endsWith(".js")) {
|
|
50
|
+
resolveAliases.file(file, projectRoot);
|
|
49
51
|
splitJsBarrels.file(file, projectRoot);
|
|
52
|
+
}
|
|
50
53
|
});
|
|
51
54
|
}
|
|
52
55
|
|
|
@@ -75,7 +78,10 @@ function syncFile(event, file) {
|
|
|
75
78
|
} else {
|
|
76
79
|
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
77
80
|
fs.copyFileSync(file, dest);
|
|
78
|
-
if (inSrc && dest.endsWith(".js"))
|
|
81
|
+
if (inSrc && dest.endsWith(".js")) {
|
|
82
|
+
resolveAliases.file(dest, projectRoot);
|
|
83
|
+
splitJsBarrels.file(dest, projectRoot);
|
|
84
|
+
}
|
|
79
85
|
if (dest === indexHtmlDest) injectIndexHtml(dest);
|
|
80
86
|
}
|
|
81
87
|
|
package/lib/alias.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
const toRoot = (dir) => (dir || process.cwd()).replace(/\/module$/, "");
|
|
5
|
+
|
|
6
|
+
const moduleSrc = path.resolve(__dirname, "../src");
|
|
7
|
+
|
|
8
|
+
const cache = new Map();
|
|
9
|
+
|
|
10
|
+
function aliases(root) {
|
|
11
|
+
const pkgPath = path.join(root, "package.json");
|
|
12
|
+
let mtime = 0;
|
|
13
|
+
try {
|
|
14
|
+
mtime = fs.statSync(pkgPath).mtimeMs;
|
|
15
|
+
} catch (e) {}
|
|
16
|
+
const hit = cache.get(root);
|
|
17
|
+
if (hit && hit.mtime === mtime) return hit.list;
|
|
18
|
+
|
|
19
|
+
let list = [];
|
|
20
|
+
try {
|
|
21
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
22
|
+
const conf = pkg.olum && pkg.olum.alias;
|
|
23
|
+
if (conf && typeof conf === "object" && !Array.isArray(conf)) {
|
|
24
|
+
list = Object.keys(conf)
|
|
25
|
+
.filter((name) => name && typeof conf[name] === "string")
|
|
26
|
+
|
|
27
|
+
.sort((a, b) => b.length - a.length)
|
|
28
|
+
.map((name) => [name, conf[name]]);
|
|
29
|
+
}
|
|
30
|
+
} catch (e) {}
|
|
31
|
+
cache.set(root, { mtime, list });
|
|
32
|
+
return list;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const matches = (spec, name) => spec === name || spec.startsWith(name + "/");
|
|
36
|
+
|
|
37
|
+
const isPlainPath = (spec) => /^\.{0,2}\//.test(spec) || /^[a-z]+:/i.test(spec);
|
|
38
|
+
|
|
39
|
+
function projectSpace(file, root) {
|
|
40
|
+
const abs = path.resolve(file);
|
|
41
|
+
return abs === moduleSrc || abs.startsWith(moduleSrc + path.sep)
|
|
42
|
+
? path.join(root, "src", path.relative(moduleSrc, abs))
|
|
43
|
+
: abs;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isAlias(spec, rootDir) {
|
|
47
|
+
if (typeof spec !== "string" || !spec || isPlainPath(spec)) return false;
|
|
48
|
+
return aliases(toRoot(rootDir)).some(([name]) => matches(spec, name));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function resolveAlias(spec, importer, rootDir) {
|
|
52
|
+
if (typeof spec !== "string" || !spec || isPlainPath(spec)) return spec;
|
|
53
|
+
const root = toRoot(rootDir);
|
|
54
|
+
const hit = aliases(root).find(([name]) => matches(spec, name));
|
|
55
|
+
if (!hit || !importer) return spec;
|
|
56
|
+
|
|
57
|
+
const [name, target] = hit;
|
|
58
|
+
const rest = spec.slice(name.length).replace(/^\//, "");
|
|
59
|
+
const abs = path.resolve(path.resolve(root, target), rest);
|
|
60
|
+
const from = path.dirname(projectSpace(importer, root));
|
|
61
|
+
const rel = path.relative(from, abs).split(path.sep).join("/");
|
|
62
|
+
return rel.startsWith(".") ? rel : "./" + rel;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function rewriteImports(code, file, rootDir) {
|
|
66
|
+
const root = toRoot(rootDir);
|
|
67
|
+
const list = aliases(root);
|
|
68
|
+
if (!list.length) return code;
|
|
69
|
+
if (!list.some(([name]) => code.includes(name))) return code;
|
|
70
|
+
|
|
71
|
+
let body;
|
|
72
|
+
try {
|
|
73
|
+
body = require("acorn").parse(code, {
|
|
74
|
+
ecmaVersion: "latest",
|
|
75
|
+
sourceType: "module",
|
|
76
|
+
}).body;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return code;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const nodes = body.filter(
|
|
82
|
+
(node) =>
|
|
83
|
+
node.source &&
|
|
84
|
+
/^(Import|ExportNamed|ExportAll)Declaration$/.test(node.type),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
88
|
+
const node = nodes[i];
|
|
89
|
+
const spec = node.source.value;
|
|
90
|
+
const resolved = resolveAlias(spec, file, root);
|
|
91
|
+
if (resolved === spec) continue;
|
|
92
|
+
code =
|
|
93
|
+
code.slice(0, node.source.start) +
|
|
94
|
+
JSON.stringify(resolved) +
|
|
95
|
+
code.slice(node.source.end);
|
|
96
|
+
}
|
|
97
|
+
return code;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function rewriteFile(file, rootDir) {
|
|
101
|
+
const code = fs.readFileSync(file, "utf8");
|
|
102
|
+
const out = rewriteImports(code, file, rootDir);
|
|
103
|
+
if (out !== code) fs.writeFileSync(file, out);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = resolveAlias;
|
|
107
|
+
module.exports.isAlias = isAlias;
|
|
108
|
+
module.exports.rewriteImports = rewriteImports;
|
|
109
|
+
module.exports.file = rewriteFile;
|
package/lib/changes.txt
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
change /home/eissapk/olum/template-compiler/src/match/[slug]/page.html
|
package/lib/helpers.js
CHANGED
|
@@ -33,9 +33,20 @@ const cleanParentheses = (filePath, type = "name") => {
|
|
|
33
33
|
|
|
34
34
|
const cleanNumbersPrefix = (name, type = "name") => {
|
|
35
35
|
if (type === "url") {
|
|
36
|
-
|
|
36
|
+
const trimmedDigit =
|
|
37
|
+
name.match(/(?:^|\/)(\d+)-/)?.[1] ?? name.match(/-(\d+)$/)?.[1] ?? "";
|
|
38
|
+
return {
|
|
39
|
+
name: name.replace(/(^|\/)\d+-/g, "$1"),
|
|
40
|
+
trimmedDigit,
|
|
41
|
+
};
|
|
37
42
|
}
|
|
38
|
-
|
|
43
|
+
|
|
44
|
+
const trimmedDigit =
|
|
45
|
+
name.match(/^(\d+)/)?.[1] ?? name.match(/(\d+)$/)?.[1] ?? "";
|
|
46
|
+
return {
|
|
47
|
+
name: name.replace(/\d+-?/g, ""),
|
|
48
|
+
trimmedDigit,
|
|
49
|
+
};
|
|
39
50
|
};
|
|
40
51
|
|
|
41
52
|
function isUpper(char) {
|
|
@@ -717,8 +728,8 @@ function toComponentName(file) {
|
|
|
717
728
|
function generateImports(manifest) {
|
|
718
729
|
return Object.values(manifest)
|
|
719
730
|
.map(({ page }) => {
|
|
720
|
-
const
|
|
721
|
-
return `import ${
|
|
731
|
+
const { name, trimmedDigit } = cleanNumbersPrefix(toComponentName(page));
|
|
732
|
+
return `import ${name}${trimmedDigit ? "_" + trimmedDigit : ""} from "./${page.replace(/.html$/, ".js")}";`;
|
|
722
733
|
})
|
|
723
734
|
.join("\n");
|
|
724
735
|
}
|
|
@@ -753,8 +764,10 @@ function generateRoutes(manifest) {
|
|
|
753
764
|
const result = `export const routes = [
|
|
754
765
|
${routes
|
|
755
766
|
.map(({ path, comp }) => {
|
|
756
|
-
|
|
757
|
-
|
|
767
|
+
const { name } = cleanNumbersPrefix(path, "url");
|
|
768
|
+
const _comp = cleanNumbersPrefix(comp);
|
|
769
|
+
paths.push(name);
|
|
770
|
+
return ` { path: "${name}", comp: ${_comp.name}${_comp.trimmedDigit ? "_" + _comp.trimmedDigit : ""} }`;
|
|
758
771
|
})
|
|
759
772
|
.join(",\n")}
|
|
760
773
|
];`;
|
package/lib/importMap.js
CHANGED
|
@@ -4,6 +4,7 @@ const colors = require("./colors");
|
|
|
4
4
|
const { ls, isFile } = require("./helpers");
|
|
5
5
|
|
|
6
6
|
const { useLocalCore, localLibs, optionalLibs } = require("./coreLibs");
|
|
7
|
+
const { isAlias } = require("./alias");
|
|
7
8
|
|
|
8
9
|
function pickEntry(exp) {
|
|
9
10
|
if (typeof exp === "string") return exp;
|
|
@@ -74,7 +75,7 @@ function resolvePackage(rootDir, pkgName) {
|
|
|
74
75
|
return "/node_modules/" + pkgName + "/" + entry;
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
function getBareSpecifiers(srcDir) {
|
|
78
|
+
function getBareSpecifiers(srcDir, rootDir) {
|
|
78
79
|
const specifiers = new Set();
|
|
79
80
|
const files = ls(srcDir, { absolute: true }).filter(
|
|
80
81
|
(p) => isFile(p) && /\.(html|js)$/i.test(p),
|
|
@@ -87,6 +88,8 @@ function getBareSpecifiers(srcDir) {
|
|
|
87
88
|
const spec = match[1];
|
|
88
89
|
if (spec.startsWith(".") || spec.startsWith("/") || /^https?:/.test(spec))
|
|
89
90
|
continue;
|
|
91
|
+
|
|
92
|
+
if (isAlias(spec, rootDir)) continue;
|
|
90
93
|
specifiers.add(spec);
|
|
91
94
|
}
|
|
92
95
|
});
|
|
@@ -167,7 +170,7 @@ function buildImports(srcDir) {
|
|
|
167
170
|
});
|
|
168
171
|
}
|
|
169
172
|
|
|
170
|
-
getBareSpecifiers(srcDir).forEach((spec) => {
|
|
173
|
+
getBareSpecifiers(srcDir, rootDir).forEach((spec) => {
|
|
171
174
|
const pkgName = toPkgName(spec);
|
|
172
175
|
if (pkgName in localLibs) return;
|
|
173
176
|
addPackage(rootDir, pkgName, imports);
|
package/lib/parser.js
CHANGED
|
@@ -5,6 +5,7 @@ const headParser = require("./headParser");
|
|
|
5
5
|
const jsParser = require("./jsParser");
|
|
6
6
|
const getImports = require("./getImports");
|
|
7
7
|
const toJs = require("./toJs");
|
|
8
|
+
const resolveAlias = require("./alias");
|
|
8
9
|
const splitBarrels = require("./barrelSplit");
|
|
9
10
|
const path = require("path");
|
|
10
11
|
const {
|
|
@@ -121,8 +122,9 @@ const parser = (str, counter, filePath) => {
|
|
|
121
122
|
const hit = (imports || []).find((item) =>
|
|
122
123
|
new RegExp("\\b" + name + "\\b").test(item.statement),
|
|
123
124
|
);
|
|
124
|
-
|
|
125
|
+
let spec =
|
|
125
126
|
hit && (hit.statement.match(/from\s+["'](.+?)["']/) || [])[1];
|
|
127
|
+
spec = resolveAlias(spec, filePath);
|
|
126
128
|
if (!spec || !spec.startsWith(".")) return;
|
|
127
129
|
const child = usage.relative(
|
|
128
130
|
path
|
|
@@ -136,7 +138,9 @@ const parser = (str, counter, filePath) => {
|
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
if (imports)
|
|
139
|
-
imports = imports.map((item) =>
|
|
141
|
+
imports = imports.map((item) =>
|
|
142
|
+
splitBarrels(toJs(item.statement, filePath)),
|
|
143
|
+
);
|
|
140
144
|
|
|
141
145
|
function mkComponentsList(comps) {
|
|
142
146
|
if (!imports || !isFullArr(imports)) return "";
|
package/lib/toJs.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
|
+
const resolveAlias = require("./alias");
|
|
3
|
+
|
|
4
|
+
function toJs(statement, filePath, rootDir) {
|
|
5
|
+
if (!/(^|\s)from\s/.test(statement))
|
|
6
|
+
return statement.replace(
|
|
7
|
+
/(["'])([^"']+)\1/,
|
|
8
|
+
(str, quote, _path) =>
|
|
9
|
+
quote + resolveAlias(_path, filePath, rootDir) + quote,
|
|
10
|
+
);
|
|
2
11
|
|
|
3
|
-
function toJs(statement) {
|
|
4
12
|
return statement.replace(/(from)(\s+)?.*/g, (str) => {
|
|
5
13
|
let _path = str.replace(/from|'|"/g, "").trim();
|
|
6
14
|
|
|
15
|
+
_path = resolveAlias(_path, filePath, rootDir);
|
|
16
|
+
|
|
7
17
|
if (!/^\.{0,2}\//.test(_path)) return 'from "' + _path + '";';
|
|
8
18
|
const extname = path.extname(_path);
|
|
9
19
|
const newPath = extname ? _path.replace(extname, "") : _path;
|
package/package.json
CHANGED
package/tests/compiler.test.js
CHANGED
|
@@ -861,7 +861,7 @@ function withRouteTree(files, fn) {
|
|
|
861
861
|
}
|
|
862
862
|
|
|
863
863
|
checkFn(
|
|
864
|
-
"route tree compiles: / first, /404 last, [param] -> :param, (group) and NN-
|
|
864
|
+
"route tree compiles: / first, /404 last, [param] -> :param, (group) stripped and NN- kept as an identifier suffix",
|
|
865
865
|
() =>
|
|
866
866
|
withRouteTree(
|
|
867
867
|
[
|
|
@@ -882,9 +882,9 @@ checkFn(
|
|
|
882
882
|
/import NotFound from "\.\/not-found\.js";/.test(out) &&
|
|
883
883
|
/import About from "\.\/about\/page\.js";/.test(out) &&
|
|
884
884
|
/import Club from "\.\/\(main\)\/club\/page\.js";/.test(out) &&
|
|
885
|
-
/import
|
|
885
|
+
/import BlogSlug_01 from "\.\/01-blog\/\[slug\]\/page\.js";/.test(out) &&
|
|
886
886
|
/\{ path: "\/club", comp: Club \}/.test(out) &&
|
|
887
|
-
/\{ path: "\/blog\/:slug", comp:
|
|
887
|
+
/\{ path: "\/blog\/:slug", comp: BlogSlug_01 \}/.test(out) &&
|
|
888
888
|
/\{ path: "\/about", comp: About \}/.test(out) &&
|
|
889
889
|
paths[0] === "/" &&
|
|
890
890
|
paths[paths.length - 1] === "/404" &&
|
|
@@ -896,6 +896,19 @@ checkFn(
|
|
|
896
896
|
},
|
|
897
897
|
);
|
|
898
898
|
|
|
899
|
+
checkFn(
|
|
900
|
+
"NN- siblings get distinct identifiers instead of colliding",
|
|
901
|
+
() =>
|
|
902
|
+
withRouteTree(["01-blog/page.html", "02-blog/page.html"], (root) =>
|
|
903
|
+
compileRoutes(root),
|
|
904
|
+
),
|
|
905
|
+
(out) =>
|
|
906
|
+
/import Blog_01 from "\.\/01-blog\/page\.js";/.test(out) &&
|
|
907
|
+
/import Blog_02 from "\.\/02-blog\/page\.js";/.test(out) &&
|
|
908
|
+
/comp: Blog_01 \}/.test(out) &&
|
|
909
|
+
/comp: Blog_02 \}/.test(out),
|
|
910
|
+
);
|
|
911
|
+
|
|
899
912
|
checkFn(
|
|
900
913
|
"no not-found.html -> no /404 route and no err in the router config",
|
|
901
914
|
() =>
|
|
@@ -1528,6 +1541,203 @@ checkHead(
|
|
|
1528
1541
|
|
|
1529
1542
|
console.warn = realWarn;
|
|
1530
1543
|
|
|
1544
|
+
section("§28 Path aliases");
|
|
1545
|
+
|
|
1546
|
+
const resolveAlias = require("../lib/alias");
|
|
1547
|
+
const toJs = require("../lib/toJs");
|
|
1548
|
+
|
|
1549
|
+
const aliasRoot = fs.mkdtempSync(nodePath.join(os.tmpdir(), "olum-alias-"));
|
|
1550
|
+
const plainRoot = fs.mkdtempSync(nodePath.join(os.tmpdir(), "olum-noalias-"));
|
|
1551
|
+
|
|
1552
|
+
fs.writeFileSync(
|
|
1553
|
+
nodePath.join(aliasRoot, "package.json"),
|
|
1554
|
+
JSON.stringify({
|
|
1555
|
+
olum: {
|
|
1556
|
+
SITE_URL: "https://example.com",
|
|
1557
|
+
alias: {
|
|
1558
|
+
"@": "./src",
|
|
1559
|
+
"#": "./src/utils",
|
|
1560
|
+
"@components": "./src/components",
|
|
1561
|
+
"@app": "./src",
|
|
1562
|
+
"@app/ui": "./src/design/ui",
|
|
1563
|
+
},
|
|
1564
|
+
},
|
|
1565
|
+
}),
|
|
1566
|
+
);
|
|
1567
|
+
fs.writeFileSync(
|
|
1568
|
+
nodePath.join(plainRoot, "package.json"),
|
|
1569
|
+
JSON.stringify({ olum: { SITE_URL: "https://example.com" } }),
|
|
1570
|
+
);
|
|
1571
|
+
|
|
1572
|
+
const inSrc = (rel) => nodePath.join(aliasRoot, "src", rel);
|
|
1573
|
+
|
|
1574
|
+
const alias = (name, spec, importer, assertion) =>
|
|
1575
|
+
checkFn(name, () => resolveAlias(spec, importer, aliasRoot), assertion);
|
|
1576
|
+
|
|
1577
|
+
const same = (spec) => (out) => out === spec;
|
|
1578
|
+
|
|
1579
|
+
alias(
|
|
1580
|
+
"a single alias resolves against the project root",
|
|
1581
|
+
"@/utils/foo.js",
|
|
1582
|
+
inSrc("page.js"),
|
|
1583
|
+
(out) => out === "./utils/foo.js",
|
|
1584
|
+
);
|
|
1585
|
+
|
|
1586
|
+
alias(
|
|
1587
|
+
"a second alias points at its own directory",
|
|
1588
|
+
"#/foo.js",
|
|
1589
|
+
inSrc("page.js"),
|
|
1590
|
+
(out) => out === "./utils/foo.js",
|
|
1591
|
+
);
|
|
1592
|
+
|
|
1593
|
+
alias(
|
|
1594
|
+
"a nested importer walks back up to the target",
|
|
1595
|
+
"@/utils/foo.js",
|
|
1596
|
+
inSrc("pages/blog/post.html"),
|
|
1597
|
+
(out) => out === "../../utils/foo.js",
|
|
1598
|
+
);
|
|
1599
|
+
|
|
1600
|
+
alias(
|
|
1601
|
+
"a nested target keeps its whole subpath",
|
|
1602
|
+
"@/a/b/c/foo.js",
|
|
1603
|
+
inSrc("page.js"),
|
|
1604
|
+
(out) => out === "./a/b/c/foo.js",
|
|
1605
|
+
);
|
|
1606
|
+
|
|
1607
|
+
alias(
|
|
1608
|
+
"the bare alias itself resolves to its directory",
|
|
1609
|
+
"#",
|
|
1610
|
+
inSrc("page.js"),
|
|
1611
|
+
(out) => out === "./utils",
|
|
1612
|
+
);
|
|
1613
|
+
|
|
1614
|
+
alias(
|
|
1615
|
+
"a relative import is left to the existing resolver",
|
|
1616
|
+
"./foo.js",
|
|
1617
|
+
inSrc("page.js"),
|
|
1618
|
+
same("./foo.js"),
|
|
1619
|
+
);
|
|
1620
|
+
|
|
1621
|
+
alias(
|
|
1622
|
+
"a parent-relative import is left to the existing resolver",
|
|
1623
|
+
"../foo.js",
|
|
1624
|
+
inSrc("pages/post.js"),
|
|
1625
|
+
same("../foo.js"),
|
|
1626
|
+
);
|
|
1627
|
+
|
|
1628
|
+
alias(
|
|
1629
|
+
"a package import is left to the existing resolver",
|
|
1630
|
+
"lodash",
|
|
1631
|
+
inSrc("page.js"),
|
|
1632
|
+
same("lodash"),
|
|
1633
|
+
);
|
|
1634
|
+
|
|
1635
|
+
alias(
|
|
1636
|
+
'a scoped package is not an alias match for "@"',
|
|
1637
|
+
"@scope/pkg",
|
|
1638
|
+
inSrc("page.js"),
|
|
1639
|
+
same("@scope/pkg"),
|
|
1640
|
+
);
|
|
1641
|
+
|
|
1642
|
+
alias(
|
|
1643
|
+
"an alias only matches on an exact name or a following slash",
|
|
1644
|
+
"@foo",
|
|
1645
|
+
inSrc("page.js"),
|
|
1646
|
+
same("@foo"),
|
|
1647
|
+
);
|
|
1648
|
+
|
|
1649
|
+
alias(
|
|
1650
|
+
"overlapping aliases pick the longest match",
|
|
1651
|
+
"@app/ui/Button.js",
|
|
1652
|
+
inSrc("page.js"),
|
|
1653
|
+
(out) => out === "./design/ui/Button.js",
|
|
1654
|
+
);
|
|
1655
|
+
|
|
1656
|
+
alias(
|
|
1657
|
+
"@components wins over @ for @components/Button.js",
|
|
1658
|
+
"@components/Button.js",
|
|
1659
|
+
inSrc("page.js"),
|
|
1660
|
+
(out) => out === "./components/Button.js",
|
|
1661
|
+
);
|
|
1662
|
+
|
|
1663
|
+
checkFn(
|
|
1664
|
+
"no alias config leaves every specifier untouched",
|
|
1665
|
+
() =>
|
|
1666
|
+
resolveAlias(
|
|
1667
|
+
"@/utils/foo.js",
|
|
1668
|
+
nodePath.join(plainRoot, "src/page.js"),
|
|
1669
|
+
plainRoot,
|
|
1670
|
+
),
|
|
1671
|
+
same("@/utils/foo.js"),
|
|
1672
|
+
);
|
|
1673
|
+
|
|
1674
|
+
const jsOf = (name, statement, importer, assertion) =>
|
|
1675
|
+
checkFn(name, () => toJs(statement, importer, aliasRoot), assertion);
|
|
1676
|
+
|
|
1677
|
+
jsOf(
|
|
1678
|
+
"an aliased component import comes out as a relative .js path",
|
|
1679
|
+
`import Foo from "@/comps/Foo.html"`,
|
|
1680
|
+
inSrc("page.html"),
|
|
1681
|
+
(out) => out === `import Foo from "./comps/Foo.js";`,
|
|
1682
|
+
);
|
|
1683
|
+
|
|
1684
|
+
jsOf(
|
|
1685
|
+
"an aliased import with no extension still gets .js",
|
|
1686
|
+
`import foo from "#/foo"`,
|
|
1687
|
+
inSrc("page.html"),
|
|
1688
|
+
(out) => out === `import foo from "./utils/foo.js";`,
|
|
1689
|
+
);
|
|
1690
|
+
|
|
1691
|
+
jsOf(
|
|
1692
|
+
"a relative import keeps the behaviour it had",
|
|
1693
|
+
`import foo from "./a"`,
|
|
1694
|
+
inSrc("page.html"),
|
|
1695
|
+
(out) => out === `import foo from "./a.js";`,
|
|
1696
|
+
);
|
|
1697
|
+
|
|
1698
|
+
jsOf(
|
|
1699
|
+
"a package import keeps the behaviour it had",
|
|
1700
|
+
`import foo from "lodash"`,
|
|
1701
|
+
inSrc("page.html"),
|
|
1702
|
+
(out) => out === `import foo from "lodash";`,
|
|
1703
|
+
);
|
|
1704
|
+
|
|
1705
|
+
jsOf(
|
|
1706
|
+
"a side-effect import resolves its alias and keeps its extension",
|
|
1707
|
+
`import "@/styles/app.css"`,
|
|
1708
|
+
inSrc("page.html"),
|
|
1709
|
+
(out) => out === `import "./styles/app.css"`,
|
|
1710
|
+
);
|
|
1711
|
+
|
|
1712
|
+
checkFn(
|
|
1713
|
+
"a plain .js file has its aliased imports rewritten, strings left alone",
|
|
1714
|
+
() =>
|
|
1715
|
+
resolveAlias.rewriteImports(
|
|
1716
|
+
`import foo from "@/utils/foo.js";\nimport bar from "./bar.js";\nexport { baz } from "#/baz.js";\nconst snippet = \`import x from "@/x.js";\`;\n`,
|
|
1717
|
+
inSrc("api.js"),
|
|
1718
|
+
aliasRoot,
|
|
1719
|
+
),
|
|
1720
|
+
(out) =>
|
|
1721
|
+
/import foo from "\.\/utils\/foo\.js";/.test(out) &&
|
|
1722
|
+
/import bar from "\.\/bar\.js";/.test(out) &&
|
|
1723
|
+
/export \{ baz \} from "\.\/utils\/baz\.js";/.test(out) &&
|
|
1724
|
+
/const snippet = `import x from "@\/x\.js";`;/.test(out),
|
|
1725
|
+
);
|
|
1726
|
+
|
|
1727
|
+
checkFn(
|
|
1728
|
+
"the import map scan knows an aliased path is not a package",
|
|
1729
|
+
() => [
|
|
1730
|
+
resolveAlias.isAlias("@/utils/foo.js", aliasRoot),
|
|
1731
|
+
resolveAlias.isAlias("@scope/pkg", aliasRoot),
|
|
1732
|
+
resolveAlias.isAlias("lodash", aliasRoot),
|
|
1733
|
+
resolveAlias.isAlias("@/utils/foo.js", plainRoot),
|
|
1734
|
+
],
|
|
1735
|
+
(out) => String(out) === "true,false,false,false",
|
|
1736
|
+
);
|
|
1737
|
+
|
|
1738
|
+
fs.rmSync(aliasRoot, { recursive: true, force: true });
|
|
1739
|
+
fs.rmSync(plainRoot, { recursive: true, force: true });
|
|
1740
|
+
|
|
1531
1741
|
console.log("\n========================");
|
|
1532
1742
|
const summary = `${passed} passed, ${failed} failed`;
|
|
1533
1743
|
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
@@ -1545,7 +1755,7 @@ if (failed) {
|
|
|
1545
1755
|
console.log("");
|
|
1546
1756
|
}
|
|
1547
1757
|
|
|
1548
|
-
const EXPECTED_CHECKS =
|
|
1758
|
+
const EXPECTED_CHECKS = 152;
|
|
1549
1759
|
const total = passed + failed;
|
|
1550
1760
|
if (total !== EXPECTED_CHECKS) {
|
|
1551
1761
|
console.log(
|