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

refactor: unsplit astro:i18n module #9790

Merged
merged 4 commits into from
Jan 24, 2024
Merged

Conversation

lilnasy
Copy link
Contributor

@lilnasy lilnasy commented Jan 23, 2024

Changes

  • The types for the astro:i18n module lived in astro/client.
  • The code as a string in vite-plugin-i18n.ts.
  • This PR combines the two into a full virtual module at astro/virtual-modules/i18n.ts
  • This would allow typescript to catch Fix i18n routing param #9274
  • The config options needed by the helpers functions are provided via a small virtual module "astro-internal:i18n-config".

Testing

Does not affect behavior. Existing tests should pass.

Docs

Does not affect usage.

Copy link

changeset-bot bot commented Jan 23, 2024

🦋 Changeset detected

Latest commit: 70d5a85

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: astro Related to the core `astro` package (scope) label Jan 23, 2024
Comment on lines 23 to 24
if (i18n === undefined) throw new Error("The `astro:i18n` module can not be used without enabling i18n in your Astro config.");
return this.resolve("astro/virtual-modules/i18n.js");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error here is new. Previously, it would throw with a generic "cant resolve" message.

Copy link
Member

Choose a reason for hiding this comment

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

Should we throw an AstroError instead? Or use this.error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure if one of them is better. I can say this will properly display the message in the browser error overlay.

Copy link
Member

@Princesseuh Princesseuh Jan 23, 2024

Choose a reason for hiding this comment

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

I think this should be an AstroError, yes! I didn't fully understand the this.error thing though

Copy link
Member

Choose a reason for hiding this comment

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

I think this should be an AstroError, yes! I didn't fully understand the this.error thing though

In a rollup plugin you can use this.error to throw an error without breaking too much stuff :D

Copy link
Contributor Author

@lilnasy lilnasy Jan 23, 2024

Choose a reason for hiding this comment

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

this.error()

image

throw new Error()

image

throw new AstroError()

image

Copy link
Member

Choose a reason for hiding this comment

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

I'd go with a AstroError so that we can have docs and stuff on it then!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

return code;
}
} else if (locale === path) {
return locale;
}
}
return undefined;
throw new Error("Unreachable");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added errors in a couple of places where implementation did not match the public API.

Copy link
Member

Choose a reason for hiding this comment

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

I suggest inserting a better message here. Even though it's for us, it's good practice to leave a message explaining why this happened, or a possible fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 4abc42e

import type { I18nInternalConfig } from "../i18n/vite-plugin-i18n.js";
export { normalizeTheLocale, toCodes, toPaths } from "../i18n/index.js";
// @ts-expect-error
import config from "astro-internal:i18n-config"
Copy link
Member

Choose a reason for hiding this comment

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

Something that I didn't know about virtual modules an vite. Nice!

Comment on lines +30 to +32
const { defaultLocale, locales, routing, fallback } = i18n!;
const config: I18nInternalConfig = { base, format, site, trailingSlash, defaultLocale, locales, routing, fallback };
return `export default ${JSON.stringify(config)};`;
Copy link
Member

Choose a reason for hiding this comment

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

Definitely more maintainable!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, and this will be the key to removing i18n from the manifest

Comment on lines 23 to 24
if (i18n === undefined) throw new Error("The `astro:i18n` module can not be used without enabling i18n in your Astro config.");
return this.resolve("astro/virtual-modules/i18n.js");
Copy link
Member

Choose a reason for hiding this comment

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

Should we throw an AstroError instead? Or use this.error?

return code;
}
} else if (locale === path) {
return locale;
}
}
return undefined;
throw new Error("Unreachable");
Copy link
Member

Choose a reason for hiding this comment

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

I suggest inserting a better message here. Even though it's for us, it's good practice to leave a message explaining why this happened, or a possible fix.

@github-actions github-actions bot added the pr: docs A PR that includes documentation for review label Jan 23, 2024
@lilnasy lilnasy merged commit 267c5aa into withastro:main Jan 24, 2024
13 checks passed
@lilnasy lilnasy deleted the unsplit-i18n branch January 24, 2024 15:41
@astrobot-houston astrobot-houston mentioned this pull request Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants