Skip to content

Commit

Permalink
feat: automatically use the fibers package if it is possible (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Aug 24, 2019
1 parent ac14fd5 commit 96184e1
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 6 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,50 @@ module.exports = {
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.
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.

To enable this, pass the `Fiber` class to the `sassOptions.fiber` option:
We automatically inject the [`fibers`](https://github.com/laverdet/node-fibers) package (setup `sassOptions.fiber`) if is possible (i.e. you need install the [`fibers`](https://github.com/laverdet/node-fibers) package).

**package.json**

```json
{
"devDependencies": {
"sass-loader": "^7.2.0",
"sass": "^1.22.10",
"fibers": "^4.0.1"
}
}
```

You can disable automatically inject the [`fibers`](https://github.com/laverdet/node-fibers) package pass the `false` value for the `sassOptions.fiber` option.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
sassOptions: {
fiber: false,
},
},
},
],
},
],
},
};
```

Also you can pass own the `fiber` value using this code:

**webpack.config.js**

Expand Down
32 changes: 30 additions & 2 deletions src/getSassOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ function isProductionLikeMode(loaderContext) {
/**
* Derives the sass options from the loader context and normalizes its values with sane defaults.
*
* @param {LoaderContext} loaderContext
* @param {object} loaderContext
* @param {object} loaderOptions
* @param {string} content
* @param {object} implementation
* @returns {Object}
*/
function getSassOptions(loaderContext, loaderOptions, content) {
function getSassOptions(loaderContext, loaderOptions, content, implementation) {
const options = cloneDeep(
loaderOptions.sassOptions
? typeof loaderOptions.sassOptions === 'function'
Expand All @@ -30,6 +31,33 @@ function getSassOptions(loaderContext, loaderOptions, content) {
: {}
);

const isDartSass = implementation.info.includes('dart-sass');

if (isDartSass) {
const shouldTryToResolveFibers = !options.fiber && options.fiber !== false;

if (shouldTryToResolveFibers) {
let fibers;

try {
fibers = require.resolve('fibers');
} catch (_error) {
// Nothing
}

if (fibers) {
// eslint-disable-next-line global-require, import/no-dynamic-require
options.fiber = require(fibers);
}
} else if (options.fiber === false) {
// Don't pass the `fiber` option for `sass` (`Dart Sass`)
delete options.fiber;
}
} else {
// Don't pass the `fiber` option for `node-sass`
delete options.fiber;
}

options.data = loaderOptions.prependData
? typeof loaderOptions.prependData === 'function'
? loaderOptions.prependData(loaderContext) + os.EOL + content
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SassError from './SassError';
/**
* The sass-loader makes node-sass and dart-sass available to webpack modules.
*
* @this {LoaderContext}
* @this {object}
* @param {string} content
*/
function loader(content) {
Expand All @@ -32,7 +32,7 @@ function loader(content) {
this.addDependency(path.normalize(file));
};

const sassOptions = getSassOptions(this, options, content);
const sassOptions = getSassOptions(this, options, content, implementation);

const shouldUseWebpackImporter =
typeof options.webpackImporter === 'boolean'
Expand Down
Loading

0 comments on commit 96184e1

Please sign in to comment.