This theme skeleton is very much a work-in-progress, but it should be enough to get you off the ground using Vite to compile assets in a WordPress project.
With some minor modifications, this skeleton can be used in a plugin, too.
This was slapped together in somewhat of a rush - don't be surprised if best-practices weren't followed.
- PHP 8.0+
- Composer
- Volta - Not a hard requirement, but can be used to manage Node and pnpm versions.
- Node.js LTS (some recent version; I'm using v20.9.0)
- pnpm (some recent version; I'm using v8.15.4. npm probably works too)
- Update
style.css
with a proper theme name and, if applicable, template (parent theme) directive. - Update
functions.php
andsrc/controllers/AssetController.php
with propernamespace
directive. - Update
vite.config.ts
projectPath
variable.- The path should be relative to your project's web-root. For Bedrock projects, this should be
/app/themes/THEME_NAME/
.
- The path should be relative to your project's web-root. For Bedrock projects, this should be
- Update
vite.config.ts
andsrc/controllers/AssetController.php
with entry-points.- By default,
admin.ts
is disabled; uncomment if using assets in admin dashboard. - At the time of writing, the preferred way of loading Vite assets is by outputting markup directly onto the page. See the
admin
/public
-Head
/Footer
-HTML
methods.
- By default,
pnpm i
(yes, that's pnpm, not npm)
-
assets/
- Compiled assets live here. Never put anything here manually. Your changes will likely be lost next time you build. -
resources/
- Asset source-code live here. -
resources/fonts/
- Font files go here. NOTE: See known issue below. -
resources/images/
- Images of any format go here. NOTE: See known issue below. -
resources/scripts/
- Any JavaScript/TypeScript source files live here. The theme comes withmain.ts
, intended for the public-facing 'front-end', andadmin.ts
, intended for the admin 'back-end'. -
resources/static/
- For anything that doesn't fit into the other directories. Assets here are copied to the build dir without any further processing.- Do note that if you include an asset from this directory in a script or stylesheet, the asset will be copied AND processed, resulting in a duplication, which you probably don't want.
- Further note that you can just create another directory in
resources/
that suits your needs.
-
resources/styles/
- Any stylesheet source files live here. The theme comes withmain.scss
, intended for the public-facing 'front-end', andadmin.scss
, intended for the admin 'back-end'.
TLDR: Using relative file paths inside nested sass/scss files fails transformation during build.
Workaround:
Depending on how angry you want your editor to be with you, you have two options that both seem to work in my limited testing:
- Aliases in
vite.config.js
- The@fonts
and@images
aliases reference theresources/fonts
andresources/images
directories respectively. You can use these inside URL strings in any sass/scss file, nested or otherwise. Unfortunately, my editor (PhpStorm) gets angry about this.- Example usage:
background: url('@images/myimage.png');
- Example usage:
- Variables in
main.scss
/admin.scss
- The$fonts
and$images
variables reference theresources/fonts
andresources/images
directories respectively. You concatenate your asset to the variable to create URL strings. Works in nested files. My editor is less angry about this one; it just doesn't know how to resolve the path.- Example usage:
background: ($images + '/myimage.png');
- Example usage:
This theme skeleton is prepped with both options.
Naturally, regarding the issue above, PhpStorm has something else to complain about. It freaks out about import.meta.url
in the alias
object. Perhaps this is solved in newer versions; I'm still on 2020.3 :').
Workaround:
- Open File -> Settings -> Languages & Frameworks -> Typescript
- Add
--module=esnext
to 'Options' field.
Oh man. So many. Search for "TODO" in this directory.