Skip to content

Commit

Permalink
Attempt to detect installed Sass implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgerigmeyer committed Oct 1, 2024
1 parent a23ea1f commit f637a85
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,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 +203,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
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8771,6 +8771,14 @@ __metadata:
stylelint: "npm:^16.9.0"
stylelint-config-standard-scss: "npm:^13.1.0"
typescript: "npm:^5.5.4"
peerDependencies:
sass: ">=1.45.0"
sass-embedded: ">=1.45.0"
peerDependenciesMeta:
sass:
optional: true
sass-embedded:
optional: true
languageName: unknown
linkType: soft

Expand Down

0 comments on commit f637a85

Please sign in to comment.