Skip to content

Commit

Permalink
Ensure Dart Sass file:// sources use relative paths
Browse files Browse the repository at this point in the history
This is consistent with our Node Sass output
  • Loading branch information
colinrotherham committed Apr 20, 2023
1 parent cbc2a20 commit 945b204
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
15 changes: 13 additions & 2 deletions shared/tasks/assets.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mkdir, writeFile } from 'fs/promises'
import { dirname } from 'path'
import { dirname, relative } from 'path'

/**
* Write asset helper
Expand All @@ -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)))
}

Expand Down
45 changes: 45 additions & 0 deletions shared/tasks/build/dist.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 945b204

Please sign in to comment.