Skip to content

Commit a9eb058

Browse files
committed
update
1 parent beba384 commit a9eb058

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

lib/AliasUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function aliasResolveHandler(
107107
prefix.length,
108108
innerRequest.length - suffix.length,
109109
);
110-
newRequestStr = item.alias.toString().replace("*", match);
110+
newRequestStr = alias.toString().replace("*", match);
111111
}
112112

113113
if (

lib/TsconfigPathsUtils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,16 @@ function tsconfigPathsToResolveOptions(aliases) {
152152
const modules = [];
153153
for (const opt of aliases) {
154154
if (opt.name === "*") {
155-
modules.push(.../** @type {Array<string>} */ (opt.alias));
155+
modules.push(
156+
.../** @type {Array<string>} */ (opt.alias)
157+
.map((dir) => {
158+
if (/\/\*$/.test(dir)) {
159+
return dir.replace(/\/\*$/, "");
160+
}
161+
return "";
162+
})
163+
.filter(Boolean),
164+
);
156165
} else {
157166
alias.push(opt);
158167
}

test/fixtures/tsconfig-paths/example/src/components/button.ts

Whitespace-only changes.

test/fixtures/tsconfig-paths/example/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"outDir": "./js_out",
66
"baseUrl": ".",
77
"paths": {
8+
"@components/*": ["./src/components/*"],
89
"foo": ["./src/mapped/foo"],
910
"bar/*": ["./src/mapped/bar/*"],
1011
"refs/*": ["./src/refs/*"],
1112
"*": ["./src/mapped/longest/one.ts", "./src/mapped/star/*"],
12-
"longest/*": ["./src/mapped/longest/two.ts"]
13+
"longest/*": ["./src/mapped/longest/four.ts", "./src/mapped/longest/two.ts"]
1314
},
1415
"composite": true
1516
}

test/tsconfig-paths.test.js

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,51 @@ const referenceDir = path.resolve(
2222
);
2323

2424
describe("TsconfigPathsPlugin", () => {
25+
it("resolves exact mapped path '@components/*' via tsconfig option (example)", (done) => {
26+
const resolver = ResolverFactory.createResolver({
27+
fileSystem,
28+
extensions: [".ts", ".tsx"],
29+
mainFields: ["browser", "main"],
30+
mainFiles: ["index"],
31+
tsconfig: path.join(exampleDir, "tsconfig.json"),
32+
useSyncFileSystemCalls: true,
33+
});
34+
35+
resolver.resolve(
36+
{},
37+
exampleDir,
38+
"@components/button",
39+
{},
40+
(err, result) => {
41+
if (err) return done(err);
42+
if (!result) return done(new Error("No result"));
43+
expect(result).toEqual(
44+
path.join(exampleDir, "src", "components", "button.ts"),
45+
);
46+
done();
47+
},
48+
);
49+
});
50+
51+
it("when multiple patterns match a module specifier, the pattern with the longest matching prefix before any * token is used:", (done) => {
52+
const resolver = ResolverFactory.createResolver({
53+
fileSystem,
54+
extensions: [".ts", ".tsx"],
55+
mainFields: ["browser", "main"],
56+
mainFiles: ["index"],
57+
tsconfig: path.join(exampleDir, "tsconfig.json"),
58+
});
59+
60+
resolver.resolve({}, exampleDir, "longest/bar", {}, (err, result) => {
61+
if (err) return done(err);
62+
if (!result) return done(new Error("No result"));
63+
expect(result).toEqual(
64+
path.join(exampleDir, "src", "mapped", "longest", "two.ts"),
65+
);
66+
done();
67+
});
68+
});
69+
2570
it("resolves exact mapped path 'foo' via tsconfig option (example)", (done) => {
2671
const resolver = ResolverFactory.createResolver({
2772
fileSystem,
@@ -109,25 +154,6 @@ describe("TsconfigPathsPlugin", () => {
109154
});
110155
});
111156

112-
it("when multiple patterns match a module specifier, the pattern with the longest matching prefix before any * token is used:", (done) => {
113-
const resolver = ResolverFactory.createResolver({
114-
fileSystem,
115-
extensions: [".ts", ".tsx"],
116-
mainFields: ["browser", "main"],
117-
mainFiles: ["index"],
118-
tsconfig: path.join(exampleDir, "tsconfig.json"),
119-
});
120-
121-
resolver.resolve({}, exampleDir, "longest/bar", {}, (err, result) => {
122-
if (err) return done(err);
123-
if (!result) return done(new Error("No result"));
124-
expect(result).toEqual(
125-
path.join(exampleDir, "src", "mapped", "longest", "two.ts"),
126-
);
127-
done();
128-
});
129-
});
130-
131157
it("resolves 'foo' using references from referenceExample project", (done) => {
132158
const resolver = ResolverFactory.createResolver({
133159
fileSystem,

0 commit comments

Comments
 (0)