Skip to content

Commit

Permalink
refactor: Switch from through2 to stream.Transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasper Hesthaven committed Sep 27, 2020
1 parent e4d3adc commit 93c269c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 91 deletions.
101 changes: 55 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,66 @@
const through2 = require("through2");
const pluginError = require("plugin-error");
const dartSass = require("sass");
const applySourceMap = require("vinyl-sourcemaps-apply");
const PluginError = require("plugin-error");
const Transform = require("stream").Transform;
const path = require("path");
const applySourceMap = require("vinyl-sourcemaps-apply");
const dartSass = require("sass");

const PLUGIN_NAME = "gulp-dart-scss";

module.exports = (options = {}) => {
return through2.obj((file, encoding, callback) => {
if (file.isNull()) {
return callback(null, file);
}

if (file.isStream()) {
return callback(
new pluginError("gulp-dart-scss", "Streaming not supported")
);
}

// Ignore _name.scss files
if (path.basename(file.path).indexOf("_") === 0) {
return callback();
}

(async () => {
try {
options.sourceMap = file.sourceMap ? file.path : false;
options.file = file.path || options.file;

const result = await dartSass.renderSync(options);
file.contents = Buffer.from(result.css);

// Replace .scss with .css
file.path = path.join(
path.dirname(file.path),
path.basename(file.path, path.extname(file.path)) + ".css"
return new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
// Return chunk if not scss extension
if (path.extname(chunk.path) !== ".scss") {
return callback(null, chunk);
}

// Ignore if empty or null
if (chunk._contents.length === 0 || chunk.isNull()) {
return callback(null);
}

// Ignore _name.scss files
if (path.basename(chunk.path).indexOf("_") === 0) {
return callback(null);
}

if (chunk.isBuffer()) {
options.sourceMap = chunk.sourceMap ? chunk.path : false;
options.file = chunk.path || options.file;

// Rename extension
chunk.path = path.join(
path.dirname(chunk.path),
path.basename(chunk.path, path.extname(chunk.path)) + ".css"
);

// Apply sourcemap if gulp-sourcemap is present
if (result.map && file.sourceMap) {
const map = JSON.parse(result.map);
map.file = file.relative;
map.sources = map.sources.map(source =>
path.relative(file.base, source)
try {
const renderResult = dartSass.renderSync(options);
chunk._contents = Buffer.from(renderResult.css);

if (renderResult.map && chunk.sourceMap) {
const map = JSON.parse(renderResult.map);
map.file = chunk.relative;
map.sources = map.sources.map((source) =>
path.relative(chunk.base, source)
);
applySourceMap(chunk, map);
}

return callback(null, chunk);
} catch (error) {
return callback(
new PluginError(PLUGIN_NAME, error.message, {
fileName: chunk.relative,
})
);
applySourceMap(file, map);
}

setImmediate(callback, null, file);
} catch (error) {
process.stderr.write(
`${new pluginError("gulp-dart-scss", error.message).toString()} \r\n`
} else if (chunk.isStream()) {
return callback(
new PluginError(PLUGIN_NAME, "Streaming is not supported.")
);
return callback();
}
})();
},
});
};
43 changes: 1 addition & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-dart-scss",
"version": "1.0.5",
"version": "1.1.0",
"description": "Gulp plugin to compile Sass (.scss) to CSS using the Dart Sass compiler.",
"author": "Kasper Hesthaven",
"repository": "kasperhesthaven/gulp-dart-scss",
Expand All @@ -19,8 +19,7 @@
"dependencies": {
"sass": "^1.26.11",
"plugin-error": "^1.0.1",
"vinyl-sourcemaps-apply": "^0.2.1",
"through2": "4.0.2"
"vinyl-sourcemaps-apply": "^0.2.1"
},
"files": [
"LICENSE",
Expand Down

0 comments on commit 93c269c

Please sign in to comment.