Skip to content

Commit bb4e18a

Browse files
committed
chore: revert changes and update readme
1 parent 2a2f072 commit bb4e18a

13 files changed

+1722
-438
lines changed

README.md

+78
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,84 @@ module.exports = {
239239
};
240240
```
241241

242+
Note that when using `sass` (`Dart Sass`), **synchronous compilation is twice as fast as asynchronous compilation** by default, due to the overhead of asynchronous callbacks.
243+
To avoid this overhead, you can use the [fibers](https://www.npmjs.com/package/fibers) package to call asynchronous importers from the synchronous code path.
244+
245+
We automatically inject the [`fibers`](https://github.com/laverdet/node-fibers) package (setup `sassOptions.fiber`) for `Node.js` less v16.0.0 if is possible (i.e. you need install the [`fibers`](https://github.com/laverdet/node-fibers) package).
246+
247+
> ⚠ Fibers is not compatible with `Node.js` v16.0.0 or later. Unfortunately, v8
248+
> commit [dacc2fee0f](https://github.com/v8/v8/commit/dacc2fee0f815823782a7e432c79c2a7767a4765) is a breaking change and workarounds are non-trivial. ([see introduction to readme](https://github.com/laverdet/node-fibers)).
249+
250+
**package.json**
251+
252+
```json
253+
{
254+
"devDependencies": {
255+
"sass-loader": "^7.2.0",
256+
"sass": "^1.22.10",
257+
"fibers": "^4.0.1"
258+
}
259+
}
260+
```
261+
262+
You can disable automatically injecting the [`fibers`](https://github.com/laverdet/node-fibers) package by passing a `false` value for the `sassOptions.fiber` option.
263+
264+
**webpack.config.js**
265+
266+
```js
267+
module.exports = {
268+
module: {
269+
rules: [
270+
{
271+
test: /\.s[ac]ss$/i,
272+
use: [
273+
"style-loader",
274+
"css-loader",
275+
{
276+
loader: "sass-loader",
277+
options: {
278+
implementation: require("sass"),
279+
sassOptions: {
280+
fiber: false,
281+
},
282+
},
283+
},
284+
],
285+
},
286+
],
287+
},
288+
};
289+
```
290+
291+
You can also pass the `fiber` value using this code:
292+
293+
**webpack.config.js**
294+
295+
```js
296+
module.exports = {
297+
module: {
298+
rules: [
299+
{
300+
test: /\.s[ac]ss$/i,
301+
use: [
302+
"style-loader",
303+
"css-loader",
304+
{
305+
loader: "sass-loader",
306+
options: {
307+
implementation: require("sass"),
308+
sassOptions: {
309+
fiber: require("fibers"),
310+
},
311+
},
312+
},
313+
],
314+
},
315+
],
316+
},
317+
};
318+
```
319+
242320
### `sassOptions`
243321

244322
Type:

package-lock.json

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dist"
3939
],
4040
"peerDependencies": {
41+
"fibers": ">= 3.1.0",
4142
"node-sass": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
4243
"sass": "^1.3.0",
4344
"sass-embedded": "*",
@@ -52,6 +53,9 @@
5253
},
5354
"sass-embedded": {
5455
"optional": true
56+
},
57+
"fibers": {
58+
"optional": true
5559
}
5660
},
5761
"dependencies": {
@@ -77,6 +81,7 @@
7781
"eslint": "^8.16.0",
7882
"eslint-config-prettier": "^8.3.0",
7983
"eslint-plugin-import": "^2.25.4",
84+
"fibers": "^5.0.1",
8085
"file-loader": "^6.2.0",
8186
"foundation-sites": "^6.6.3",
8287
"husky": "^8.0.1",

src/utils.js

+34
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ function proxyCustomImporters(importers, loaderContext) {
112112
);
113113
}
114114

115+
function isSupportedFibers() {
116+
const [nodeVersion] = process.versions.node.split(".");
117+
118+
return Number(nodeVersion) < 16;
119+
}
120+
115121
/**
116122
* Derives the sass options from the loader context and normalizes its values with sane defaults.
117123
*
@@ -137,6 +143,7 @@ async function getSassOptions(
137143
: {}
138144
);
139145

146+
const isDartSass = implementation.info.includes("dart-sass");
140147
const isModernAPI = loaderOptions.api === "modern";
141148

142149
options.data = loaderOptions.additionalData
@@ -226,6 +233,32 @@ async function getSassOptions(
226233
} else {
227234
options.file = resourcePath;
228235

236+
if (isDartSass && isSupportedFibers()) {
237+
const shouldTryToResolveFibers =
238+
!options.fiber && options.fiber !== false;
239+
240+
if (shouldTryToResolveFibers) {
241+
let fibers;
242+
243+
try {
244+
fibers = require.resolve("fibers");
245+
} catch (_error) {
246+
// Nothing
247+
}
248+
249+
if (fibers) {
250+
// eslint-disable-next-line global-require, import/no-dynamic-require
251+
options.fiber = require(fibers);
252+
}
253+
} else if (options.fiber === false) {
254+
// Don't pass the `fiber` option for `sass` (`Dart Sass`)
255+
delete options.fiber;
256+
}
257+
} else {
258+
// Don't pass the `fiber` option for `node-sass`
259+
delete options.fiber;
260+
}
261+
229262
// opt.outputStyle
230263
if (!options.outputStyle && isProductionLikeMode(loaderContext)) {
231264
options.outputStyle = "compressed";
@@ -775,4 +808,5 @@ export {
775808
getWebpackImporter,
776809
getCompileFn,
777810
normalizeSourceMap,
811+
isSupportedFibers,
778812
};

0 commit comments

Comments
 (0)