Skip to content

Commit

Permalink
Use Dart Sass modern compilation API
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 2, 2023
1 parent 46c8e1c commit df6f181
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 39 deletions.
28 changes: 7 additions & 21 deletions lib/jest-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cheerio = require('cheerio')
const { configureAxe } = require('jest-axe')
const nunjucks = require('nunjucks')
const { outdent } = require('outdent')
const sass = require('sass-embedded')
const { compileAsync, compileStringAsync } = require('sass-embedded')

const { paths } = require('../config/index.js')

Expand Down Expand Up @@ -130,43 +130,29 @@ async function getExamples (componentName) {
*
* @param {string} path - Path to Sass file
* @param {import('sass-embedded').Options} [options] - Options to pass to Sass
* @returns {Promise<import('sass-embedded').CompileResult>} Sass compile result
*/
async function compileSassFile (path, options = {}) {
const { css, map, stats } = sass.renderSync({
file: path,
includePaths: sassPaths,
outputStyle: 'expanded',
return compileAsync(path, {
loadPaths: sassPaths,
quietDeps: true,
...options
})

return {
css: css?.toString().trim(),
map: map?.toString().trim(),
stats
}
}

/**
* Render Sass from string
*
* @param {string} source - Sass source string
* @param {import('sass-embedded').Options} [options] - Options to pass to Sass
* @returns {Promise<import('sass-embedded').CompileResult>} Sass compile result
*/
async function compileSassString (source, options = {}) {
const { css, map, stats } = sass.renderSync({
data: source,
includePaths: sassPaths,
outputStyle: 'expanded',
return compileStringAsync(source, {
loadPaths: sassPaths,
quietDeps: true,
...options
})

return {
css: css?.toString().trim(),
map: map?.toString().trim(),
stats
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/govuk/components/components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Components', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand All @@ -32,7 +32,7 @@ describe('Components', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/govuk/helpers/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('The helpers layer', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/govuk/objects/objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('The objects layer', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/govuk/settings/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('The settings layer', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/govuk/tools/tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('The tools layer', () => {

return expect(compileSassFile(file)).resolves.toMatchObject({
css: expect.any(String),
stats: expect.any(Object)
loadedUrls: expect.arrayContaining([expect.any(URL)])
})
})

Expand Down
18 changes: 6 additions & 12 deletions tasks/compile-stylesheets.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { readFile } from 'fs/promises'
import { join, parse } from 'path'
import { promisify } from 'util'

import PluginError from 'plugin-error'
import postcss from 'postcss'
// eslint-disable-next-line import/default
import postcssrc from 'postcss-load-config'
import { render } from 'sass-embedded'
import { compileAsync } from 'sass-embedded'

import { paths, pkg } from '../config/index.js'
import { getListing } from '../lib/file-helper.js'

import { writeAsset } from './compile-assets.mjs'
import { destination, isDist, isPackage, isPublic } from './task-arguments.mjs'

// Sass renderer
const sassRender = promisify(render)

/**
* Compile Sass to CSS task
*
Expand Down Expand Up @@ -62,28 +58,26 @@ export async function compileStylesheet ([modulePath, { srcPath, destPath }]) {

// Render Sass
if (!isPackage) {
({ css, map } = await sassRender({
file: moduleSrcPath,
outFile: moduleDestPath,

({ css, sourceMap: map } = await compileAsync(moduleSrcPath, {
// Turn off dependency warnings
quietDeps: true,

// Enable source maps
sourceMap: true,
sourceMapContents: true,
sourceMapIncludeSources: true,

// Resolve @imports via
includePaths: [
loadPaths: [
join(paths.node_modules, 'govuk_frontend_toolkit/stylesheets'),
paths.node_modules
]
}))

// Pass source maps to PostCSS
options.map = {
annotation: true,
inline: false,
prev: map.toString()
prev: map
}
}

Expand Down

0 comments on commit df6f181

Please sign in to comment.