olum-compiler 1.8.3 → 1.9.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/lib/barrelSplit.js +190 -0
- package/lib/importMap.js +60 -5
- package/lib/parser.js +3 -1
- package/package.json +1 -1
- package/tests/compiler.test.js +197 -1
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const acorn = require("acorn");
|
|
4
|
+
|
|
5
|
+
const cache = new Map();
|
|
6
|
+
|
|
7
|
+
function pickEntry(exp) {
|
|
8
|
+
if (typeof exp === "string") return exp;
|
|
9
|
+
if (Array.isArray(exp)) {
|
|
10
|
+
for (const item of exp) {
|
|
11
|
+
const found = pickEntry(item);
|
|
12
|
+
if (found) return found;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (exp && typeof exp === "object") {
|
|
17
|
+
for (const key of ["browser", "import", "module", "default"]) {
|
|
18
|
+
if (exp[key]) {
|
|
19
|
+
const found = pickEntry(exp[key]);
|
|
20
|
+
if (found) return found;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// A package with an `exports` map is only reachable through the subpaths it
|
|
28
|
+
// lists -- a file that exists on disk but isn't exported can't be imported by
|
|
29
|
+
// name, and bundlers hard-fail on the attempt. Deep-importing a barrel member
|
|
30
|
+
// is only safe once the target survives this check. Mirrors the pattern
|
|
31
|
+
// matching in Node's PACKAGE_EXPORTS_RESOLVE, minus the condition ordering
|
|
32
|
+
// (pickEntry already picks a browser/ESM branch).
|
|
33
|
+
function exposes(exp, subpath, dir) {
|
|
34
|
+
// Matching a pattern isn't enough -- the wildcard has to be substituted back
|
|
35
|
+
// into the target and the result has to exist. "./*.js": "./dist/*.js" does
|
|
36
|
+
// match "./dist/OlumLogo.js", but resolves it to "./dist/dist/OlumLogo.js",
|
|
37
|
+
// which is nothing. Approving that would emit a specifier no bundler
|
|
38
|
+
// resolves, which is the failure this whole check exists to prevent.
|
|
39
|
+
const lands = (target) =>
|
|
40
|
+
typeof target === "string" &&
|
|
41
|
+
fs.existsSync(path.resolve(dir, target.replace(/^\.?\//, "")));
|
|
42
|
+
|
|
43
|
+
// No exports field at all: legacy resolution, every file is addressable.
|
|
44
|
+
if (exp == null) return true;
|
|
45
|
+
|
|
46
|
+
// A string, an array or a bare conditions object describes the root export
|
|
47
|
+
// and nothing else, so no deep path is reachable.
|
|
48
|
+
const isSubpathMap =
|
|
49
|
+
typeof exp === "object" &&
|
|
50
|
+
!Array.isArray(exp) &&
|
|
51
|
+
Object.keys(exp).some((key) => key.startsWith("."));
|
|
52
|
+
if (!isSubpathMap) return false;
|
|
53
|
+
|
|
54
|
+
if (Object.hasOwn(exp, subpath)) return lands(pickEntry(exp[subpath]));
|
|
55
|
+
|
|
56
|
+
// Longest matching prefix wins, as in Node's resolver.
|
|
57
|
+
let best = null;
|
|
58
|
+
for (const key of Object.keys(exp)) {
|
|
59
|
+
const star = key.indexOf("*");
|
|
60
|
+
if (star === -1) continue;
|
|
61
|
+
const prefix = key.slice(0, star);
|
|
62
|
+
const suffix = key.slice(star + 1);
|
|
63
|
+
if (subpath.length < prefix.length + suffix.length) continue;
|
|
64
|
+
if (!subpath.startsWith(prefix)) continue;
|
|
65
|
+
if (suffix && !subpath.endsWith(suffix)) continue;
|
|
66
|
+
if (!best || prefix.length > best.prefix.length)
|
|
67
|
+
best = { prefix, suffix, value: exp[key] };
|
|
68
|
+
}
|
|
69
|
+
if (!best) return false;
|
|
70
|
+
|
|
71
|
+
const target = pickEntry(best.value);
|
|
72
|
+
if (typeof target !== "string" || !target.includes("*")) return lands(target);
|
|
73
|
+
|
|
74
|
+
const captured = subpath.slice(
|
|
75
|
+
best.prefix.length,
|
|
76
|
+
best.suffix ? subpath.length - best.suffix.length : undefined,
|
|
77
|
+
);
|
|
78
|
+
return lands(target.replace("*", captured));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const nameOf = (node) => (node.type === "Identifier" ? node.name : node.value);
|
|
82
|
+
|
|
83
|
+
function barrelMap(rootDir, pkgName) {
|
|
84
|
+
const key = rootDir + "\0" + pkgName;
|
|
85
|
+
if (cache.has(key)) return cache.get(key);
|
|
86
|
+
|
|
87
|
+
let map = null;
|
|
88
|
+
try {
|
|
89
|
+
const dir = path.join(rootDir, "node_modules", pkgName);
|
|
90
|
+
const pkg = JSON.parse(
|
|
91
|
+
fs.readFileSync(path.join(dir, "package.json"), "utf8"),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
let exp = pkg.exports;
|
|
95
|
+
if (exp && typeof exp === "object" && !Array.isArray(exp)) {
|
|
96
|
+
const keys = Object.keys(exp);
|
|
97
|
+
if (keys.some((k) => k.startsWith("."))) exp = exp["."];
|
|
98
|
+
}
|
|
99
|
+
let entry = pickEntry(exp) || pkg.module || pkg.main || "index.js";
|
|
100
|
+
let entryPath = path.resolve(dir, entry.replace(/^\.?\//, ""));
|
|
101
|
+
if (fs.existsSync(entryPath) && fs.statSync(entryPath).isDirectory())
|
|
102
|
+
entryPath = path.join(entryPath, "index.js");
|
|
103
|
+
|
|
104
|
+
const body = acorn.parse(fs.readFileSync(entryPath, "utf8"), {
|
|
105
|
+
ecmaVersion: "latest",
|
|
106
|
+
sourceType: "module",
|
|
107
|
+
}).body;
|
|
108
|
+
|
|
109
|
+
const isPureBarrel =
|
|
110
|
+
body.length > 0 &&
|
|
111
|
+
body.every(
|
|
112
|
+
(node) => node.type === "ExportNamedDeclaration" && node.source,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
if (isPureBarrel) {
|
|
116
|
+
map = new Map();
|
|
117
|
+
body.forEach((node) => {
|
|
118
|
+
const target = path.resolve(path.dirname(entryPath), node.source.value);
|
|
119
|
+
const rel = path.relative(dir, target).split(path.sep).join("/");
|
|
120
|
+
// Also drops re-exports that resolve outside the package ("../").
|
|
121
|
+
if (!exposes(pkg.exports, "./" + rel, dir)) return;
|
|
122
|
+
const spec = pkgName + "/" + rel;
|
|
123
|
+
node.specifiers.forEach((item) =>
|
|
124
|
+
map.set(nameOf(item.exported), {
|
|
125
|
+
spec,
|
|
126
|
+
imported: nameOf(item.local),
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
} catch (e) {
|
|
132
|
+
map = null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
cache.set(key, map);
|
|
136
|
+
return map;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function splitBarrels(statement, rootDir) {
|
|
140
|
+
const root = (rootDir || process.cwd()).replace(/\/module$/, "");
|
|
141
|
+
|
|
142
|
+
let node;
|
|
143
|
+
try {
|
|
144
|
+
const body = acorn.parse(statement, {
|
|
145
|
+
ecmaVersion: "latest",
|
|
146
|
+
sourceType: "module",
|
|
147
|
+
}).body;
|
|
148
|
+
if (body.length !== 1 || body[0].type !== "ImportDeclaration")
|
|
149
|
+
return statement;
|
|
150
|
+
node = body[0];
|
|
151
|
+
} catch (e) {
|
|
152
|
+
return statement;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const source = node.source.value;
|
|
156
|
+
if (/^[./]/.test(source) || /^https?:/.test(source)) return statement;
|
|
157
|
+
|
|
158
|
+
const segments = source.split("/");
|
|
159
|
+
const pkgName = source.startsWith("@")
|
|
160
|
+
? segments.slice(0, 2).join("/")
|
|
161
|
+
: segments[0];
|
|
162
|
+
if (pkgName !== source) return statement;
|
|
163
|
+
if (!node.specifiers.length) return statement;
|
|
164
|
+
if (!node.specifiers.every((item) => item.type === "ImportSpecifier"))
|
|
165
|
+
return statement;
|
|
166
|
+
|
|
167
|
+
const map = barrelMap(root, pkgName);
|
|
168
|
+
if (!map) return statement;
|
|
169
|
+
|
|
170
|
+
const lines = [];
|
|
171
|
+
for (const item of node.specifiers) {
|
|
172
|
+
const hit = map.get(nameOf(item.imported));
|
|
173
|
+
if (!hit) return statement;
|
|
174
|
+
const local = item.local.name;
|
|
175
|
+
lines.push(
|
|
176
|
+
hit.imported === "default"
|
|
177
|
+
? "import " + local + ' from "' + hit.spec + '";'
|
|
178
|
+
: "import { " +
|
|
179
|
+
hit.imported +
|
|
180
|
+
" as " +
|
|
181
|
+
local +
|
|
182
|
+
' } from "' +
|
|
183
|
+
hit.spec +
|
|
184
|
+
'";',
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return lines.join("\n");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = splitBarrels;
|
package/lib/importMap.js
CHANGED
|
@@ -25,6 +25,43 @@ function pickEntry(exp) {
|
|
|
25
25
|
return null;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// A `pkgName + "/"` prefix entry resolves every subpath straight to disk, which
|
|
29
|
+
// is only how Node behaves for a package with no `exports` map. Where a map
|
|
30
|
+
// exists its keys are aliases -- "olum-icons/lucide" names dist/lucide/index.js,
|
|
31
|
+
// it isn't a path -- so each one needs an entry of its own, and the blanket
|
|
32
|
+
// prefix has to go: it would let dev resolve specifiers the bundler rejects.
|
|
33
|
+
function subpathImports(pkg, pkgName) {
|
|
34
|
+
const base = "/node_modules/" + pkgName + "/";
|
|
35
|
+
const exp = pkg.exports;
|
|
36
|
+
const isSubpathMap =
|
|
37
|
+
exp &&
|
|
38
|
+
typeof exp === "object" &&
|
|
39
|
+
!Array.isArray(exp) &&
|
|
40
|
+
Object.keys(exp).some((key) => key.startsWith("."));
|
|
41
|
+
if (!isSubpathMap) return { [pkgName + "/"]: base };
|
|
42
|
+
|
|
43
|
+
const imports = {};
|
|
44
|
+
Object.keys(exp).forEach((key) => {
|
|
45
|
+
if (key === "." || !key.startsWith(".")) return;
|
|
46
|
+
const target = pickEntry(exp[key]);
|
|
47
|
+
if (!target) return;
|
|
48
|
+
if (!key.includes("*")) {
|
|
49
|
+
imports[pkgName + key.slice(1)] = base + target.replace(/^\.?\//, "");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// An import map only substitutes prefixes, so a pattern is expressible
|
|
53
|
+
// when both sides carry the same text after the wildcard -- that text is
|
|
54
|
+
// then part of the specifier the browser writes and needs no rewriting.
|
|
55
|
+
// "./*.js": "./dist/*.js" becomes "pkg/" -> "/node_modules/pkg/dist/".
|
|
56
|
+
const [keyPrefix, keySuffix] = key.split("*");
|
|
57
|
+
const [targetPrefix, targetSuffix] = target.split("*");
|
|
58
|
+
if (keySuffix !== targetSuffix) return;
|
|
59
|
+
imports[pkgName + keyPrefix.slice(1)] =
|
|
60
|
+
base + targetPrefix.replace(/^\.?\//, "");
|
|
61
|
+
});
|
|
62
|
+
return imports;
|
|
63
|
+
}
|
|
64
|
+
|
|
28
65
|
function resolvePackage(rootDir, pkgName) {
|
|
29
66
|
const pkgJsonPath = path.join(
|
|
30
67
|
rootDir,
|
|
@@ -86,8 +123,6 @@ function addPackage(rootDir, pkgName, imports) {
|
|
|
86
123
|
);
|
|
87
124
|
return;
|
|
88
125
|
}
|
|
89
|
-
imports[pkgName] = entry;
|
|
90
|
-
imports[pkgName + "/"] = "/node_modules/" + pkgName + "/";
|
|
91
126
|
const pkgJsonPath = path.join(
|
|
92
127
|
rootDir,
|
|
93
128
|
"node_modules",
|
|
@@ -95,12 +130,14 @@ function addPackage(rootDir, pkgName, imports) {
|
|
|
95
130
|
"package.json",
|
|
96
131
|
);
|
|
97
132
|
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, "utf8"));
|
|
133
|
+
imports[pkgName] = entry;
|
|
134
|
+
Object.assign(imports, subpathImports(pkg, pkgName));
|
|
98
135
|
Object.keys(pkg.dependencies || {}).forEach((dep) =>
|
|
99
136
|
addPackage(rootDir, dep, imports),
|
|
100
137
|
);
|
|
101
138
|
}
|
|
102
139
|
|
|
103
|
-
function
|
|
140
|
+
function buildImports(srcDir) {
|
|
104
141
|
const rootDir = path.resolve(srcDir, "..");
|
|
105
142
|
const imports = {};
|
|
106
143
|
|
|
@@ -123,7 +160,18 @@ function importMap(srcDir) {
|
|
|
123
160
|
return;
|
|
124
161
|
}
|
|
125
162
|
imports[pkgName] = entry;
|
|
126
|
-
|
|
163
|
+
Object.assign(
|
|
164
|
+
imports,
|
|
165
|
+
subpathImports(
|
|
166
|
+
JSON.parse(
|
|
167
|
+
fs.readFileSync(
|
|
168
|
+
path.join(rootDir, "node_modules", pkgName, "package.json"),
|
|
169
|
+
"utf8",
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
pkgName,
|
|
173
|
+
),
|
|
174
|
+
);
|
|
127
175
|
});
|
|
128
176
|
}
|
|
129
177
|
|
|
@@ -132,11 +180,18 @@ function importMap(srcDir) {
|
|
|
132
180
|
if (pkgName in localLibs) return;
|
|
133
181
|
addPackage(rootDir, pkgName, imports);
|
|
134
182
|
});
|
|
183
|
+
return imports;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function importMap(srcDir) {
|
|
135
187
|
return (
|
|
136
188
|
'<script type="importmap">' +
|
|
137
|
-
JSON.stringify({ imports }, null, 2) +
|
|
189
|
+
JSON.stringify({ imports: buildImports(srcDir) }, null, 2) +
|
|
138
190
|
"</script>"
|
|
139
191
|
);
|
|
140
192
|
}
|
|
141
193
|
|
|
142
194
|
module.exports = importMap;
|
|
195
|
+
// Exposed for the test suite -- the interesting logic is the exports-map
|
|
196
|
+
// translation, which shouldn't need a whole node_modules tree to exercise.
|
|
197
|
+
module.exports.subpathImports = subpathImports;
|
package/lib/parser.js
CHANGED
|
@@ -6,6 +6,7 @@ const cssParser = require("./cssParser");
|
|
|
6
6
|
const jsParser = require("./jsParser");
|
|
7
7
|
const getImports = require("./getImports");
|
|
8
8
|
const toJs = require("./toJs");
|
|
9
|
+
const splitBarrels = require("./barrelSplit");
|
|
9
10
|
const path = require("path");
|
|
10
11
|
const {
|
|
11
12
|
isFullArr,
|
|
@@ -131,7 +132,8 @@ const parser = (str, counter, filePath) => {
|
|
|
131
132
|
} catch (e) {}
|
|
132
133
|
}
|
|
133
134
|
|
|
134
|
-
if (imports)
|
|
135
|
+
if (imports)
|
|
136
|
+
imports = imports.map((item) => splitBarrels(toJs(item.statement)));
|
|
135
137
|
|
|
136
138
|
function mkComponentsList(comps) {
|
|
137
139
|
if (!imports || !isFullArr(imports)) return "";
|
package/package.json
CHANGED
package/tests/compiler.test.js
CHANGED
|
@@ -1015,6 +1015,202 @@ check(
|
|
|
1015
1015
|
parses(out),
|
|
1016
1016
|
);
|
|
1017
1017
|
|
|
1018
|
+
section("§24 Barrel splitting");
|
|
1019
|
+
|
|
1020
|
+
const splitBarrels = require("../lib/barrelSplit");
|
|
1021
|
+
|
|
1022
|
+
// splitBarrels reads the dependency off disk, so these run against throwaway
|
|
1023
|
+
// packages instead of a compiled template.
|
|
1024
|
+
const fixtureRoot = fs.mkdtempSync(nodePath.join(os.tmpdir(), "olum-barrel-"));
|
|
1025
|
+
|
|
1026
|
+
function pkg(name, files) {
|
|
1027
|
+
Object.keys(files).forEach((rel) => {
|
|
1028
|
+
const file = nodePath.join(fixtureRoot, "node_modules", name, rel);
|
|
1029
|
+
fs.mkdirSync(nodePath.dirname(file), { recursive: true });
|
|
1030
|
+
fs.writeFileSync(
|
|
1031
|
+
file,
|
|
1032
|
+
typeof files[rel] === "string"
|
|
1033
|
+
? files[rel]
|
|
1034
|
+
: JSON.stringify(files[rel], null, 2),
|
|
1035
|
+
);
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
const split = (name, statement, assertion) =>
|
|
1040
|
+
checkFn(name, () => splitBarrels(statement, fixtureRoot), assertion);
|
|
1041
|
+
|
|
1042
|
+
const BARREL = `export { A } from "./A.js";\nexport { B } from "./B.js";\n`;
|
|
1043
|
+
const untouched = (statement) => (out) => out === statement;
|
|
1044
|
+
|
|
1045
|
+
pkg("legacy-barrel", {
|
|
1046
|
+
"package.json": { name: "legacy-barrel", main: "./index.js" },
|
|
1047
|
+
"index.js": BARREL,
|
|
1048
|
+
"A.js": "export const A = 1;",
|
|
1049
|
+
"B.js": "export const B = 2;",
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
split(
|
|
1053
|
+
"splits a barrel in a package with no exports field",
|
|
1054
|
+
`import { A } from "legacy-barrel";`,
|
|
1055
|
+
(out) => /^import \{ A as A \} from "legacy-barrel\/A\.js";$/.test(out),
|
|
1056
|
+
);
|
|
1057
|
+
|
|
1058
|
+
pkg("guarded-barrel", {
|
|
1059
|
+
"package.json": {
|
|
1060
|
+
name: "guarded-barrel",
|
|
1061
|
+
exports: { ".": "./dist/index.js", "./extra": "./dist/extra/index.js" },
|
|
1062
|
+
},
|
|
1063
|
+
"dist/index.js": BARREL,
|
|
1064
|
+
"dist/A.js": "export const A = 1;",
|
|
1065
|
+
"dist/B.js": "export const B = 2;",
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
split(
|
|
1069
|
+
"leaves the barrel alone when the target isn't in the exports map",
|
|
1070
|
+
`import { A } from "guarded-barrel";`,
|
|
1071
|
+
untouched(`import { A } from "guarded-barrel";`),
|
|
1072
|
+
);
|
|
1073
|
+
|
|
1074
|
+
pkg("wildcard-barrel", {
|
|
1075
|
+
"package.json": {
|
|
1076
|
+
name: "wildcard-barrel",
|
|
1077
|
+
exports: { ".": "./dist/index.js", "./dist/*": { import: "./dist/*" } },
|
|
1078
|
+
},
|
|
1079
|
+
"dist/index.js": BARREL,
|
|
1080
|
+
"dist/A.js": "export const A = 1;",
|
|
1081
|
+
"dist/B.js": "export const B = 2;",
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1084
|
+
split(
|
|
1085
|
+
"splits when a wildcard subpath exposes the target",
|
|
1086
|
+
`import { A, B } from "wildcard-barrel";`,
|
|
1087
|
+
(out) =>
|
|
1088
|
+
/from "wildcard-barrel\/dist\/A\.js"/.test(out) &&
|
|
1089
|
+
/from "wildcard-barrel\/dist\/B\.js"/.test(out),
|
|
1090
|
+
);
|
|
1091
|
+
|
|
1092
|
+
pkg("blocked-barrel", {
|
|
1093
|
+
"package.json": {
|
|
1094
|
+
name: "blocked-barrel",
|
|
1095
|
+
exports: { ".": "./dist/index.js", "./dist/*": null },
|
|
1096
|
+
},
|
|
1097
|
+
"dist/index.js": BARREL,
|
|
1098
|
+
"dist/A.js": "export const A = 1;",
|
|
1099
|
+
"dist/B.js": "export const B = 2;",
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
split(
|
|
1103
|
+
"leaves the barrel alone when a wildcard maps the target to null",
|
|
1104
|
+
`import { A } from "blocked-barrel";`,
|
|
1105
|
+
untouched(`import { A } from "blocked-barrel";`),
|
|
1106
|
+
);
|
|
1107
|
+
|
|
1108
|
+
pkg("escaping-barrel", {
|
|
1109
|
+
"package.json": {
|
|
1110
|
+
name: "escaping-barrel",
|
|
1111
|
+
exports: { ".": "./dist/index.js", "./dist/*": "./dist/*" },
|
|
1112
|
+
},
|
|
1113
|
+
"dist/index.js": `export { A } from "../outside.js";\n`,
|
|
1114
|
+
"outside.js": "export const A = 1;",
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
split(
|
|
1118
|
+
"leaves the barrel alone when a re-export escapes the package root",
|
|
1119
|
+
`import { A } from "escaping-barrel";`,
|
|
1120
|
+
untouched(`import { A } from "escaping-barrel";`),
|
|
1121
|
+
);
|
|
1122
|
+
|
|
1123
|
+
pkg("shifted-barrel", {
|
|
1124
|
+
"package.json": {
|
|
1125
|
+
name: "shifted-barrel",
|
|
1126
|
+
exports: { ".": "./dist/index.js", "./*.js": "./dist/*.js" },
|
|
1127
|
+
},
|
|
1128
|
+
"dist/index.js": BARREL,
|
|
1129
|
+
"dist/A.js": "export const A = 1;",
|
|
1130
|
+
"dist/B.js": "export const B = 2;",
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
split(
|
|
1134
|
+
"leaves the barrel alone when the pattern matches but resolves nowhere",
|
|
1135
|
+
`import { A } from "shifted-barrel";`,
|
|
1136
|
+
untouched(`import { A } from "shifted-barrel";`),
|
|
1137
|
+
);
|
|
1138
|
+
|
|
1139
|
+
pkg("flat-barrel", {
|
|
1140
|
+
"package.json": {
|
|
1141
|
+
name: "flat-barrel",
|
|
1142
|
+
exports: { ".": "./index.js", "./*.js": "./*.js" },
|
|
1143
|
+
},
|
|
1144
|
+
"index.js": BARREL,
|
|
1145
|
+
"A.js": "export const A = 1;",
|
|
1146
|
+
"B.js": "export const B = 2;",
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
split(
|
|
1150
|
+
"splits when the substituted pattern lands on a real file",
|
|
1151
|
+
`import { A } from "flat-barrel";`,
|
|
1152
|
+
(out) => /^import \{ A as A \} from "flat-barrel\/A\.js";$/.test(out),
|
|
1153
|
+
);
|
|
1154
|
+
|
|
1155
|
+
split(
|
|
1156
|
+
"leaves a subpath import alone",
|
|
1157
|
+
`import { A } from "wildcard-barrel/dist/A.js";`,
|
|
1158
|
+
untouched(`import { A } from "wildcard-barrel/dist/A.js";`),
|
|
1159
|
+
);
|
|
1160
|
+
|
|
1161
|
+
fs.rmSync(fixtureRoot, { recursive: true, force: true });
|
|
1162
|
+
|
|
1163
|
+
section("§25 Import map subpaths");
|
|
1164
|
+
|
|
1165
|
+
const { subpathImports } = require("../lib/importMap");
|
|
1166
|
+
|
|
1167
|
+
const mapOf = (name, exports, assertion) =>
|
|
1168
|
+
checkFn(name, () => subpathImports({ exports }, "pkg"), assertion);
|
|
1169
|
+
|
|
1170
|
+
mapOf(
|
|
1171
|
+
"falls back to a prefix mapping when there is no exports field",
|
|
1172
|
+
undefined,
|
|
1173
|
+
(out) => out["pkg/"] === "/node_modules/pkg/",
|
|
1174
|
+
);
|
|
1175
|
+
|
|
1176
|
+
mapOf(
|
|
1177
|
+
"maps each subpath alias to its real file and drops the prefix mapping",
|
|
1178
|
+
{ ".": "./dist/index.js", "./lucide": "./dist/lucide/index.js" },
|
|
1179
|
+
(out) =>
|
|
1180
|
+
out["pkg/lucide"] === "/node_modules/pkg/dist/lucide/index.js" &&
|
|
1181
|
+
!("pkg/" in out),
|
|
1182
|
+
);
|
|
1183
|
+
|
|
1184
|
+
mapOf(
|
|
1185
|
+
"resolves a subpath given as a conditions object",
|
|
1186
|
+
{ "./lucide": { browser: "./dist/lucide/index.js", default: "./x.js" } },
|
|
1187
|
+
(out) => out["pkg/lucide"] === "/node_modules/pkg/dist/lucide/index.js",
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
mapOf(
|
|
1191
|
+
"turns a trailing wildcard into a prefix mapping",
|
|
1192
|
+
{ "./dist/*": "./dist/*" },
|
|
1193
|
+
(out) => out["pkg/dist/"] === "/node_modules/pkg/dist/",
|
|
1194
|
+
);
|
|
1195
|
+
|
|
1196
|
+
mapOf(
|
|
1197
|
+
"turns a wildcard with a matching suffix into a prefix mapping",
|
|
1198
|
+
{ "./*.js": "./dist/*.js" },
|
|
1199
|
+
(out) => out["pkg/"] === "/node_modules/pkg/dist/",
|
|
1200
|
+
);
|
|
1201
|
+
|
|
1202
|
+
mapOf(
|
|
1203
|
+
"skips a wildcard an import map can't express",
|
|
1204
|
+
{ ".": "./index.js", "./icons/*": "./dist/icons/*.js" },
|
|
1205
|
+
(out) => Object.keys(out).length === 0,
|
|
1206
|
+
);
|
|
1207
|
+
|
|
1208
|
+
mapOf(
|
|
1209
|
+
"skips a subpath the package blocks with null",
|
|
1210
|
+
{ ".": "./index.js", "./internal": null },
|
|
1211
|
+
(out) => Object.keys(out).length === 0,
|
|
1212
|
+
);
|
|
1213
|
+
|
|
1018
1214
|
console.log("\n========================");
|
|
1019
1215
|
const summary = `${passed} passed, ${failed} failed`;
|
|
1020
1216
|
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
@@ -1032,7 +1228,7 @@ if (failed) {
|
|
|
1032
1228
|
console.log("");
|
|
1033
1229
|
}
|
|
1034
1230
|
|
|
1035
|
-
const EXPECTED_CHECKS =
|
|
1231
|
+
const EXPECTED_CHECKS = 105;
|
|
1036
1232
|
const total = passed + failed;
|
|
1037
1233
|
if (total !== EXPECTED_CHECKS) {
|
|
1038
1234
|
console.log(
|