Skip to content

Commit

Permalink
Merge pull request #292 from oddbird/auto-detect-sass
Browse files Browse the repository at this point in the history
Attempt to detect installed Sass implementation
  • Loading branch information
jgerigmeyer authored Oct 2, 2024
2 parents f2c504b + edbfc63 commit 630ae08
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 86 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# True Changelog

## Unreleased

- FEATURE: If True `sass` option is not specified, True will automatically
attempt to use `embedded-sass`, then `sass`.
[#290](https://github.com/oddbird/true/issues/290)
- INTERNAL: Add `sass` and `sass-embedded` as optional peer-dependencies.
- INTERNAL: Update dependencies

## 8.0.0 (02/23/24)

- FEATURE: Add True `sass` option (`string` or Sass implementation instance,
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ In command line:
npm install --save-dev sass-true
```

True requires Dart Sass v1.45.0 or higher, so install it if you haven't already:

```bash
npm install --save-dev sass-embedded # or `sass`
```

Import in your test directory,
like any other Sass file:

Expand Down Expand Up @@ -155,11 +161,11 @@ when upgrading from an older version of True.
npm install --save-dev sass-true
```

2. [Optional] Install Dart Sass (`sass` or `sass-embedded`), if not already
2. [Optional] Install Dart Sass (`sass-embedded` or `sass`), if not already
installed.

```bash
npm install --save-dev sass
npm install --save-dev sass-embedded # or `sass`
```

3. Write some Sass tests in `test/test.scss` (see above).
Expand Down Expand Up @@ -203,8 +209,8 @@ should be usable in the same way: just pass your test runner's `describe` and

The `sass` option is an optional string name of a Dart Sass implementation
installed in the current environment (e.g. `'embedded-sass'` or `'sass'`), or a
Dart Sass implementation instance itself. If none is provided, this defaults to
`'sass'`.
Dart Sass implementation instance itself. If none is provided, True will attempt
to detect which implementation is available, starting with `sass-embedded`.

If True can't parse the CSS output, it'll give you some context lines of CSS as
part of the error message. This context will likely be helpful in understanding
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
"jest-diff": "^29.7.0",
"lodash": "^4.17.21"
},
"peerDependencies": {
"sass": ">=1.45.0",
"sass-embedded": ">=1.45.0"
},
"peerDependenciesMeta": {
"sass": {
"optional": true
},
"sass-embedded": {
"optional": true
}
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
Expand Down
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export type Rule = CssCommentAST | CssRuleAST | CssAtRuleAST;

export type Parser = (rule: Rule, ctx: Context) => Parser;

const loadSass = function (sassPkg: string) {
try {
// eslint-disable-next-line global-require
return require(sassPkg);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
throw new Error(`Cannot find Dart Sass (\`${sassPkg}\`) dependency.`);
}
};

export const runSass = function (
trueOptions: TrueOptions,
src: string,
Expand Down Expand Up @@ -103,14 +113,23 @@ export const runSass = function (
let compiler;
if (trueOpts.sass && typeof trueOpts.sass !== 'string') {
compiler = trueOpts.sass;
} else if (typeof trueOpts.sass === 'string') {
compiler = loadSass(trueOpts.sass);
} else {
const sassPkg = trueOpts.sass ?? 'sass';
try {
// eslint-disable-next-line global-require
compiler = require(sassPkg);
// try sass-embedded before sass
compiler = loadSass('sass-embedded');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
throw new Error(`Cannot find Dart Sass (\`${sassPkg}\`) dependency.`);
} catch (e1) {
/* istanbul ignore next */
try {
compiler = loadSass('sass');
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e2) {
throw new Error(
'Cannot find Dart Sass (`sass-embedded` or `sass`) dependency.',
);
}
}
}

Expand Down
Loading

0 comments on commit 630ae08

Please sign in to comment.