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

Lazily require node-sass #748

Closed
Closed
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Only [Active LTS and Current releases][1] are supported.
# Install

```
npm install node-sass gulp-sass --save-dev
npm install gulp-sass --save-dev
```
You must also install either `node-sass` or `sass`.
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

Thoughts on changing this to:

You can either choose node-sass or sass:

npm install gulp-sass sass --save-dev

or:

npm install gulp-sass node-sass --save-dev

This way, users can copy the one-liner they prefer?

Copy link
Author

Choose a reason for hiding this comment

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

I think that makes sense. Perhaps we should also add some additional information on the differences?


# Basic Usage

Expand Down Expand Up @@ -60,7 +61,8 @@ gulp.task('sass:watch', function () {
});
```

You can choose whether to use [Dart Sass][] or [Node Sass][] by setting the `sass.compiler` property. Node Sass will be used by default, but it's strongly recommended that you set it explicitly for forwards-compatibility in case the default ever changes.
## Compiler implementation
You can choose whether to use [Dart Sass][] or [Node Sass][] by setting the `sass.compiler` property. Node Sass will be used by default, but it's strongly recommended that you set it explicitly for forwards-compatibility in case the default ever changes. For the default behavior to work the `node-sass` package has to be installed.

[Dart Sass]: http://sass-lang.com/dart-sass
[Node Sass]: https://github.com/sass/node-sass
Expand Down
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
return cb(new PluginError(PLUGIN_NAME, error));
};

// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const compiler = gulpSass.compiler || require('node-sass');
Copy link

Choose a reason for hiding this comment

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

Should not this be auto-detection?

let compiler = gulpSass.compiler;
if (!compiler) {
  try {
    compiler = require('node-sass');
  } catch (e) {
    try {
      compiler = require('sass');
    } catch (e) {
      return cb(new PluginError(
        PLUGIN_NAME,
        "No compiler found!\nPlease install one of the npm packages: node-sass or sass, or set compiler in code:\n    const sass = require('gulp-sass');\n    sass.compiler = require('sass');\n"
      ));
    }
  }
}

So that user don't need to set dart-sass by hand. For reference, webpack's sass-loader does this kind of auto-detection.

Copy link
Author

Choose a reason for hiding this comment

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

That makes a lot of sense, however I'm not in a position to fix this at the moment.
I'll probably get to it somewhere next week (if nobody else has yet).


if (sync !== true) {
//////////////////////////////
// Async Sass render
Expand All @@ -129,13 +132,13 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
filePush(obj);
};

gulpSass.compiler.render(opts, callback);
compiler.render(opts, callback);
} else {
//////////////////////////////
// Sync Sass render
//////////////////////////////
try {
filePush(gulpSass.compiler.renderSync(opts));
filePush(compiler.renderSync(opts));
} catch (error) {
return errorM(error);
}
Expand All @@ -157,8 +160,8 @@ gulpSass.logError = function logError(error) {
};

//////////////////////////////
// Store compiler in a prop
// Store compiler in a prop (node-sass is dynamically required if needed)
//////////////////////////////
gulpSass.compiler = require('node-sass');
gulpSass.compiler = null;

module.exports = gulpSass;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"dependencies": {
"chalk": "^2.3.0",
"lodash": "^4.17.11",
"node-sass": "^4.8.3",
"plugin-error": "^1.0.1",
"replace-ext": "^1.0.0",
"strip-ansi": "^4.0.0",
Expand All @@ -44,6 +43,7 @@
"gulp-sourcemaps": "^2.6.4",
"gulp-tap": "^0.1.3",
"mocha": "^5.0.4",
"node-sass": "^4.12.0",
HoldYourWaffle marked this conversation as resolved.
Show resolved Hide resolved
"rimraf": "^2.4.3",
"should": "^13.2.1",
"vinyl": "^2.1.0"
Expand Down