Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve for exports fields and exports fields #380

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.