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

Build/bundle assets and CSS #1786

Merged
merged 25 commits into from Nov 11, 2021
Merged

Build/bundle assets and CSS #1786

merged 25 commits into from Nov 11, 2021

Conversation

matthewp
Copy link
Contributor

Changes

  • This updates the build so that assets and CSS are also included.
  • This is achieved by creating two new rollup plugins: one handles HTML scanning and updating, and the other handles building CSS.

Testing

  • CSS Bundling test back in.
  • Asset test back in.

Docs

N/A

@matthewp matthewp requested a review from a team as a code owner November 10, 2021 16:37
@changeset-bot
Copy link

changeset-bot bot commented Nov 10, 2021

🦋 Changeset detected

Latest commit: a1c7c48

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

This PR includes changesets to release 1 package
Name Type
astro Patch

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

@jasikpark
Copy link
Contributor

jasikpark commented Nov 10, 2021

🤔 btw when trying to build www on this branch (cb1e874) I get this output:

image

Is there a way I can get better introspection into what's going on?

@natemoo-re natemoo-re added this to the v0.21 milestone Nov 10, 2021
@jasikpark
Copy link
Contributor

     ✔ is only called once during build

READING /D:/a/astro/astro/packages/astro/test/fixtures/astro-global/src/images/penguin.png
READING /D:/a/astro/astro/packages/astro/test/fixtures/astro-global/src/components/nested/images/penguin.png
[@astro/rollup-plugin-build] ENOENT: no such file or directory, open 'D:\D:\a\astro\astro\packages\astro\test\fixtures\astro-global\src\images\penguin.png'

(node:4508) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 5)
  Astro.*
    2) "before all" hook for "Astro.request.url"

that's pretty weird 🤔 (https://github.com/snowpackjs/astro/runs/4169598680?check_suite_focus=true#step:7:236)

@matthewp
Copy link
Contributor Author

@jasikpark looks like www needs to be updated before it can be built, it is using some imports that do not work with Vite.

@jasikpark
Copy link
Contributor

jasikpark commented Nov 10, 2021

@jasikpark looks like www needs to be updated before it can be built, it is using some imports that do not work with Vite.

right, when testing on this branch + the changes in #1775 though, I still get:

image

Maybe that's not an assets thing though 🤷‍♂️

@matthewp
Copy link
Contributor Author

@jasikpark I just rebased with main and don't see that. Maybe try pulling this branch again, running yarn, then yarn build and letting me know.

@@ -66,7 +66,7 @@
"@babel/traverse": "^7.15.4",
"@proload/core": "^0.2.1",
"@proload/plugin-tsm": "^0.1.0",
"@web/rollup-plugin-html": "^1.10.1",
Copy link
Member

Choose a reason for hiding this comment

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

🎉

@jasikpark
Copy link
Contributor

@jasikpark I just rebased with main and don't see that. Maybe try pulling this branch again, running yarn, then yarn build and letting me know.

I still have the same problem... maybe it's somehow just a problem in Gitpod??? Want to enable the www smoketest in ci.yml` and see if that changes anything?

@@ -71,3 +71,7 @@ export function resolveDependency(dep: string, astroConfig: AstroConfig) {
// For Windows compat, we need a fully resolved `file://` URL string
return pathToFileURL(resolved).toString();
}

export function viteifyPath(pathname: string): string {
Copy link
Member

Choose a reason for hiding this comment

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

Ha I like the name

@@ -0,0 +1,10 @@
import type { ComponentPreload } from '../ssr/index';
import type { RouteData } from '../../@types/astro-core';
Copy link
Member

Choose a reason for hiding this comment

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

What’s the reason for this type file living here and not @types?

input,
extractAssets: false,
}) as any, // "any" needed for CI; also we don’t need typedefs for this anyway
rollupPluginAstroBuildHTML({
Copy link
Member

Choose a reason for hiding this comment

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

💯

const [renderers, mod] = await preload(ssrOpts);
return render(renderers, mod, ssrOpts);
} catch (e: unknown) {
await errorHandler(e, ssrOpts.viteServer, ssrOpts.filePath);
Copy link
Member

Choose a reason for hiding this comment

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

Nice cleanup!

packages/astro/src/vite-plugin-build-css/index.ts Outdated Show resolved Hide resolved

const ASTRO_PAGE_STYLE_PREFIX = '@astro-page-all-styles';

const isCSSRequest = (request: string) => STYLE_EXTENSIONS.has(path.extname(request));
Copy link
Member

Choose a reason for hiding this comment

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

Vite may sometimes append query params to URLs, e.g. styles.css?direct. Probably safest to make sure those are being handled as well.

@@ -48,6 +48,7 @@ export interface TopLevelAstro {

export interface SSRMetadata {
renderers: Renderer[];
pathname: string;
Copy link
Member

Choose a reason for hiding this comment

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

Where is pathname used? Can this be a URL or no?

allPages[route.component] = {
route,
paths: [route.pathname],
preload: await ssrPreload({
Copy link
Member

Choose a reason for hiding this comment

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

I like the preload approach. Clever

});
renderedPageMap.set(id, html);

const document = parse5.parse(html, {
Copy link
Member

Choose a reason for hiding this comment

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

Just a sidenote: if we like parse5 better we should probably replace htmlparser2 with this (I didn’t profile parse5; I just went with htmlparser2 because it’s fast, easy-to-use, and actively maintained)

Copy link
Member

Choose a reason for hiding this comment

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

Was going to be my comment as well. I'm fine with either, but we should only use one.

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.

Looking good! Drew covered all of my comments ☺️

if (!PREPROCESSOR_EXTENSIONS.has(lang)) {
export async function transformWithVite({ value, attrs, transformHook, id, ssr, force }: TransformWithViteOptions): Promise<vite.TransformResult | null> {
let lang = (`.${attrs.lang}` || '').toLowerCase(); // add leading "."; don’t be case-sensitive
if (!force && !PREPROCESSOR_EXTENSIONS.has(lang)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that this is not a needed change any more

matthewp and others added 2 commits November 10, 2021 16:11
Co-authored-by: Drew Powers <1369770+drwpow@users.noreply.github.com>
@matthewp matthewp merged commit fd52bce into main Nov 11, 2021
@matthewp matthewp deleted the build-assets branch November 11, 2021 13:44
@hans-d
Copy link

hans-d commented Nov 11, 2021

SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Bundling CSS

* Current progress of building assets

* New build progress

* Its finally working

* Force css to go through the build

* Prettier filenames

* Split into separate CSS and HTML plugins

* Always have at least one input

* Bring back in sitemaps + output

* Bring back srcset support

* Bundle CSS

* Bring back minify

* Update dynamic tests

* Update remaining tests

* Linting

* Fix remaining broken test

* Use fs directly

* Adding a changeset

* Use path.posix

* Debugging windows

* More debugging

* Pass URLs into readFile

* Remove some debugging stuff

* Remove force flag from transformWithVite

* Update packages/astro/src/vite-plugin-build-css/index.ts

Co-authored-by: Drew Powers <1369770+drwpow@users.noreply.github.com>

Co-authored-by: Drew Powers <1369770+drwpow@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Assets in src/ do not get build in astro@next
5 participants