Refactor image path handling in vitePluginNextImage#67
Open
valentinpalkovic wants to merge 1 commit intomainfrom
Open
Refactor image path handling in vitePluginNextImage#67valentinpalkovic wants to merge 1 commit intomainfrom
valentinpalkovic wants to merge 1 commit intomainfrom
Conversation
… for improved compatibility
Member
|
@valentinpalkovic Are we planning to release this this week? |
Contributor
|
I've since switched to this which works quite well for us: diff --git a/dist/index.cjs b/dist/index.cjs
index f64334b..d489b60 100644
--- a/dist/index.cjs
+++ b/dist/index.cjs
@@ -4779,13 +4779,41 @@ function vitePluginNextImage(nextConfigResolver, options = {}) {
);
}
if (includePattern2.test(source) && !excludeImporterPattern.test(importer ?? "") && !importer?.startsWith(virtualImage)) {
- const isAbsolute3 = posix.isAbsolute(id);
- const imagePath = importer ? isAbsolute3 ? source : posix.join(posix.dirname(importer), source) : source;
- const pathForFilter = imagePath.replace(postfixRE, "");
+ const importerPath = (importer ?? "").replace(/\\/g, "/");
+ const isAbsolute3 = posix.isAbsolute(source);
+ let imagePath = source;
+ if (importer && !isAbsolute3) {
+ if (source.startsWith(".")) {
+ imagePath = posix.join(posix.dirname(importerPath), source);
+ } else {
+ let resolvedId;
+ try {
+ resolvedId = (await this.resolve(source, importer, {
+ skipSelf: true
+ }))?.id;
+ } catch {
+ resolvedId = undefined;
+ }
+ if (!resolvedId) {
+ try {
+ resolvedId = require4.resolve(source, {
+ paths: [posix.dirname(importerPath), process.cwd()]
+ });
+ } catch {
+ resolvedId = undefined;
+ }
+ }
+ imagePath = resolvedId || source;
+ }
+ }
+ const normalizedImagePath = imagePath
+ .replace(/\\/g, "/")
+ .replace(postfixRE, "");
+ const pathForFilter = normalizedImagePath;
if (!filter(pathForFilter)) {
return null;
}
- return `${virtualImage}?${querystring.encode({ imagePath })}`;
+ return `${virtualImage}?${querystring.encode({ imagePath: normalizedImagePath })}`;
}
if (id === "next/image" && importer !== virtualNextImage) {
return virtualNextImage;
diff --git a/dist/index.js b/dist/index.js
index e039936..a35edc6 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -4746,13 +4746,41 @@ function vitePluginNextImage(nextConfigResolver, options = {}) {
);
}
if (includePattern2.test(source) && !excludeImporterPattern.test(importer ?? "") && !importer?.startsWith(virtualImage)) {
- const isAbsolute3 = posix.isAbsolute(id);
- const imagePath = importer ? isAbsolute3 ? source : posix.join(posix.dirname(importer), source) : source;
- const pathForFilter = imagePath.replace(postfixRE, "");
+ const importerPath = (importer ?? "").replace(/\\/g, "/");
+ const isAbsolute3 = posix.isAbsolute(source);
+ let imagePath = source;
+ if (importer && !isAbsolute3) {
+ if (source.startsWith(".")) {
+ imagePath = posix.join(posix.dirname(importerPath), source);
+ } else {
+ let resolvedId;
+ try {
+ resolvedId = (await this.resolve(source, importer, {
+ skipSelf: true
+ }))?.id;
+ } catch {
+ resolvedId = undefined;
+ }
+ if (!resolvedId) {
+ try {
+ resolvedId = require4.resolve(source, {
+ paths: [posix.dirname(importerPath), process.cwd()]
+ });
+ } catch {
+ resolvedId = undefined;
+ }
+ }
+ imagePath = resolvedId || source;
+ }
+ }
+ const normalizedImagePath = imagePath
+ .replace(/\\/g, "/")
+ .replace(postfixRE, "");
+ const pathForFilter = normalizedImagePath;
if (!filter(pathForFilter)) {
return null;
}
- return `${virtualImage}?${encode({ imagePath })}`;
+ return `${virtualImage}?${encode({ imagePath: normalizedImagePath })}`;
}
if (id === "next/image" && importer !== virtualNextImage) {
return virtualNextImage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes storybookjs/storybook#32355
Closes storybookjs/storybook#32354
Supersedes #57