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

Astro Integration System #2820

Merged
merged 9 commits into from
Mar 18, 2022
Merged

Astro Integration System #2820

merged 9 commits into from
Mar 18, 2022

Conversation

FredKSchott
Copy link
Member

@FredKSchott FredKSchott commented Mar 17, 2022

Changes

How to Review

This is all interconnected to the point that I couldn't easily break the PR into smaller, individual, functional PRs. But, I was able to break down this PR into 3 different commits for a bit easier review:

  1. Review updated usage in examples
  2. Review initial integrations implementations
  3. Update tests
  4. Review integration support in Astro

Testing

  • Updated.

Docs

@changeset-bot
Copy link

changeset-bot bot commented Mar 17, 2022

⚠️ No Changeset found

Latest commit: 659ad8d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope) labels Mar 17, 2022
export default function createPlugin(): AstroIntegration {
let config: AstroConfig;
return {
name: '@astrojs/sitemap',
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like this integration, glad to see this kind of thing can be external to the build now.

}
}

// TODO: Add support for `injectElement()` for full HTML element injection, not just scripts.
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't something like that be part of rendering and not part of the build? Otherwise it won't happen in SSR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure I know what you mean, any links to a better location in the codebase for this TODO would be appreciated!

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed! This is part of my concern with a single hook for "build" without a "compile" vs "render" discrepancy. I guess we're assuming that "build" implies static build.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll move this TODO somewhere else that doesn't imply it will be handled by static build. And yes, I'm +1 on that. Would love to chat afterwards about the best path forward to implement with SSR in mind.

