From 357271c29e3f74ccfafb100eacfed3d12ed36272 Mon Sep 17 00:00:00 2001 From: Colin Rotherham Date: Thu, 20 Apr 2023 12:04:36 +0100 Subject: [PATCH] Ensure Dart Sass `file://` sources use relative paths This is consistent with our Node Sass output --- shared/tasks/assets.mjs | 15 +++++++++-- shared/tasks/build/dist.test.mjs | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/shared/tasks/assets.mjs b/shared/tasks/assets.mjs index 20d17063f2..77189533ff 100644 --- a/shared/tasks/assets.mjs +++ b/shared/tasks/assets.mjs @@ -1,5 +1,5 @@ import { mkdir, writeFile } from 'fs/promises' -import { dirname } from 'path' +import { dirname, relative } from 'path' /** * Write asset helper @@ -22,7 +22,18 @@ export async function write (filePath, result) { writeTasks.push(writeFile(filePath, code)) // 2. Write source map (example.js.map) - if (map) { + if (map && 'sources' in map) { + map.sources = map.sources + + /** + * 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://') + ? relative(dirname(filePath), new URL(path).pathname) + : path + ) + writeTasks.push(writeFile(`${filePath}.map`, JSON.stringify(map))) } diff --git a/shared/tasks/build/dist.test.mjs b/shared/tasks/build/dist.test.mjs index b1c1bcb80a..5c967bb51d 100644 --- a/shared/tasks/build/dist.test.mjs +++ b/shared/tasks/build/dist.test.mjs @@ -46,6 +46,21 @@ describe('dist/', () => { }) }) + describe('govuk-frontend-[version].min.css.map', () => { + let filename + let sourcemap + + beforeAll(async () => { + filename = `govuk-frontend-${pkg.version}.min.css.map` + sourcemap = JSON.parse(await readFile(join(paths.dist, filename), 'utf8')) + }) + + it('should contain relative paths to sources', () => { + expect(sourcemap.sources).toContain('../src/govuk/all.scss') + expect(sourcemap.sources).toContain('../src/govuk/core/_govuk-frontend-version.scss') + }) + }) + describe('govuk-frontend-ie8-[version].min.css', () => { let filename let stylesheet @@ -64,6 +79,21 @@ describe('dist/', () => { }) }) + describe('govuk-frontend-ie8-[version].min.css.map', () => { + let filename + let sourcemap + + beforeAll(async () => { + filename = `govuk-frontend-ie8-${pkg.version}.min.css.map` + sourcemap = JSON.parse(await readFile(join(paths.dist, filename), 'utf8')) + }) + + it('should contain relative paths to sources', () => { + expect(sourcemap.sources).toContain('../src/govuk/all-ie8.scss') + expect(sourcemap.sources).toContain('../src/govuk/core/_govuk-frontend-version.scss') + }) + }) + describe('govuk-frontend-[version].min.js', () => { let filename let javascript @@ -86,6 +116,21 @@ describe('dist/', () => { }) }) + describe('govuk-frontend-[version].min.js.map', () => { + let filename + let sourcemap + + beforeAll(async () => { + filename = `govuk-frontend-${pkg.version}.min.js.map` + sourcemap = JSON.parse(await readFile(join(paths.dist, filename), 'utf8')) + }) + + it('should contain relative paths to sources', () => { + expect(sourcemap.sources).toContain('../src/govuk/all.mjs') + expect(sourcemap.sources).toContain('../src/govuk/common/govuk-frontend-version.mjs') + }) + }) + describe('VERSION.txt', () => { let filename let version