From 98d9a91aa7ae2dba204a139b5cded96e23f3b7b6 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 6 Oct 2021 13:31:17 +0300 Subject: [PATCH 1/4] Minor tweaks and cleanup * enable strict mode (these aren't modules) * use a template literal for the missing compiler message * move variables where we use them * disable a few ESLint rules * move ESLint overrides around --- .eslintrc | 12 ++- index.js | 262 ++++++++++++++++++++++++++------------------------- test/main.js | 2 + 3 files changed, 147 insertions(+), 129 deletions(-) diff --git a/.eslintrc b/.eslintrc index 6bebcd20..9d817ff9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,9 +4,17 @@ env: mocha: true node: true +extends: "airbnb-base" + +parserOptions: + sourceType: "script" + rules: max-len: - 2 - 120 - -extends: "airbnb-base" + no-multi-assign: "off" + no-param-reassign: "off" + strict: + - "error" + - "safe" diff --git a/index.js b/index.js index 3d03cea4..b93bde58 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +'use strict'; + const path = require('path'); const chalk = require('chalk'); const PluginError = require('plugin-error'); @@ -9,151 +11,161 @@ const applySourceMap = require('vinyl-sourcemaps-apply'); const PLUGIN_NAME = 'gulp-sass'; -/// /////////////////////////// -// Main Gulp Sass function -/// /////////////////////////// -const gulpSass = (options, sync) => transfob((file, enc, cb) => { // eslint-disable-line consistent-return - if (file.isNull()) { - return cb(null, file); - } +const MISSING_COMPILER_MESSAGE = ` +gulp-sass 5 does not have a default Sass compiler; please set one yourself. +Both the "sass" and "node-sass" packages are permitted. +For example, in your gulpfile: - if (file.isStream()) { - return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported')); - } + const sass = require('gulp-sass')(require('sass')); +`; - if (path.basename(file.path).startsWith('_')) { - return cb(); - } +/* + Main Gulp Sass function +*/ - if (!file.contents.length) { - file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign - return cb(null, file); - } - - const opts = clonedeep(options || {}); - opts.data = file.contents.toString(); +// eslint-disable-next-line arrow-body-style +const gulpSass = (options, sync) => { + // eslint-disable-next-line consistent-return + return transfob((file, enc, cb) => { + if (file.isNull()) { + return cb(null, file); + } - // we set the file path here so that libsass can correctly resolve import paths - opts.file = file.path; + if (file.isStream()) { + return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported')); + } - // Ensure `indentedSyntax` is true if a `.sass` file - if (path.extname(file.path) === '.sass') { - opts.indentedSyntax = true; - } + if (path.basename(file.path).startsWith('_')) { + return cb(); + } - // Ensure file's parent directory in the include path - if (opts.includePaths) { - if (typeof opts.includePaths === 'string') { - opts.includePaths = [opts.includePaths]; + if (!file.contents.length) { + file.path = replaceExtension(file.path, '.css'); + return cb(null, file); } - } else { - opts.includePaths = []; - } - opts.includePaths.unshift(path.dirname(file.path)); + const opts = clonedeep(options || {}); + opts.data = file.contents.toString(); - // Generate Source Maps if plugin source-map present - if (file.sourceMap) { - opts.sourceMap = file.path; - opts.omitSourceMapUrl = true; - opts.sourceMapContents = true; - } + // We set the file path here so that libsass can correctly resolve import paths + opts.file = file.path; + + // Ensure `indentedSyntax` is true if a `.sass` file + if (path.extname(file.path) === '.sass') { + opts.indentedSyntax = true; + } - /// /////////////////////////// - // Handles returning the file to the stream - /// /////////////////////////// - const filePush = (sassObj) => { - let sassMap; - let sassMapFile; - let sassFileSrc; - let sassFileSrcPath; - let sourceFileIndex; - - // Build Source Maps! - if (sassObj.map) { - // Transform map into JSON - sassMap = JSON.parse(sassObj.map.toString()); - // Grab the stdout and transform it into stdin - sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); - // Grab the base file name that's being worked on - sassFileSrc = file.relative; - // Grab the path portion of the file that's being worked on - sassFileSrcPath = path.dirname(sassFileSrc); - if (sassFileSrcPath) { - // Prepend the path to all files in the sources array except the file that's being worked on - sourceFileIndex = sassMap.sources.indexOf(sassMapFile); - sassMap.sources = sassMap.sources.map((source, index) => { // eslint-disable-line arrow-body-style - return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source); - }); + // Ensure file's parent directory in the include path + if (opts.includePaths) { + if (typeof opts.includePaths === 'string') { + opts.includePaths = [opts.includePaths]; } + } else { + opts.includePaths = []; + } - // Remove 'stdin' from souces and replace with filenames! - sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src); + opts.includePaths.unshift(path.dirname(file.path)); - // Replace the map file with the original file name (but new extension) - sassMap.file = replaceExtension(sassFileSrc, '.css'); - // Apply the map - applySourceMap(file, sassMap); + // Generate Source Maps if the source-map plugin is present + if (file.sourceMap) { + opts.sourceMap = file.path; + opts.omitSourceMapUrl = true; + opts.sourceMapContents = true; } - file.contents = sassObj.css; // eslint-disable-line no-param-reassign - file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign + /* + Handles returning the file to the stream + */ + const filePush = (sassObj) => { + // Build Source Maps! + if (sassObj.map) { + // Transform map into JSON + const sassMap = JSON.parse(sassObj.map.toString()); + // Grab the stdout and transform it into stdin + const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); + // Grab the base filename that's being worked on + const sassFileSrc = file.relative; + // Grab the path portion of the file that's being worked on + const sassFileSrcPath = path.dirname(sassFileSrc); + + if (sassFileSrcPath) { + const sourceFileIndex = sassMap.sources.indexOf(sassMapFile); + // Prepend the path to all files in the sources array except the file that's being worked on + sassMap.sources = sassMap.sources.map((source, index) => ( + index === sourceFileIndex + ? source + : path.join(sassFileSrcPath, source) + )); + } + + // Remove 'stdin' from souces and replace with filenames! + sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src); + + // Replace the map file with the original filename (but new extension) + sassMap.file = replaceExtension(sassFileSrc, '.css'); + // Apply the map + applySourceMap(file, sassMap); + } - if (file.stat) { - file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); // eslint-disable-line - } + file.contents = sassObj.css; + file.path = replaceExtension(file.path, '.css'); - cb(null, file); - }; - - /// /////////////////////////// - // Handles error message - /// /////////////////////////// - const errorM = (error) => { - const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; - const relativePath = path.relative(process.cwd(), filePath); - const message = [chalk.underline(relativePath), error.formatted].join('\n'); - - error.messageFormatted = message; // eslint-disable-line no-param-reassign - error.messageOriginal = error.message; // eslint-disable-line no-param-reassign - error.message = stripAnsi(message); // eslint-disable-line no-param-reassign - error.relativePath = relativePath; // eslint-disable-line no-param-reassign - - return cb(new PluginError(PLUGIN_NAME, error)); - }; - - if (sync !== true) { - /// /////////////////////////// - // Async Sass render - /// /////////////////////////// - const callback = (error, obj) => { // eslint-disable-line consistent-return - if (error) { - return errorM(error); + if (file.stat) { + file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); } - filePush(obj); + + cb(null, file); }; - gulpSass.compiler.render(opts, callback); - } else { - /// /////////////////////////// - // Sync Sass render - /// /////////////////////////// - try { - filePush(gulpSass.compiler.renderSync(opts)); - } catch (error) { - return errorM(error); + /* + Handles error message + */ + const handleError = (error) => { + const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; + const relativePath = path.relative(process.cwd(), filePath); + const message = [chalk.underline(relativePath), error.formatted].join('\n'); + + error.messageFormatted = message; + error.messageOriginal = error.message; + error.message = stripAnsi(message); + error.relativePath = relativePath; + + return cb(new PluginError(PLUGIN_NAME, error)); + }; + + if (sync !== true) { + /* + Async Sass render + */ + // eslint-disable-next-line consistent-return + gulpSass.compiler.render(opts, (error, obj) => { + if (error) { + return handleError(error); + } + + filePush(obj); + }); + } else { + /* + Sync Sass render + */ + try { + filePush(gulpSass.compiler.renderSync(opts)); + } catch (error) { + return handleError(error); + } } - } -}); + }); +}; -/// /////////////////////////// -// Sync Sass render -/// /////////////////////////// +/* + Sync Sass render +*/ gulpSass.sync = (options) => gulpSass(options, true); -/// /////////////////////////// -// Log errors nicely -/// /////////////////////////// +/* + Log errors nicely +*/ gulpSass.logError = function logError(error) { const message = new PluginError('sass', error.messageFormatted).toString(); process.stderr.write(`${message}\n`); @@ -164,17 +176,13 @@ module.exports = (compiler) => { if (!compiler || !compiler.render) { const message = new PluginError( PLUGIN_NAME, - '\n' - + 'gulp-sass 5 does not have a default Sass compiler; please set one yourself.\n' - + 'Both the `sass` and `node-sass` packages are permitted.\n' - - + 'For example, in your gulpfile:\n\n' - + ' var sass = require(\'gulp-sass\')(require(\'sass\'));\n', + MISSING_COMPILER_MESSAGE, { showProperties: false }, ).toString(); process.stderr.write(`${message}\n`); process.exit(1); } + gulpSass.compiler = compiler; return gulpSass; }; diff --git a/test/main.js b/test/main.js index 284f1190..5cd8bb16 100644 --- a/test/main.js +++ b/test/main.js @@ -1,3 +1,5 @@ +'use strict'; + const fs = require('fs'); const path = require('path'); const should = require('should'); From 65c8be4afec43c70d5850c53902e95d6f661d5b3 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 6 Oct 2021 19:04:26 +0300 Subject: [PATCH 2/4] Move helper functions outside of `gulpSass` --- index.js | 128 +++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/index.js b/index.js index b93bde58..a23a71f3 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,66 @@ For example, in your gulpfile: const sass = require('gulp-sass')(require('sass')); `; +/* + Handles returning the file to the stream +*/ +const filePush = (file, sassObj, cb) => { + // Build Source Maps! + if (sassObj.map) { + // Transform map into JSON + const sassMap = JSON.parse(sassObj.map.toString()); + // Grab the stdout and transform it into stdin + const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); + // Grab the base filename that's being worked on + const sassFileSrc = file.relative; + // Grab the path portion of the file that's being worked on + const sassFileSrcPath = path.dirname(sassFileSrc); + + if (sassFileSrcPath) { + const sourceFileIndex = sassMap.sources.indexOf(sassMapFile); + // Prepend the path to all files in the sources array except the file that's being worked on + sassMap.sources = sassMap.sources.map((source, index) => ( + index === sourceFileIndex + ? source + : path.join(sassFileSrcPath, source) + )); + } + + // Remove 'stdin' from souces and replace with filenames! + sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src); + + // Replace the map file with the original filename (but new extension) + sassMap.file = replaceExtension(sassFileSrc, '.css'); + // Apply the map + applySourceMap(file, sassMap); + } + + file.contents = sassObj.css; + file.path = replaceExtension(file.path, '.css'); + + if (file.stat) { + file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); + } + + cb(null, file); +}; + +/* + Handles error message +*/ +const handleError = (error, file, cb) => { + const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; + const relativePath = path.relative(process.cwd(), filePath); + const message = [chalk.underline(relativePath), error.formatted].join('\n'); + + error.messageFormatted = message; + error.messageOriginal = error.message; + error.message = stripAnsi(message); + error.relativePath = relativePath; + + return cb(new PluginError(PLUGIN_NAME, error)); +}; + /* Main Gulp Sass function */ @@ -73,66 +133,6 @@ const gulpSass = (options, sync) => { opts.sourceMapContents = true; } - /* - Handles returning the file to the stream - */ - const filePush = (sassObj) => { - // Build Source Maps! - if (sassObj.map) { - // Transform map into JSON - const sassMap = JSON.parse(sassObj.map.toString()); - // Grab the stdout and transform it into stdin - const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); - // Grab the base filename that's being worked on - const sassFileSrc = file.relative; - // Grab the path portion of the file that's being worked on - const sassFileSrcPath = path.dirname(sassFileSrc); - - if (sassFileSrcPath) { - const sourceFileIndex = sassMap.sources.indexOf(sassMapFile); - // Prepend the path to all files in the sources array except the file that's being worked on - sassMap.sources = sassMap.sources.map((source, index) => ( - index === sourceFileIndex - ? source - : path.join(sassFileSrcPath, source) - )); - } - - // Remove 'stdin' from souces and replace with filenames! - sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src); - - // Replace the map file with the original filename (but new extension) - sassMap.file = replaceExtension(sassFileSrc, '.css'); - // Apply the map - applySourceMap(file, sassMap); - } - - file.contents = sassObj.css; - file.path = replaceExtension(file.path, '.css'); - - if (file.stat) { - file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); - } - - cb(null, file); - }; - - /* - Handles error message - */ - const handleError = (error) => { - const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; - const relativePath = path.relative(process.cwd(), filePath); - const message = [chalk.underline(relativePath), error.formatted].join('\n'); - - error.messageFormatted = message; - error.messageOriginal = error.message; - error.message = stripAnsi(message); - error.relativePath = relativePath; - - return cb(new PluginError(PLUGIN_NAME, error)); - }; - if (sync !== true) { /* Async Sass render @@ -140,19 +140,19 @@ const gulpSass = (options, sync) => { // eslint-disable-next-line consistent-return gulpSass.compiler.render(opts, (error, obj) => { if (error) { - return handleError(error); + return handleError(error, file, cb); } - filePush(obj); + filePush(file, obj, cb); }); } else { /* Sync Sass render */ try { - filePush(gulpSass.compiler.renderSync(opts)); + filePush(file, gulpSass.compiler.renderSync(opts), cb); } catch (error) { - return handleError(error); + return handleError(error, file, cb); } } }); From cf79dddc89cb2559f0dfe4de639ff15a82f9fd68 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Thu, 7 Oct 2021 07:37:36 +0300 Subject: [PATCH 3/4] Rename variables --- index.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index a23a71f3..b2a137cb 100644 --- a/index.js +++ b/index.js @@ -22,11 +22,11 @@ For example, in your gulpfile: /* Handles returning the file to the stream */ -const filePush = (file, sassObj, cb) => { +const filePush = (file, sassObject, callback) => { // Build Source Maps! - if (sassObj.map) { + if (sassObject.map) { // Transform map into JSON - const sassMap = JSON.parse(sassObj.map.toString()); + const sassMap = JSON.parse(sassObject.map.toString()); // Grab the stdout and transform it into stdin const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); // Grab the base filename that's being worked on @@ -53,20 +53,20 @@ const filePush = (file, sassObj, cb) => { applySourceMap(file, sassMap); } - file.contents = sassObj.css; + file.contents = sassObject.css; file.path = replaceExtension(file.path, '.css'); if (file.stat) { file.stat.atime = file.stat.mtime = file.stat.ctime = new Date(); } - cb(null, file); + callback(null, file); }; /* Handles error message */ -const handleError = (error, file, cb) => { +const handleError = (error, file, callback) => { const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; const relativePath = path.relative(process.cwd(), filePath); const message = [chalk.underline(relativePath), error.formatted].join('\n'); @@ -76,7 +76,7 @@ const handleError = (error, file, cb) => { error.message = stripAnsi(message); error.relativePath = relativePath; - return cb(new PluginError(PLUGIN_NAME, error)); + return callback(new PluginError(PLUGIN_NAME, error)); }; /* @@ -86,22 +86,22 @@ const handleError = (error, file, cb) => { // eslint-disable-next-line arrow-body-style const gulpSass = (options, sync) => { // eslint-disable-next-line consistent-return - return transfob((file, enc, cb) => { + return transfob((file, encoding, callback) => { if (file.isNull()) { - return cb(null, file); + return callback(null, file); } if (file.isStream()) { - return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported')); + return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported')); } if (path.basename(file.path).startsWith('_')) { - return cb(); + return callback(); } if (!file.contents.length) { file.path = replaceExtension(file.path, '.css'); - return cb(null, file); + return callback(null, file); } const opts = clonedeep(options || {}); @@ -140,19 +140,19 @@ const gulpSass = (options, sync) => { // eslint-disable-next-line consistent-return gulpSass.compiler.render(opts, (error, obj) => { if (error) { - return handleError(error, file, cb); + return handleError(error, file, callback); } - filePush(file, obj, cb); + filePush(file, obj, callback); }); } else { /* Sync Sass render */ try { - filePush(file, gulpSass.compiler.renderSync(opts), cb); + filePush(file, gulpSass.compiler.renderSync(opts), callback); } catch (error) { - return handleError(error, file, cb); + return handleError(error, file, callback); } } }); From 3e2a2d871cf03abe5c38c07095772a8a4f6b34e9 Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sat, 9 Oct 2021 08:10:42 +0300 Subject: [PATCH 4/4] Change comments --- index.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index b2a137cb..3d98e704 100644 --- a/index.js +++ b/index.js @@ -19,9 +19,9 @@ For example, in your gulpfile: const sass = require('gulp-sass')(require('sass')); `; -/* - Handles returning the file to the stream -*/ +/** + * Handles returning the file to the stream + */ const filePush = (file, sassObject, callback) => { // Build Source Maps! if (sassObject.map) { @@ -63,9 +63,9 @@ const filePush = (file, sassObject, callback) => { callback(null, file); }; -/* - Handles error message -*/ +/** + * Handles error message + */ const handleError = (error, file, callback) => { const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; const relativePath = path.relative(process.cwd(), filePath); @@ -79,9 +79,9 @@ const handleError = (error, file, callback) => { return callback(new PluginError(PLUGIN_NAME, error)); }; -/* - Main Gulp Sass function -*/ +/** + * Main Gulp Sass function + */ // eslint-disable-next-line arrow-body-style const gulpSass = (options, sync) => { @@ -134,9 +134,9 @@ const gulpSass = (options, sync) => { } if (sync !== true) { - /* - Async Sass render - */ + /** + * Async Sass render + */ // eslint-disable-next-line consistent-return gulpSass.compiler.render(opts, (error, obj) => { if (error) { @@ -146,9 +146,9 @@ const gulpSass = (options, sync) => { filePush(file, obj, callback); }); } else { - /* - Sync Sass render - */ + /** + * Sync Sass render + */ try { filePush(file, gulpSass.compiler.renderSync(opts), callback); } catch (error) { @@ -158,14 +158,14 @@ const gulpSass = (options, sync) => { }); }; -/* - Sync Sass render -*/ +/** + * Sync Sass render + */ gulpSass.sync = (options) => gulpSass(options, true); -/* - Log errors nicely -*/ +/** + * Log errors nicely + */ gulpSass.logError = function logError(error) { const message = new PluginError('sass', error.messageFormatted).toString(); process.stderr.write(`${message}\n`);