Skip to content

Conversation

@colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Apr 20, 2023

In preparation for v4.6.0 release we noticed Dart Sass writes absolute file paths to source map sources:

You can see we last released source maps (via Node Sass) with relative paths:

dist/govuk-frontend-4.5.0.min.css.map
dist/govuk-frontend-4.5.0.min.js.map

But when building with Dart Sass we see absolute paths to govuk-frontend included:

Node Sass source maps

{
  "sources": [
    "components/accordion/_index.scss"
    "components/back-link/_index.scss",
    "components/breadcrumbs/_index.scss"
  ]
}

Dart Sass source maps

{
  "sources": [
    "file:///Users/username/path/to/govuk-frontend/src/govuk/components/accordion/_index.scss"
    "file:///Users/username/path/to/govuk-frontend/src/govuk/components/back-link/_index.scss",
    "file:///Users/username/path/to/govuk-frontend/src/govuk/components/breadcrumbs/_index.scss"
  ]
}

This PR ensures we keep relative sources paths as we did with Node Sass

@colinrotherham colinrotherham added 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) sass / css labels Apr 20, 2023
@colinrotherham colinrotherham added this to the v4.6.0 milestone Apr 20, 2023
@colinrotherham colinrotherham self-assigned this Apr 20, 2023
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3527 April 20, 2023 11:07 Inactive
@colinrotherham
Copy link
Contributor Author

colinrotherham commented Apr 20, 2023

The GOV.UK Design System website also uses Dart Sass but isn't built locally:

Source maps on the Design System website

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3527 April 20, 2023 11:20 Inactive

// Files to write
/** @type {SourceMap | undefined} */
const map = result.map ? JSON.parse(result.map.toString()) : undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need map.sources a few lines down so parse to SourceMap here

Otherwise across Terser, Rollup and PostCSS we see source maps as:

  1. JavaScript object types that need JSON.stringify()
  2. Class instances with .toJSON() and .toString() methods
  3. Preprepared string type
  4. Missing undefined type

This can be seen via the AssetOutput @typedef in this file

* Make source file:// paths relative (e.g. for Dart Sass)
* {@link https://sass-lang.com/documentation/js-api/interfaces/CompileResult#sourceMap}
*/
.map((path) => path.startsWith('file://')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

PostCSS adds the Dart Sass entry point ../src/all.scss as a relative path

But all the other Dart Sass sources are absolute path file:// URLs

// Add Autoprefixer prefixes to all source '*.scss' files
.flatMap(mapPathTo(['**/*.scss'], ({ dir: requirePath, name }) => [
join(requirePath, `${name}.scss`),
join(requirePath, `${name}.scss.map`) // with source map
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Service teams with source maps enabled will have been inspecting our Autoprefixer-transformed sources, not the original prefix-free *.scss source files

inline: false,
prev: map
if (typeof options.map === 'object') {
options.map.prev = map
Copy link
Contributor Author

@colinrotherham colinrotherham Apr 20, 2023

Choose a reason for hiding this comment

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

PostCSS will automatically discover source maps via sourceMappingURL comments but we prefer to pass them directly from Dart Sass here (like we did previously)

@colinrotherham colinrotherham removed this from the v4.6.0 milestone Apr 20, 2023
We transform Sass sources (Autoprefixer only) but didn’t source map the changes
Using types from Terser for the Source Map Revision 3 spec
This is consistent with our Node Sass output
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3527 April 20, 2023 13:42 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

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

I think that did it and it's all very localised! Thanks for adding tests as well 🙌🏻

Screenshot 2023-04-21 at 09 46 00

Screenshot 2023-04-21 at 09 49 00

@colinrotherham colinrotherham merged commit 8ccff69 into main Apr 21, 2023
@colinrotherham colinrotherham deleted the source-map-sources branch May 4, 2023 11:49
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request Feb 20, 2025
colinrotherham added a commit to nhsuk/nhsuk-frontend that referenced this pull request Feb 20, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 21, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 24, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 24, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 24, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 25, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 25, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 25, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 26, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 26, 2025
colinrotherham added a commit to ministryofjustice/moj-frontend that referenced this pull request Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working the way it should (including incorrect wording in documentation) sass / css

Projects

Development

Successfully merging this pull request may close these issues.

4 participants