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(image): allow usage of image from any directory #5932

Merged
merged 1 commit into from
Jan 30, 2023

Conversation

rasendubi
Copy link
Contributor

@rasendubi rasendubi commented Jan 22, 2023

Currently, @astrojs/image allows importing images from srcDir only. Importing images from outside srcDir fails miserably in dev mode and produces incorrect src.

This happens because path.relative(fileURLToPath(config.srcDir), id) resolves to "../something" and when joined with '/@astroimage' cancels it (join('/@astroimage', '../../something') => '/something').

Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always export absolute path to the target file. This allows images to be places anywhere on the filesystem—even outside project root.

Testing

Added tests for @astrojs/image.

Docs

This lifts the restriction of only placing images in src or public directory. Images can be imported from anywhere—even outside project root.

/cc @withastro/maintainers-docs for feedback!

@changeset-bot
Copy link

changeset-bot bot commented Jan 22, 2023

🦋 Changeset detected

Latest commit: 71e24c4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Jan 22, 2023
Currently, @astrojs/image allows *importing* images from srcDir
only. Importing images from outside srcDir fails miserably *in dev
mode* and produces incorrect src.

This happens because `path.relative(fileURLToPath(config.srcDir), id)`
resolves to "../something" and when joined with '/@astroimage' cancels
it out (`join('/@astroimage', '../../something')` => `'/something'`).

Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always
export absolute path to the target file.
@rishi-raj-jain
Copy link
Contributor

This is great!

Comment on lines 66 to 78
} else {
const relId = path.relative(fileURLToPath(config.srcDir), id);

meta.src = join('/@astroimage', relId);

// Windows compat
meta.src = slash(meta.src);
meta.src = '/@astroimage' + url.pathname;
}

return `export default ${JSON.stringify(meta)}`;
},
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url?.startsWith('/@astroimage/')) {
const [, id] = req.url.split('/@astroimage/');
// Reconstructing URL to get rid of query parameters in path
const url = new URL(req.url.slice('/@astroimage'.length), 'file:');

const url = new URL(id, config.srcDir);
const file = await fs.readFile(url);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are reviewing a PR, this is the core and the only meaningful change—the rest is just tests

@rasendubi
Copy link
Contributor Author

rasendubi commented Jan 27, 2023

What's my next step to get this PR moving?

cc @matthewp @natemoo-re @andersk (btw, congrats on 2.0 launch!)

@natemoo-re
Copy link
Member

Thanks for the PR @rasendubi! I'm going to pull in @Princesseuh who is doing some cleanup on the image integration. This seems like a reasonable change!

@matthewp matthewp merged commit b3e6599 into withastro:main Jan 30, 2023
@astrobot-houston astrobot-houston mentioned this pull request Jan 30, 2023
matthewp pushed a commit that referenced this pull request Feb 3, 2023
Currently, @astrojs/image allows *importing* images from srcDir
only. Importing images from outside srcDir fails miserably *in dev
mode* and produces incorrect src.

This happens because `path.relative(fileURLToPath(config.srcDir), id)`
resolves to "../something" and when joined with '/@astroimage' cancels
it out (`join('/@astroimage', '../../something')` => `'/something'`).

Rework /@astroimage URL scheme to be similar to "/@fs/" scheme—always
export absolute path to the target file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants