Skip to content

Commit

Permalink
Merge pull request #380 from webpack/issue-355
Browse files Browse the repository at this point in the history
fix: resolve for exports fields and exports fields
  • Loading branch information
TheLarkInn authored May 8, 2023
2 parents da81b8e + a420b0f commit 35af1c4
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 12 deletions.
33 changes: 28 additions & 5 deletions lib/ExtensionAliasPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,40 @@ module.exports = class ExtensionAliasPlugin {
.tapAsync("ExtensionAliasPlugin", (request, resolveContext, callback) => {
const requestPath = request.request;
if (!requestPath || !requestPath.endsWith(extension)) return callback();
const resolve = (alias, callback) => {
resolver.doResolve(
const isAliasString = typeof alias === "string";
const resolve = (alias, callback, index) => {
const newRequest = `${requestPath.slice(
0,
-extension.length
)}${alias}`;

return resolver.doResolve(
target,
{
...request,
request: `${requestPath.slice(0, -extension.length)}${alias}`,
request: newRequest,
fullySpecified: true
},
`aliased from extension alias with mapping '${extension}' to '${alias}'`,
resolveContext,
callback
(err, result) => {
// Throw error if we are on the last alias (for multiple aliases) and it failed, always throw if we are not an array or we have only one alias
if (!isAliasString && index) {
if (index !== this.options.alias.length) {
if (resolveContext.log) {
resolveContext.log(
`Failed to alias from extension alias with mapping '${extension}' to '${alias}' for '${newRequest}': ${err}`
);
}

return callback(null, result);
}

return callback(err, result);
} else {
callback(err, result);
}
}
);
};

Expand All @@ -56,7 +79,7 @@ module.exports = class ExtensionAliasPlugin {
// Don't allow other aliasing or raw request
return callback(null, null);
};
if (typeof alias === "string") {
if (isAliasString) {
resolve(alias, stoppingCallback);
} else if (alias.length > 1) {
forEachBail(alias, resolve, stoppingCallback);
Expand Down
18 changes: 11 additions & 7 deletions lib/forEachBail.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ module.exports = function forEachBail(array, iterator, callback) {
let i = 0;
const next = () => {
let loop = undefined;
iterator(array[i++], (err, result) => {
if (err || result !== undefined || i >= array.length) {
return callback(err, result);
}
if (loop === false) while (next());
loop = true;
});
iterator(
array[i++],
(err, result) => {
if (err || result !== undefined || i >= array.length) {
return callback(err, result);
}
if (loop === false) while (next());
loop = true;
},
i
);
if (!loop) loop = false;
return loop;
};
Expand Down
121 changes: 121 additions & 0 deletions test/exportsField.js
Original file line number Diff line number Diff line change
Expand Up @@ -2911,4 +2911,125 @@ describe("ExportsFieldPlugin", () => {
}
);
});

it("should resolve with the `extensionAlias` option", done => {
const resolver = ResolverFactory.createResolver({
extensions: [".js"],
extensionAlias: {
".js": [".ts", ".js"]
},
fileSystem: nodeFileSystem,
fullySpecified: true,
conditionNames: ["webpack", "default"]
});
const fixture = path.resolve(
__dirname,
"./fixtures/exports-field-and-extension-alias/"
);

resolver.resolve({}, fixture, "@org/pkg/string.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(
path.resolve(fixture, "./node_modules/@org/pkg/dist/string.js")
);
done();
});
});

it("should resolve with the `extensionAlias` option #2", done => {
const resolver = ResolverFactory.createResolver({
extensions: [".js"],
extensionAlias: {
".js": [".ts", ".js"]
},
fileSystem: nodeFileSystem,
fullySpecified: true,
conditionNames: ["webpack", "default"]
});
const fixture = path.resolve(
__dirname,
"./fixtures/exports-field-and-extension-alias/"
);

resolver.resolve({}, fixture, "pkg/string.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(
path.resolve(fixture, "./node_modules/pkg/dist/string.js")
);
done();
});
});

it("should resolve with the `extensionAlias` option #3", done => {
const resolver = ResolverFactory.createResolver({
extensions: [".js"],
extensionAlias: {
".js": [".foo", ".baz", ".baz", ".ts", ".js"]
},
fileSystem: nodeFileSystem,
fullySpecified: true,
conditionNames: ["webpack", "default"]
});
const fixture = path.resolve(
__dirname,
"./fixtures/exports-field-and-extension-alias/"
);

resolver.resolve({}, fixture, "pkg/string.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(
path.resolve(fixture, "./node_modules/pkg/dist/string.js")
);
done();
});
});

it("should throw error with the `extensionAlias` option", done => {
const resolver = ResolverFactory.createResolver({
extensions: [".js"],
extensionAlias: {
".js": [".ts"]
},
fileSystem: nodeFileSystem,
fullySpecified: true,
conditionNames: ["webpack", "default"]
});
const fixture = path.resolve(
__dirname,
"./fixtures/exports-field-and-extension-alias/"
);

resolver.resolve({}, fixture, "pkg/string.js", {}, (err, result) => {
if (!err) throw new Error(`expect error, got ${result}`);
err.should.be.instanceof(Error);
err.message.should.match(/Package path \.\/string\.ts is not exported/);
done();
});
});

it("should throw error with the `extensionAlias` option #2", done => {
const resolver = ResolverFactory.createResolver({
extensions: [".js"],
extensionAlias: {
".js": ".ts"
},
fileSystem: nodeFileSystem,
fullySpecified: true,
conditionNames: ["webpack", "default"]
});
const fixture = path.resolve(
__dirname,
"./fixtures/exports-field-and-extension-alias/"
);

resolver.resolve({}, fixture, "pkg/string.js", {}, (err, result) => {
if (!err) throw new Error(`expect error, got ${result}`);
err.should.be.instanceof(Error);
err.message.should.match(/Package path \.\/string\.ts is not exported/);
done();
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35af1c4

Please sign in to comment.