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

Basic Vite setup #4

Merged
merged 3 commits into from
May 14, 2024
Merged

Basic Vite setup #4

merged 3 commits into from
May 14, 2024

Conversation

joshuapease
Copy link
Contributor

@joshuapease joshuapease commented May 14, 2024

Overview

What this PR does.

  • Bare bones Vite config
  • DDEV setup for ports & daemons
  • Working webfonts
  • Working JS
  • Avoids flash of unstyled content while in dev mode.

What this PR (intentionally) doesn't do

  • Set up Tailwind
  • Set up Prettier & other coding standards
  • Static asset copying (think icon SVGs, will address in future PR)

How to test

Create a directory for your new Craft project

mkdir my-craft-project

# Then move into your directory
cd my-craft-project

Run the following composer create-project command.

It uses this branch as the starter project.

docker run --rm -it -v "$PWD":/app -v ${COMPOSER_HOME:-$HOME/.composer}:/tmp composer create-project viget/craft-site-starter=dev-jp/vite-setup ./ --ignore-platform-reqs

After completing the setup steps (it prompts for your project's name), run the following commands.

👇 These will gradually become more automated as this project progresses.

# Start DDEV
ddev start

# Install Craft
ddev craft install

# Install Vite plugin
ddev craft plugin/install vite

# Launch the site!
ddev launch

What you should see:

  • A styled page
  • Webfonts being loaded in the network tab.
  • A console log (and no errors)

ℹ️ You may need a ddev restart to get rid of 404s around webfonts. The Vite config is looking for the PRIMARY_SITE_URL environment variable. It might not be fully loaded by DDEV.

CleanShot 2024-05-14 at 09 34 26@2x

@joshuapease joshuapease marked this pull request as draft May 14, 2024 00:18
Comment on lines +11 to +16
$host = Craft::$app->getRequest()->getIsConsoleRequest()
? App::env('PRIMARY_SITE_URL')
: Craft::$app->getRequest()->getHostInfo();
$httpPort = 3000;
$httpsPort = 3001;
$devServerPort = str_starts_with($host, 'https') ? $httpsPort : $httpPort;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For a while I was having difficulty getting https working with DDEV (I've got it working great now).

Figured it wasn't too much extra code to accommodate other's who might hit that issue.

font-family: 'Source Sans Pro';
font-style: normal;
font-weight: 300;
src: url('../fonts/sourcesanspro-light-webfont.woff2') format('woff2');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Vite can automatically copy & hash filenames for fonts. It just has to be a relative path from the css file to the font file.

Comment on lines +10 to +13
<link rel="preload" href="{{ craft.vite.asset("src/fonts/sourcesanspro-bold-webfont.woff2") }}" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="{{ craft.vite.asset("src/fonts/sourcesanspro-light-webfont.woff2") }}" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="{{ craft.vite.asset("src/fonts/sourcesanspro-regular-webfont.woff2") }}" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="{{ craft.vite.asset("src/fonts/sourcesanspro-semibold-webfont.woff2") }}" as="font" type="font/woff2" crossorigin>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Figured this would be a nice example/reminder to preload your fonts.

host: '0.0.0.0',
strictPort: true,
port: HTTP_PORT,
origin: env.PRIMARY_SITE_URL + ':' + originPort,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line is also needed to get webfonts, images, etc to work properly in dev mode.

You have to tell Vite the precise URL to use for any files referenced in your CSS & JS files.

@joshuapease joshuapease marked this pull request as ready for review May 14, 2024 17:01
Copy link
Contributor

@maxfenton maxfenton left a comment

Choose a reason for hiding this comment

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

Great work!

@joshuapease joshuapease merged commit bfd0c2c into 5.x May 14, 2024
@joshuapease joshuapease deleted the jp/vite-setup branch May 14, 2024 23:39
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.

4 participants