olum-compiler 1.8.4 → 1.9.1
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/lib/barrelSplit.js +120 -17
- package/lib/importMap.js +51 -5
- package/package.json +1 -1
- package/tests/compiler.test.js +248 -1
package/lib/barrelSplit.js
CHANGED
|
@@ -24,10 +24,116 @@ function pickEntry(exp) {
|
|
|
24
24
|
return null;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
const isSubpathMap = (exp) =>
|
|
28
|
+
exp != null &&
|
|
29
|
+
typeof exp === "object" &&
|
|
30
|
+
!Array.isArray(exp) &&
|
|
31
|
+
Object.keys(exp).some((key) => key.startsWith("."));
|
|
32
|
+
|
|
33
|
+
const toPath = (dir, target) => path.resolve(dir, target.replace(/^\.?\//, ""));
|
|
34
|
+
|
|
35
|
+
const splitStar = (str) => {
|
|
36
|
+
const star = str.indexOf("*");
|
|
37
|
+
return star === -1 ? null : [str.slice(0, star), str.slice(star + 1)];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function resolveTarget(exp, subpath) {
|
|
41
|
+
if (!isSubpathMap(exp)) return null;
|
|
42
|
+
if (Object.hasOwn(exp, subpath)) return pickEntry(exp[subpath]);
|
|
43
|
+
|
|
44
|
+
let best = null;
|
|
45
|
+
for (const key of Object.keys(exp)) {
|
|
46
|
+
const parts = splitStar(key);
|
|
47
|
+
if (!parts) continue;
|
|
48
|
+
const [prefix, suffix] = parts;
|
|
49
|
+
if (subpath.length <= prefix.length + suffix.length) continue;
|
|
50
|
+
if (!subpath.startsWith(prefix)) continue;
|
|
51
|
+
if (suffix && !subpath.endsWith(suffix)) continue;
|
|
52
|
+
if (!best || prefix.length > best.prefix.length)
|
|
53
|
+
best = { prefix, suffix, value: exp[key] };
|
|
54
|
+
}
|
|
55
|
+
if (!best) return null;
|
|
56
|
+
|
|
57
|
+
const target = pickEntry(best.value);
|
|
58
|
+
if (typeof target !== "string" || !target.includes("*")) return target;
|
|
59
|
+
const captured = subpath.slice(
|
|
60
|
+
best.prefix.length,
|
|
61
|
+
best.suffix ? subpath.length - best.suffix.length : undefined,
|
|
62
|
+
);
|
|
63
|
+
return target.replace("*", captured);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function subpathOf(exp, rel, dir) {
|
|
67
|
+
if (rel.startsWith("../")) return null;
|
|
68
|
+
const file = "./" + rel;
|
|
69
|
+
if (exp == null) return file;
|
|
70
|
+
if (!isSubpathMap(exp)) return null;
|
|
71
|
+
|
|
72
|
+
const hits = (subpath) => {
|
|
73
|
+
const target = resolveTarget(exp, subpath);
|
|
74
|
+
return (
|
|
75
|
+
typeof target === "string" && toPath(dir, target) === toPath(dir, file)
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
for (const key of Object.keys(exp)) {
|
|
80
|
+
if (key === "." || key.includes("*")) continue;
|
|
81
|
+
if (hits(key)) return key;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let best = null;
|
|
85
|
+
for (const key of Object.keys(exp)) {
|
|
86
|
+
const keyParts = splitStar(key);
|
|
87
|
+
if (!keyParts) continue;
|
|
88
|
+
const target = pickEntry(exp[key]);
|
|
89
|
+
if (typeof target !== "string") continue;
|
|
90
|
+
const targetParts = splitStar(target);
|
|
91
|
+
if (!targetParts) continue;
|
|
92
|
+
const [keyPrefix, keySuffix] = keyParts;
|
|
93
|
+
const [targetPrefix, targetSuffix] = targetParts;
|
|
94
|
+
|
|
95
|
+
if (keySuffix !== targetSuffix) continue;
|
|
96
|
+
if (file.length <= targetPrefix.length + targetSuffix.length) continue;
|
|
97
|
+
if (!file.startsWith(targetPrefix) || !file.endsWith(targetSuffix))
|
|
98
|
+
continue;
|
|
99
|
+
const captured = file.slice(
|
|
100
|
+
targetPrefix.length,
|
|
101
|
+
targetSuffix ? file.length - targetSuffix.length : undefined,
|
|
102
|
+
);
|
|
103
|
+
const subpath = keyPrefix + captured + keySuffix;
|
|
104
|
+
if (!hits(subpath)) continue;
|
|
105
|
+
if (!best || targetPrefix.length > best.prefix.length)
|
|
106
|
+
best = { prefix: targetPrefix, subpath };
|
|
107
|
+
}
|
|
108
|
+
return best ? best.subpath : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function entryFor(dir, pkg, subpath) {
|
|
112
|
+
const exp = pkg.exports;
|
|
113
|
+
let entry;
|
|
114
|
+
if (isSubpathMap(exp)) {
|
|
115
|
+
entry = resolveTarget(exp, subpath);
|
|
116
|
+
if (typeof entry !== "string" && subpath === ".")
|
|
117
|
+
entry = pkg.module || pkg.main;
|
|
118
|
+
} else if (subpath === ".") {
|
|
119
|
+
entry = pickEntry(exp) || pkg.module || pkg.main || "index.js";
|
|
120
|
+
} else {
|
|
121
|
+
entry = subpath;
|
|
122
|
+
}
|
|
123
|
+
if (typeof entry !== "string") return null;
|
|
124
|
+
|
|
125
|
+
let entryPath = toPath(dir, entry);
|
|
126
|
+
if (!fs.existsSync(entryPath) && fs.existsSync(entryPath + ".js"))
|
|
127
|
+
entryPath += ".js";
|
|
128
|
+
if (fs.existsSync(entryPath) && fs.statSync(entryPath).isDirectory())
|
|
129
|
+
entryPath = path.join(entryPath, "index.js");
|
|
130
|
+
return fs.existsSync(entryPath) ? entryPath : null;
|
|
131
|
+
}
|
|
132
|
+
|
|
27
133
|
const nameOf = (node) => (node.type === "Identifier" ? node.name : node.value);
|
|
28
134
|
|
|
29
|
-
function barrelMap(rootDir, pkgName) {
|
|
30
|
-
const key = rootDir + "\0" + pkgName;
|
|
135
|
+
function barrelMap(rootDir, pkgName, subpath) {
|
|
136
|
+
const key = rootDir + "\0" + pkgName + "\0" + subpath;
|
|
31
137
|
if (cache.has(key)) return cache.get(key);
|
|
32
138
|
|
|
33
139
|
let map = null;
|
|
@@ -37,16 +143,7 @@ function barrelMap(rootDir, pkgName) {
|
|
|
37
143
|
fs.readFileSync(path.join(dir, "package.json"), "utf8"),
|
|
38
144
|
);
|
|
39
145
|
|
|
40
|
-
|
|
41
|
-
if (exp && typeof exp === "object" && !Array.isArray(exp)) {
|
|
42
|
-
const keys = Object.keys(exp);
|
|
43
|
-
if (keys.some((k) => k.startsWith("."))) exp = exp["."];
|
|
44
|
-
}
|
|
45
|
-
let entry = pickEntry(exp) || pkg.module || pkg.main || "index.js";
|
|
46
|
-
let entryPath = path.resolve(dir, entry.replace(/^\.?\//, ""));
|
|
47
|
-
if (fs.existsSync(entryPath) && fs.statSync(entryPath).isDirectory())
|
|
48
|
-
entryPath = path.join(entryPath, "index.js");
|
|
49
|
-
|
|
146
|
+
const entryPath = entryFor(dir, pkg, subpath);
|
|
50
147
|
const body = acorn.parse(fs.readFileSync(entryPath, "utf8"), {
|
|
51
148
|
ecmaVersion: "latest",
|
|
52
149
|
sourceType: "module",
|
|
@@ -62,8 +159,12 @@ function barrelMap(rootDir, pkgName) {
|
|
|
62
159
|
map = new Map();
|
|
63
160
|
body.forEach((node) => {
|
|
64
161
|
const target = path.resolve(path.dirname(entryPath), node.source.value);
|
|
65
|
-
|
|
66
|
-
|
|
162
|
+
if (!fs.existsSync(target)) return;
|
|
163
|
+
const rel = path.relative(dir, target).split(path.sep).join("/");
|
|
164
|
+
|
|
165
|
+
const member = subpathOf(pkg.exports, rel, dir);
|
|
166
|
+
if (!member) return;
|
|
167
|
+
const spec = pkgName + member.slice(1);
|
|
67
168
|
node.specifiers.forEach((item) =>
|
|
68
169
|
map.set(nameOf(item.exported), {
|
|
69
170
|
spec,
|
|
@@ -103,18 +204,20 @@ function splitBarrels(statement, rootDir) {
|
|
|
103
204
|
const pkgName = source.startsWith("@")
|
|
104
205
|
? segments.slice(0, 2).join("/")
|
|
105
206
|
: segments[0];
|
|
106
|
-
|
|
207
|
+
|
|
208
|
+
const rest = source.slice(pkgName.length);
|
|
209
|
+
const subpath = rest ? "." + rest : ".";
|
|
107
210
|
if (!node.specifiers.length) return statement;
|
|
108
211
|
if (!node.specifiers.every((item) => item.type === "ImportSpecifier"))
|
|
109
212
|
return statement;
|
|
110
213
|
|
|
111
|
-
const map = barrelMap(root, pkgName);
|
|
214
|
+
const map = barrelMap(root, pkgName, subpath);
|
|
112
215
|
if (!map) return statement;
|
|
113
216
|
|
|
114
217
|
const lines = [];
|
|
115
218
|
for (const item of node.specifiers) {
|
|
116
219
|
const hit = map.get(nameOf(item.imported));
|
|
117
|
-
if (!hit) return statement;
|
|
220
|
+
if (!hit || hit.spec === source) return statement;
|
|
118
221
|
const local = item.local.name;
|
|
119
222
|
lines.push(
|
|
120
223
|
hit.imported === "default"
|
package/lib/importMap.js
CHANGED
|
@@ -25,6 +25,35 @@ function pickEntry(exp) {
|
|
|
25
25
|
return null;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
function subpathImports(pkg, pkgName) {
|
|
29
|
+
const base = "/node_modules/" + pkgName + "/";
|
|
30
|
+
const exp = pkg.exports;
|
|
31
|
+
const isSubpathMap =
|
|
32
|
+
exp &&
|
|
33
|
+
typeof exp === "object" &&
|
|
34
|
+
!Array.isArray(exp) &&
|
|
35
|
+
Object.keys(exp).some((key) => key.startsWith("."));
|
|
36
|
+
if (!isSubpathMap) return { [pkgName + "/"]: base };
|
|
37
|
+
|
|
38
|
+
const imports = {};
|
|
39
|
+
Object.keys(exp).forEach((key) => {
|
|
40
|
+
if (key === "." || !key.startsWith(".")) return;
|
|
41
|
+
const target = pickEntry(exp[key]);
|
|
42
|
+
if (!target) return;
|
|
43
|
+
if (!key.includes("*")) {
|
|
44
|
+
imports[pkgName + key.slice(1)] = base + target.replace(/^\.?\//, "");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const [keyPrefix, keySuffix] = key.split("*");
|
|
49
|
+
const [targetPrefix, targetSuffix] = target.split("*");
|
|
50
|
+
if (keySuffix !== targetSuffix) return;
|
|
51
|
+
imports[pkgName + keyPrefix.slice(1)] =
|
|
52
|
+
base + targetPrefix.replace(/^\.?\//, "");
|
|
53
|
+
});
|
|
54
|
+
return imports;
|
|
55
|
+
}
|
|
56
|
+
|
|
28
57
|
function resolvePackage(rootDir, pkgName) {
|
|
29
58
|
const pkgJsonPath = path.join(
|
|
30
59
|
rootDir,
|
|
@@ -86,8 +115,6 @@ function addPackage(rootDir, pkgName, imports) {
|
|
|
86
115
|
);
|
|
87
116
|
return;
|
|
88
117
|
}
|
|
89
|
-
imports[pkgName] = entry;
|
|
90
|
-
imports[pkgName + "/"] = "/node_modules/" + pkgName + "/";
|
|
91
118
|
const pkgJsonPath = path.join(
|
|
92
119
|
rootDir,
|
|
93
120
|
"node_modules",
|
|
@@ -95,12 +122,14 @@ function addPackage(rootDir, pkgName, imports) {
|
|
|
95
122
|
"package.json",
|
|
96
123
|
);
|
|
97
124
|
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
125
|
+
imports[pkgName] = entry;
|
|
126
|
+
Object.assign(imports, subpathImports(pkg, pkgName));
|
|
98
127
|
Object.keys(pkg.dependencies || {}).forEach((dep) =>
|
|
99
128
|
addPackage(rootDir, dep, imports),
|
|
100
129
|
);
|
|
101
130
|
}
|
|
102
131
|
|
|
103
|
-
function
|
|
132
|
+
function buildImports(srcDir) {
|
|
104
133
|
const rootDir = path.resolve(srcDir, "..");
|
|
105
134
|
const imports = {};
|
|
106
135
|
|
|
@@ -123,7 +152,18 @@ function importMap(srcDir) {
|
|
|
123
152
|
return;
|
|
124
153
|
}
|
|
125
154
|
imports[pkgName] = entry;
|
|
126
|
-
|
|
155
|
+
Object.assign(
|
|
156
|
+
imports,
|
|
157
|
+
subpathImports(
|
|
158
|
+
JSON.parse(
|
|
159
|
+
fs.readFileSync(
|
|
160
|
+
path.join(rootDir, "node_modules", pkgName, "package.json"),
|
|
161
|
+
"utf8",
|
|
162
|
+
),
|
|
163
|
+
),
|
|
164
|
+
pkgName,
|
|
165
|
+
),
|
|
166
|
+
);
|
|
127
167
|
});
|
|
128
168
|
}
|
|
129
169
|
|
|
@@ -132,11 +172,17 @@ function importMap(srcDir) {
|
|
|
132
172
|
if (pkgName in localLibs) return;
|
|
133
173
|
addPackage(rootDir, pkgName, imports);
|
|
134
174
|
});
|
|
175
|
+
return imports;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function importMap(srcDir) {
|
|
135
179
|
return (
|
|
136
180
|
'<script type="importmap">' +
|
|
137
|
-
JSON.stringify({ imports }, null, 2) +
|
|
181
|
+
JSON.stringify({ imports: buildImports(srcDir) }, null, 2) +
|
|
138
182
|
"</script>"
|
|
139
183
|
);
|
|
140
184
|
}
|
|
141
185
|
|
|
142
186
|
module.exports = importMap;
|
|
187
|
+
|
|
188
|
+
module.exports.subpathImports = subpathImports;
|
package/package.json
CHANGED
package/tests/compiler.test.js
CHANGED
|
@@ -1015,6 +1015,253 @@ check(
|
|
|
1015
1015
|
parses(out),
|
|
1016
1016
|
);
|
|
1017
1017
|
|
|
1018
|
+
section("§24 Barrel splitting");
|
|
1019
|
+
|
|
1020
|
+
const splitBarrels = require("../lib/barrelSplit");
|
|
1021
|
+
|
|
1022
|
+
const fixtureRoot = fs.mkdtempSync(nodePath.join(os.tmpdir(), "olum-barrel-"));
|
|
1023
|
+
|
|
1024
|
+
function pkg(name, files) {
|
|
1025
|
+
Object.keys(files).forEach((rel) => {
|
|
1026
|
+
const file = nodePath.join(fixtureRoot, "node_modules", name, rel);
|
|
1027
|
+
fs.mkdirSync(nodePath.dirname(file), { recursive: true });
|
|
1028
|
+
fs.writeFileSync(
|
|
1029
|
+
file,
|
|
1030
|
+
typeof files[rel] === "string"
|
|
1031
|
+
? files[rel]
|
|
1032
|
+
: JSON.stringify(files[rel], null, 2),
|
|
1033
|
+
);
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
const split = (name, statement, assertion) =>
|
|
1038
|
+
checkFn(name, () => splitBarrels(statement, fixtureRoot), assertion);
|
|
1039
|
+
|
|
1040
|
+
const BARREL = `export { A } from "./A.js";\nexport { B } from "./B.js";\n`;
|
|
1041
|
+
const untouched = (statement) => (out) => out === statement;
|
|
1042
|
+
|
|
1043
|
+
pkg("legacy-barrel", {
|
|
1044
|
+
"package.json": { name: "legacy-barrel", main: "./index.js" },
|
|
1045
|
+
"index.js": BARREL,
|
|
1046
|
+
"A.js": "export const A = 1;",
|
|
1047
|
+
"B.js": "export const B = 2;",
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
split(
|
|
1051
|
+
"splits a barrel in a package with no exports field",
|
|
1052
|
+
`import { A } from "legacy-barrel";`,
|
|
1053
|
+
(out) => /^import \{ A as A \} from "legacy-barrel\/A\.js";$/.test(out),
|
|
1054
|
+
);
|
|
1055
|
+
|
|
1056
|
+
pkg("guarded-barrel", {
|
|
1057
|
+
"package.json": {
|
|
1058
|
+
name: "guarded-barrel",
|
|
1059
|
+
exports: { ".": "./dist/index.js", "./extra": "./dist/extra/index.js" },
|
|
1060
|
+
},
|
|
1061
|
+
"dist/index.js": BARREL,
|
|
1062
|
+
"dist/A.js": "export const A = 1;",
|
|
1063
|
+
"dist/B.js": "export const B = 2;",
|
|
1064
|
+
});
|
|
1065
|
+
|
|
1066
|
+
split(
|
|
1067
|
+
"leaves the barrel alone when the target isn't in the exports map",
|
|
1068
|
+
`import { A } from "guarded-barrel";`,
|
|
1069
|
+
untouched(`import { A } from "guarded-barrel";`),
|
|
1070
|
+
);
|
|
1071
|
+
|
|
1072
|
+
pkg("wildcard-barrel", {
|
|
1073
|
+
"package.json": {
|
|
1074
|
+
name: "wildcard-barrel",
|
|
1075
|
+
exports: { ".": "./dist/index.js", "./dist/*": { import: "./dist/*" } },
|
|
1076
|
+
},
|
|
1077
|
+
"dist/index.js": BARREL,
|
|
1078
|
+
"dist/A.js": "export const A = 1;",
|
|
1079
|
+
"dist/B.js": "export const B = 2;",
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
split(
|
|
1083
|
+
"splits when a wildcard subpath exposes the target",
|
|
1084
|
+
`import { A, B } from "wildcard-barrel";`,
|
|
1085
|
+
(out) =>
|
|
1086
|
+
/from "wildcard-barrel\/dist\/A\.js"/.test(out) &&
|
|
1087
|
+
/from "wildcard-barrel\/dist\/B\.js"/.test(out),
|
|
1088
|
+
);
|
|
1089
|
+
|
|
1090
|
+
pkg("blocked-barrel", {
|
|
1091
|
+
"package.json": {
|
|
1092
|
+
name: "blocked-barrel",
|
|
1093
|
+
exports: { ".": "./dist/index.js", "./dist/*": null },
|
|
1094
|
+
},
|
|
1095
|
+
"dist/index.js": BARREL,
|
|
1096
|
+
"dist/A.js": "export const A = 1;",
|
|
1097
|
+
"dist/B.js": "export const B = 2;",
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
split(
|
|
1101
|
+
"leaves the barrel alone when a wildcard maps the target to null",
|
|
1102
|
+
`import { A } from "blocked-barrel";`,
|
|
1103
|
+
untouched(`import { A } from "blocked-barrel";`),
|
|
1104
|
+
);
|
|
1105
|
+
|
|
1106
|
+
pkg("escaping-barrel", {
|
|
1107
|
+
"package.json": {
|
|
1108
|
+
name: "escaping-barrel",
|
|
1109
|
+
exports: { ".": "./dist/index.js", "./dist/*": "./dist/*" },
|
|
1110
|
+
},
|
|
1111
|
+
"dist/index.js": `export { A } from "../outside.js";\n`,
|
|
1112
|
+
"outside.js": "export const A = 1;",
|
|
1113
|
+
});
|
|
1114
|
+
|
|
1115
|
+
split(
|
|
1116
|
+
"leaves the barrel alone when a re-export escapes the package root",
|
|
1117
|
+
`import { A } from "escaping-barrel";`,
|
|
1118
|
+
untouched(`import { A } from "escaping-barrel";`),
|
|
1119
|
+
);
|
|
1120
|
+
|
|
1121
|
+
pkg("shifted-barrel", {
|
|
1122
|
+
"package.json": {
|
|
1123
|
+
name: "shifted-barrel",
|
|
1124
|
+
exports: { ".": "./dist/index.js", "./*.js": "./dist/*.js" },
|
|
1125
|
+
},
|
|
1126
|
+
"dist/index.js": BARREL,
|
|
1127
|
+
"dist/A.js": "export const A = 1;",
|
|
1128
|
+
"dist/B.js": "export const B = 2;",
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
split(
|
|
1132
|
+
"names the target through the pattern, not by its path on disk",
|
|
1133
|
+
`import { A } from "shifted-barrel";`,
|
|
1134
|
+
(out) => /^import \{ A as A \} from "shifted-barrel\/A\.js";$/.test(out),
|
|
1135
|
+
);
|
|
1136
|
+
|
|
1137
|
+
pkg("flat-barrel", {
|
|
1138
|
+
"package.json": {
|
|
1139
|
+
name: "flat-barrel",
|
|
1140
|
+
exports: { ".": "./index.js", "./*.js": "./*.js" },
|
|
1141
|
+
},
|
|
1142
|
+
"index.js": BARREL,
|
|
1143
|
+
"A.js": "export const A = 1;",
|
|
1144
|
+
"B.js": "export const B = 2;",
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
split(
|
|
1148
|
+
"splits when the substituted pattern lands on a real file",
|
|
1149
|
+
`import { A } from "flat-barrel";`,
|
|
1150
|
+
(out) => /^import \{ A as A \} from "flat-barrel\/A\.js";$/.test(out),
|
|
1151
|
+
);
|
|
1152
|
+
|
|
1153
|
+
split(
|
|
1154
|
+
"leaves a subpath import alone when it isn't a barrel",
|
|
1155
|
+
`import { A } from "wildcard-barrel/dist/A.js";`,
|
|
1156
|
+
untouched(`import { A } from "wildcard-barrel/dist/A.js";`),
|
|
1157
|
+
);
|
|
1158
|
+
|
|
1159
|
+
pkg("nested-barrel", {
|
|
1160
|
+
"package.json": {
|
|
1161
|
+
name: "nested-barrel",
|
|
1162
|
+
exports: {
|
|
1163
|
+
".": "./dist/index.js",
|
|
1164
|
+
"./sub": "./dist/sub/index.js",
|
|
1165
|
+
"./*.js": "./dist/*.js",
|
|
1166
|
+
},
|
|
1167
|
+
},
|
|
1168
|
+
"dist/index.js": BARREL,
|
|
1169
|
+
"dist/A.js": "export const A = 1;",
|
|
1170
|
+
"dist/B.js": "export const B = 2;",
|
|
1171
|
+
"dist/sub/index.js": `export { C } from "./C.js";\n`,
|
|
1172
|
+
"dist/sub/C.js": "export const C = 3;",
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
split(
|
|
1176
|
+
"splits a barrel reached through a subpath",
|
|
1177
|
+
`import { C } from "nested-barrel/sub";`,
|
|
1178
|
+
(out) => /^import \{ C as C \} from "nested-barrel\/sub\/C\.js";$/.test(out),
|
|
1179
|
+
);
|
|
1180
|
+
|
|
1181
|
+
pkg("legacy-nested-barrel", {
|
|
1182
|
+
"package.json": { name: "legacy-nested-barrel", main: "./index.js" },
|
|
1183
|
+
"index.js": BARREL,
|
|
1184
|
+
"A.js": "export const A = 1;",
|
|
1185
|
+
"B.js": "export const B = 2;",
|
|
1186
|
+
"sub/index.js": `export { C } from "./C.js";\n`,
|
|
1187
|
+
"sub/C.js": "export const C = 3;",
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
split(
|
|
1191
|
+
"splits a subpath barrel in a package with no exports field",
|
|
1192
|
+
`import { C } from "legacy-nested-barrel/sub";`,
|
|
1193
|
+
(out) =>
|
|
1194
|
+
/^import \{ C as C \} from "legacy-nested-barrel\/sub\/C\.js";$/.test(out),
|
|
1195
|
+
);
|
|
1196
|
+
|
|
1197
|
+
pkg("opaque-barrel", {
|
|
1198
|
+
"package.json": {
|
|
1199
|
+
name: "opaque-barrel",
|
|
1200
|
+
exports: { ".": "./index.js", "./icons/*": "./dist/icons/*.js" },
|
|
1201
|
+
},
|
|
1202
|
+
"index.js": `export { A } from "./dist/icons/A.js";\n`,
|
|
1203
|
+
"dist/icons/A.js": "export const A = 1;",
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
split(
|
|
1207
|
+
"leaves the barrel alone when the import map can't express the pattern",
|
|
1208
|
+
`import { A } from "opaque-barrel";`,
|
|
1209
|
+
untouched(`import { A } from "opaque-barrel";`),
|
|
1210
|
+
);
|
|
1211
|
+
|
|
1212
|
+
fs.rmSync(fixtureRoot, { recursive: true, force: true });
|
|
1213
|
+
|
|
1214
|
+
section("§25 Import map subpaths");
|
|
1215
|
+
|
|
1216
|
+
const { subpathImports } = require("../lib/importMap");
|
|
1217
|
+
|
|
1218
|
+
const mapOf = (name, exports, assertion) =>
|
|
1219
|
+
checkFn(name, () => subpathImports({ exports }, "pkg"), assertion);
|
|
1220
|
+
|
|
1221
|
+
mapOf(
|
|
1222
|
+
"falls back to a prefix mapping when there is no exports field",
|
|
1223
|
+
undefined,
|
|
1224
|
+
(out) => out["pkg/"] === "/node_modules/pkg/",
|
|
1225
|
+
);
|
|
1226
|
+
|
|
1227
|
+
mapOf(
|
|
1228
|
+
"maps each subpath alias to its real file and drops the prefix mapping",
|
|
1229
|
+
{ ".": "./dist/index.js", "./lucide": "./dist/lucide/index.js" },
|
|
1230
|
+
(out) =>
|
|
1231
|
+
out["pkg/lucide"] === "/node_modules/pkg/dist/lucide/index.js" &&
|
|
1232
|
+
!("pkg/" in out),
|
|
1233
|
+
);
|
|
1234
|
+
|
|
1235
|
+
mapOf(
|
|
1236
|
+
"resolves a subpath given as a conditions object",
|
|
1237
|
+
{ "./lucide": { browser: "./dist/lucide/index.js", default: "./x.js" } },
|
|
1238
|
+
(out) => out["pkg/lucide"] === "/node_modules/pkg/dist/lucide/index.js",
|
|
1239
|
+
);
|
|
1240
|
+
|
|
1241
|
+
mapOf(
|
|
1242
|
+
"turns a trailing wildcard into a prefix mapping",
|
|
1243
|
+
{ "./dist/*": "./dist/*" },
|
|
1244
|
+
(out) => out["pkg/dist/"] === "/node_modules/pkg/dist/",
|
|
1245
|
+
);
|
|
1246
|
+
|
|
1247
|
+
mapOf(
|
|
1248
|
+
"turns a wildcard with a matching suffix into a prefix mapping",
|
|
1249
|
+
{ "./*.js": "./dist/*.js" },
|
|
1250
|
+
(out) => out["pkg/"] === "/node_modules/pkg/dist/",
|
|
1251
|
+
);
|
|
1252
|
+
|
|
1253
|
+
mapOf(
|
|
1254
|
+
"skips a wildcard an import map can't express",
|
|
1255
|
+
{ ".": "./index.js", "./icons/*": "./dist/icons/*.js" },
|
|
1256
|
+
(out) => Object.keys(out).length === 0,
|
|
1257
|
+
);
|
|
1258
|
+
|
|
1259
|
+
mapOf(
|
|
1260
|
+
"skips a subpath the package blocks with null",
|
|
1261
|
+
{ ".": "./index.js", "./internal": null },
|
|
1262
|
+
(out) => Object.keys(out).length === 0,
|
|
1263
|
+
);
|
|
1264
|
+
|
|
1018
1265
|
console.log("\n========================");
|
|
1019
1266
|
const summary = `${passed} passed, ${failed} failed`;
|
|
1020
1267
|
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
@@ -1032,7 +1279,7 @@ if (failed) {
|
|
|
1032
1279
|
console.log("");
|
|
1033
1280
|
}
|
|
1034
1281
|
|
|
1035
|
-
const EXPECTED_CHECKS =
|
|
1282
|
+
const EXPECTED_CHECKS = 108;
|
|
1036
1283
|
const total = passed + failed;
|
|
1037
1284
|
if (total !== EXPECTED_CHECKS) {
|
|
1038
1285
|
console.log(
|