From c7150e05f8a15204f02c15d75dc5b640fe0decb8 Mon Sep 17 00:00:00 2001 From: David Biddle Date: Wed, 15 Jan 2025 15:48:43 +0000 Subject: [PATCH] Load govuk-frontend Sass with NodePackageImporter There's [a bug in the way Vite's inbuilt sass importing works](https://github.com/vitejs/vite/issues/19196) that means some of our assets don't build properly if we import the govuk-frontend styles using the @GOVUK alias. We can get around this by using Sass's builtin NodePackageImporter to import the govuk-frontend styles. This bypasses the Vite import code that causes the issue. It's also better for a couple of other reasons. It makes it clear that this import is coming from an npm package (which was obscured slightly when we were using the alias). It also makes us resilient to structure changes in the govuk-frontend package (NodePackageImporter uses the 'sass' field in govuk-frontend's package.json to determine the file to import, so we no longer need to point to a specific file which could change). --- app/frontend/entrypoints/application.scss | 2 +- vite.config.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/frontend/entrypoints/application.scss b/app/frontend/entrypoints/application.scss index 8757bebfb..246685eea 100644 --- a/app/frontend/entrypoints/application.scss +++ b/app/frontend/entrypoints/application.scss @@ -2,7 +2,7 @@ $govuk-images-path: "@govuk/assets/images/"; $govuk-fonts-path: "@govuk/assets/fonts/"; $govuk-global-styles: true; -@import "@govuk/all"; +@import "pkg:govuk-frontend"; @import "dfe-autocomplete/src/dfe-autocomplete"; @import "../styles/app_form_states"; @import "../styles/app_memorandum_of_understanding"; diff --git a/vite.config.js b/vite.config.js index be5d2ade2..c330afed1 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,6 +3,7 @@ import { defineConfig } from 'vite' import RubyPlugin from 'vite-plugin-ruby' import * as path from 'node:path' +import { NodePackageImporter } from 'sass' export default defineConfig({ plugins: [RubyPlugin()], @@ -11,7 +12,7 @@ export default defineConfig({ preprocessorOptions: { scss: { api: 'modern', - loadPaths: ['./node_modules/govuk-frontend/'], + importers: [new NodePackageImporter()], quietDeps: true }, devSourcemaps: true