@@ -69,10 +75,19 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO
children: '',
});
scripts.add({
props: { type: 'module', src: new URL('../../../runtime/client/hmr.js', import.meta.url).pathname },
props: { type: 'module', src: '/@id/astro/client/hmr.js' },
Copy link
Contributor

Choose a reason for hiding this comment

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

Wish I knew about this magic /@id sooner.

Copy link
Member Author

Choose a reason for hiding this comment

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

Vite always keeps you guessing :)

Copy link
Contributor

Choose a reason for hiding this comment

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

THIS WORKS WHAT THIS IS GAME CHANGING

// We have a need to pass context based on configured state,
// that is different from the user-exposed configuration.
// TODO: Create an AstroConfig class to manage this, long-term.
_ctx: {
Copy link
Contributor

@matthewp matthewp Mar 17, 2022

Choose a reason for hiding this comment

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

I don't know if I will die on this hill (I might) but I think mixing configuration and state is a mistake that will be hard to unravel. Probably the right thing to do is create a class/object that wraps the config and then pass that everywhere (I believe that is what ResolvedConfig is for in Vite).

But that would involve updating a crazy amount of code, so I understand not wanting to do that.

How about a compromise where this context object is kept in a global WeakMap?

interface Context { ... }

const contexts = new WeakMap<AstroConfig, Context>();
const getContext = (config: AstroConfig) => contexts.get(config);

Copy link
Member

Choose a reason for hiding this comment

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

I agree with this. Not sure the best solution but I don't love shoving a stateful object in here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, I tried to leave the TODO to capture that need here. I agree, a context object isn't really a property of your config. If anything, it's the opposite, so maybe AstroContext becomes the class that manages your config and other stuff.

If we're talking whats in scope for this PR and the short-term, I think I prefer the _ctx hack vs. maintaining a weakmap separately. It's not a hill that I'll die on either, but I don't see that as any easier/harder to unravel than _ctx.

The compromise that I'd suggest instead would be to fast-track that larger Class/Object refactor instead of letting this sit for to long post-merge. If we could commit to that (maybe @bholmesdev could take a look next week?) then I think _ctx is the better short-term path.

@matthewp
Copy link
Contributor

GitHub is being weird and stopped letting me add comments inside of code.

Aside from the comments I left the biggest thing I was unsure about from reading this is how the scripts end up in the page. It looks like this.emitFile is not saving the reference to the hash in order to get the filename. Hoisted scripts have to do this kind of thing, so maybe that's something to use as reference.

Lastly, I really liked the idea of splitting the PR into more digestable chunks, that helped a lot with reviewing.

examples/minimal/astro.config.mjs Outdated Show resolved Hide resolved
examples/with-tailwindcss/astro.config.mjs Outdated Show resolved Hide resolved
packages/integrations/README.md Outdated Show resolved Hide resolved
packages/integrations/partytown/src/index.ts Show resolved Hide resolved
@@ -36,7 +71,19 @@ export const AstroConfigSchema = z.object({
.optional()
.default('./dist')
.transform((val) => new URL(val)),
renderers: z.array(z.string()).optional().default(['@astrojs/renderer-svelte', '@astrojs/renderer-vue', '@astrojs/renderer-react', '@astrojs/renderer-preact']),
integrations: z.array(z.any()).default([]),
Copy link
Member

Choose a reason for hiding this comment

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

Glad to see this defaults to none!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea... are we okay with this? It makes it a more disruptive change if so. I might ping you all on Discord to confirm that this is what we want, since we don't need to make this breaking change now.

But like all things, maybe best to just rip the bandaid off now

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wow, I had no clue it defaulted to all frameworks previously. I definitely think we'll move renderers out of the core and into separate packages down the road. The requirement to install every supported UI framework makes npm init astro + plain npm (no yarn or pnpm) a pain to use. As a stepping stone to this, I'd say we should default to none for 1.0.

packages/astro/src/vite-plugin-astro/index.ts Outdated Show resolved Hide resolved
packages/astro/src/vite-plugin-astro/index.ts Outdated Show resolved Hide resolved
packages/astro/src/vite-plugin-scripts/index.ts Outdated Show resolved Hide resolved
@FredKSchott
Copy link
Member Author

Lastly, I really liked the idea of splitting the PR into more digestable chunks, that helped a lot with reviewing.

Thanks, it took some time so that means a lot! I'll usually try to break something this big into entirely standalone smaller PRs. But in this case I just couldn't find a good way to do it, given how interconnected it all is.

@FredKSchott FredKSchott force-pushed the new-integrations-system branch from 1b550c2 to 090b2c4 Compare March 17, 2022 20:14
@github-actions github-actions bot added pkg: create-astro Related to the `create-astro` package (scope) test labels Mar 18, 2022
@FredKSchott FredKSchott force-pushed the new-integrations-system branch 2 times, most recently from 7a710fb to 9246346 Compare March 18, 2022 04:40
@github-actions github-actions bot added the 🚨 action Modifies GitHub Actions label Mar 18, 2022
@FredKSchott FredKSchott force-pushed the new-integrations-system branch 8 times, most recently from 74aaa1f to aa961a5 Compare March 18, 2022 07:17
@FredKSchott FredKSchott force-pushed the new-integrations-system branch 2 times, most recently from 7be3e88 to 7c2edbb Compare March 18, 2022 07:43

let SUFFIX = '';
// Add HMR handling in dev mode.
if (!resolvedConfig.isProduction) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed? Pretty sure we are already handling this elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh this, was already happening above, weird.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm happy to remove but yea, would love to keep moving parts to a minimum in this PR

@FredKSchott FredKSchott force-pushed the new-integrations-system branch from a4d5bab to f94af7a Compare March 18, 2022 16:50
Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

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

lgtm

: // Otherwise, you are following an import in the module import tree.
// You are safe to use getModuleById() here because Vite has already
// resolved the correct `id` for you, by creating the import you followed here.
new Set([viteServer.moduleGraph.getModuleById(id)!]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, nice find! Glad we can default to the ID-based system where possible

Copy link
Member Author

Choose a reason for hiding this comment

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

Doh, I meant to call this out to you! The reason for the change was that we needed to support virtual imports that contain CSS but don't exist on file, like the ones added by our Tailwind hooks.

I'd love your help confirming this change is good, it made sense to me and I tested it more than any other part of this PR probably.

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

A few small comments focused on the examples which I'd love to see fixed. Once those are addressed I approve!

examples/framework-alpine/astro.config.mjs Outdated Show resolved Hide resolved
examples/framework-lit/astro.config.mjs Outdated Show resolved Hide resolved
examples/integrations-playground/README.md Outdated Show resolved Hide resolved
examples/integrations-playground/src/components/Button.jsx Outdated Show resolved Hide resolved
examples/integrations-playground/src/pages/foo.astro Outdated Show resolved Hide resolved
examples/integrations-playground/src/pages/index.astro Outdated Show resolved Hide resolved
examples/non-html-pages/astro.config.mjs Outdated Show resolved Hide resolved
examples/with-tailwindcss/tailwind.config.js Show resolved Hide resolved
packages/astro/src/core/config.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@bholmesdev bholmesdev left a comment

Choose a reason for hiding this comment

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

Flagged remaining integrations array examples. Non-blocking for me though ✅

examples/env-vars/astro.config.mjs Outdated Show resolved Hide resolved
examples/non-html-pages/astro.config.mjs Outdated Show resolved Hide resolved
examples/starter/astro.config.mjs Outdated Show resolved Hide resolved
examples/with-markdown-plugins/astro.config.mjs Outdated Show resolved Hide resolved
examples/with-markdown-shiki/astro.config.mjs Outdated Show resolved Hide resolved
examples/framework-alpine/astro.config.mjs Show resolved Hide resolved
examples/with-vite-plugin-pwa/astro.config.mjs Outdated Show resolved Hide resolved
@FredKSchott FredKSchott merged commit 6386c14 into main Mar 18, 2022
@FredKSchott FredKSchott deleted the new-integrations-system branch March 18, 2022 22:35
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* update examples

* add initial integrations

* update tests

* update astro

* update ci

* get final tests working

* update injectelement todo

* update ben code review

* respond to final code review feedback
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* update examples

* add initial integrations

* update tests

* update astro

* update ci

* get final tests working

* update injectelement todo

* update ben code review

* respond to final code review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 action Modifies GitHub Actions pkg: astro Related to the core `astro` package (scope) pkg: create-astro Related to the `create-astro` package (scope) pkg: example Related to an example package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants