Skip to content

Commit 630ae08

Browse files
authored
Merge pull request #292 from oddbird/auto-detect-sass
Attempt to detect installed Sass implementation
2 parents f2c504b + edbfc63 commit 630ae08

File tree

5 files changed

+139
-86
lines changed

5 files changed

+139
-86
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# True Changelog
22

3+
## Unreleased
4+
5+
- FEATURE: If True `sass` option is not specified, True will automatically
6+
attempt to use `embedded-sass`, then `sass`.
7+
[#290](https://github.com/oddbird/true/issues/290)
8+
- INTERNAL: Add `sass` and `sass-embedded` as optional peer-dependencies.
9+
- INTERNAL: Update dependencies
10+
311
## 8.0.0 (02/23/24)
412

513
- FEATURE: Add True `sass` option (`string` or Sass implementation instance,

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ In command line:
3131
npm install --save-dev sass-true
3232
```
3333

34+
True requires Dart Sass v1.45.0 or higher, so install it if you haven't already:
35+
36+
```bash
37+
npm install --save-dev sass-embedded # or `sass`
38+
```
39+
3440
Import in your test directory,
3541
like any other Sass file:
3642

@@ -155,11 +161,11 @@ when upgrading from an older version of True.
155161
npm install --save-dev sass-true
156162
```
157163

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

161167
```bash
162-
npm install --save-dev sass
168+
npm install --save-dev sass-embedded # or `sass`
163169
```
164170

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

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

209215
If True can't parse the CSS output, it'll give you some context lines of CSS as
210216
part of the error message. This context will likely be helpful in understanding

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666
"jest-diff": "^29.7.0",
6767
"lodash": "^4.17.21"
6868
},
69+
"peerDependencies": {
70+
"sass": ">=1.45.0",
71+
"sass-embedded": ">=1.45.0"
72+
},
73+
"peerDependenciesMeta": {
74+
"sass": {
75+
"optional": true
76+
},
77+
"sass-embedded": {
78+
"optional": true
79+
}
80+
},
6981
"devDependencies": {
7082
"@babel/core": "^7.25.2",
7183
"@babel/preset-env": "^7.25.4",

src/index.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ export type Rule = CssCommentAST | CssRuleAST | CssAtRuleAST;
6666

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

69+
const loadSass = function (sassPkg: string) {
70+
try {
71+
// eslint-disable-next-line global-require
72+
return require(sassPkg);
73+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
74+
} catch (err) {
75+
throw new Error(`Cannot find Dart Sass (\`${sassPkg}\`) dependency.`);
76+
}
77+
};
78+
6979
export const runSass = function (
7080
trueOptions: TrueOptions,
7181
src: string,
@@ -103,14 +113,23 @@ export const runSass = function (
103113
let compiler;
104114
if (trueOpts.sass && typeof trueOpts.sass !== 'string') {
105115
compiler = trueOpts.sass;
116+
} else if (typeof trueOpts.sass === 'string') {
117+
compiler = loadSass(trueOpts.sass);
106118
} else {
107-
const sassPkg = trueOpts.sass ?? 'sass';
108119
try {
109-
// eslint-disable-next-line global-require
110-
compiler = require(sassPkg);
120+
// try sass-embedded before sass
121+
compiler = loadSass('sass-embedded');
111122
// eslint-disable-next-line @typescript-eslint/no-unused-vars
112-
} catch (err) {
113-
throw new Error(`Cannot find Dart Sass (\`${sassPkg}\`) dependency.`);
123+
} catch (e1) {
124+
/* istanbul ignore next */
125+
try {
126+
compiler = loadSass('sass');
127+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
128+
} catch (e2) {
129+
throw new Error(
130+
'Cannot find Dart Sass (`sass-embedded` or `sass`) dependency.',
131+
);
132+
}
114133
}
115134
}
116135

0 commit comments

Comments
 (0)