Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to detect installed Sass implementation #292

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why ">=1.45.0". I think it's the minimum supported version? Should we add that to the README?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's the minimum version that supports the "modern" API (which is what True uses). Added a note in 51c9e41

},
"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