|
1 |
| -'use strict'; |
| 1 | +const chalk = require('chalk'); |
| 2 | +const PluginError = require('plugin-error'); |
| 3 | +const replaceExtension = require('replace-ext'); |
| 4 | +const stripAnsi = require('strip-ansi'); |
| 5 | +const through = require('through2'); |
| 6 | +const clonedeep = require('lodash.clonedeep'); |
| 7 | +const path = require('path'); |
| 8 | +const applySourceMap = require('vinyl-sourcemaps-apply'); |
2 | 9 |
|
3 |
| -var gutil = require('gulp-util'); |
4 |
| -var through = require('through2'); |
5 |
| -var clonedeep = require('lodash.clonedeep'); |
6 |
| -var path = require('path'); |
7 |
| -var applySourceMap = require('vinyl-sourcemaps-apply'); |
8 |
| - |
9 |
| -var PLUGIN_NAME = 'gulp-sass'; |
| 10 | +const PLUGIN_NAME = 'gulp-sass'; |
10 | 11 |
|
11 | 12 | //////////////////////////////
|
12 | 13 | // Main Gulp Sass function
|
13 | 14 | //////////////////////////////
|
14 |
| -var gulpSass = function gulpSass(options, sync) { |
15 |
| - return through.obj(function(file, enc, cb) { |
16 |
| - var opts, |
17 |
| - filePush, |
18 |
| - errorM, |
19 |
| - callback, |
20 |
| - result; |
21 |
| - |
22 |
| - if (file.isNull()) { |
23 |
| - return cb(null, file); |
24 |
| - } |
25 |
| - if (file.isStream()) { |
26 |
| - return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported')); |
27 |
| - } |
28 |
| - if (path.basename(file.path).indexOf('_') === 0) { |
29 |
| - return cb(); |
30 |
| - } |
31 |
| - if (!file.contents.length) { |
32 |
| - file.path = gutil.replaceExtension(file.path, '.css'); |
33 |
| - return cb(null, file); |
| 15 | +const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-disable-line consistent-return |
| 16 | + if (file.isNull()) { |
| 17 | + return cb(null, file); |
| 18 | + } |
| 19 | + |
| 20 | + if (file.isStream()) { |
| 21 | + return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported')); |
| 22 | + } |
| 23 | + |
| 24 | + if (path.basename(file.path).indexOf('_') === 0) { |
| 25 | + return cb(); |
| 26 | + } |
| 27 | + |
| 28 | + if (!file.contents.length) { |
| 29 | + file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign |
| 30 | + return cb(null, file); |
| 31 | + } |
| 32 | + |
| 33 | + const opts = clonedeep(options || {}); |
| 34 | + opts.data = file.contents.toString(); |
| 35 | + |
| 36 | + // we set the file path here so that libsass can correctly resolve import paths |
| 37 | + opts.file = file.path; |
| 38 | + |
| 39 | + // Ensure `indentedSyntax` is true if a `.sass` file |
| 40 | + if (path.extname(file.path) === '.sass') { |
| 41 | + opts.indentedSyntax = true; |
| 42 | + } |
| 43 | + |
| 44 | + // Ensure file's parent directory in the include path |
| 45 | + if (opts.includePaths) { |
| 46 | + if (typeof opts.includePaths === 'string') { |
| 47 | + opts.includePaths = [opts.includePaths]; |
34 | 48 | }
|
| 49 | + } else { |
| 50 | + opts.includePaths = []; |
| 51 | + } |
| 52 | + |
| 53 | + opts.includePaths.unshift(path.dirname(file.path)); |
| 54 | + |
| 55 | + // Generate Source Maps if plugin source-map present |
| 56 | + if (file.sourceMap) { |
| 57 | + opts.sourceMap = file.path; |
| 58 | + opts.omitSourceMapUrl = true; |
| 59 | + opts.sourceMapContents = true; |
| 60 | + } |
| 61 | + |
| 62 | + ////////////////////////////// |
| 63 | + // Handles returning the file to the stream |
| 64 | + ////////////////////////////// |
| 65 | + const filePush = (sassObj) => { |
| 66 | + let sassMap; |
| 67 | + let sassMapFile; |
| 68 | + let sassFileSrc; |
| 69 | + let sassFileSrcPath; |
| 70 | + let sourceFileIndex; |
| 71 | + |
| 72 | + // Build Source Maps! |
| 73 | + if (sassObj.map) { |
| 74 | + // Transform map into JSON |
| 75 | + sassMap = JSON.parse(sassObj.map.toString()); |
| 76 | + // Grab the stdout and transform it into stdin |
| 77 | + sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); |
| 78 | + // Grab the base file name that's being worked on |
| 79 | + sassFileSrc = file.relative; |
| 80 | + // Grab the path portion of the file that's being worked on |
| 81 | + sassFileSrcPath = path.dirname(sassFileSrc); |
| 82 | + if (sassFileSrcPath) { |
| 83 | + // Prepend the path to all files in the sources array except the file that's being worked on |
| 84 | + sourceFileIndex = sassMap.sources.indexOf(sassMapFile); |
| 85 | + sassMap.sources = sassMap.sources.map((source, index) => { // eslint-disable-line arrow-body-style |
| 86 | + return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source); |
| 87 | + }); |
| 88 | + } |
35 | 89 |
|
| 90 | + // Remove 'stdin' from souces and replace with filenames! |
| 91 | + sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src); |
36 | 92 |
|
37 |
| - opts = clonedeep(options || {}); |
38 |
| - opts.data = file.contents.toString(); |
| 93 | + // Replace the map file with the original file name (but new extension) |
| 94 | + sassMap.file = replaceExtension(sassFileSrc, '.css'); |
| 95 | + // Apply the map |
| 96 | + applySourceMap(file, sassMap); |
| 97 | + } |
39 | 98 |
|
40 |
| - // we set the file path here so that libsass can correctly resolve import paths |
41 |
| - opts.file = file.path; |
| 99 | + file.contents = sassObj.css; // eslint-disable-line no-param-reassign |
| 100 | + file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign |
42 | 101 |
|
43 |
| - // Ensure `indentedSyntax` is true if a `.sass` file |
44 |
| - if (path.extname(file.path) === '.sass') { |
45 |
| - opts.indentedSyntax = true; |
46 |
| - } |
| 102 | + cb(null, file); |
| 103 | + }; |
47 | 104 |
|
48 |
| - // Ensure file's parent directory in the include path |
49 |
| - if (opts.includePaths) { |
50 |
| - if (typeof opts.includePaths === 'string') { |
51 |
| - opts.includePaths = [opts.includePaths]; |
52 |
| - } |
53 |
| - } |
54 |
| - else { |
55 |
| - opts.includePaths = []; |
56 |
| - } |
| 105 | + ////////////////////////////// |
| 106 | + // Handles error message |
| 107 | + ////////////////////////////// |
| 108 | + const errorM = (error) => { |
| 109 | + const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path; |
| 110 | + const relativePath = path.relative(process.cwd(), filePath); |
| 111 | + const message = [chalk.underline(relativePath), error.formatted].join('\n'); |
57 | 112 |
|
58 |
| - opts.includePaths.unshift(path.dirname(file.path)); |
| 113 | + error.messageFormatted = message; // eslint-disable-line no-param-reassign |
| 114 | + error.messageOriginal = error.message; // eslint-disable-line no-param-reassign |
| 115 | + error.message = stripAnsi(message); // eslint-disable-line no-param-reassign |
| 116 | + error.relativePath = relativePath; // eslint-disable-line no-param-reassign |
59 | 117 |
|
60 |
| - // Generate Source Maps if plugin source-map present |
61 |
| - if (file.sourceMap) { |
62 |
| - opts.sourceMap = file.path; |
63 |
| - opts.omitSourceMapUrl = true; |
64 |
| - opts.sourceMapContents = true; |
65 |
| - } |
| 118 | + return cb(new PluginError(PLUGIN_NAME, error)); |
| 119 | + }; |
66 | 120 |
|
| 121 | + if (sync !== true) { |
67 | 122 | //////////////////////////////
|
68 |
| - // Handles returning the file to the stream |
| 123 | + // Async Sass render |
69 | 124 | //////////////////////////////
|
70 |
| - filePush = function filePush(sassObj) { |
71 |
| - var sassMap, |
72 |
| - sassMapFile, |
73 |
| - sassFileSrc, |
74 |
| - sassFileSrcPath, |
75 |
| - sourceFileIndex; |
76 |
| - |
77 |
| - // Build Source Maps! |
78 |
| - if (sassObj.map) { |
79 |
| - // Transform map into JSON |
80 |
| - sassMap = JSON.parse(sassObj.map.toString()); |
81 |
| - // Grab the stdout and transform it into stdin |
82 |
| - sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin'); |
83 |
| - // Grab the base file name that's being worked on |
84 |
| - sassFileSrc = file.relative; |
85 |
| - // Grab the path portion of the file that's being worked on |
86 |
| - sassFileSrcPath = path.dirname(sassFileSrc); |
87 |
| - if (sassFileSrcPath) { |
88 |
| - // Prepend the path to all files in the sources array except the file that's being worked on |
89 |
| - sourceFileIndex = sassMap.sources.indexOf(sassMapFile); |
90 |
| - sassMap.sources = sassMap.sources.map(function(source, index) { |
91 |
| - return (index === sourceFileIndex) ? source : path.join(sassFileSrcPath, source); |
92 |
| - }); |
93 |
| - } |
94 |
| - |
95 |
| - // Remove 'stdin' from souces and replace with filenames! |
96 |
| - sassMap.sources = sassMap.sources.filter(function(src) { |
97 |
| - if (src !== 'stdin') { |
98 |
| - return src; |
99 |
| - } |
100 |
| - }); |
101 |
| - |
102 |
| - // Replace the map file with the original file name (but new extension) |
103 |
| - sassMap.file = gutil.replaceExtension(sassFileSrc, '.css'); |
104 |
| - // Apply the map |
105 |
| - applySourceMap(file, sassMap); |
| 125 | + const callback = (error, obj) => { // eslint-disable-line consistent-return |
| 126 | + if (error) { |
| 127 | + return errorM(error); |
106 | 128 | }
|
107 |
| - |
108 |
| - file.contents = sassObj.css; |
109 |
| - file.path = gutil.replaceExtension(file.path, '.css'); |
110 |
| - |
111 |
| - cb(null, file); |
| 129 | + filePush(obj); |
112 | 130 | };
|
113 | 131 |
|
| 132 | + gulpSass.compiler.render(opts, callback); |
| 133 | + } else { |
114 | 134 | //////////////////////////////
|
115 |
| - // Handles error message |
| 135 | + // Sync Sass render |
116 | 136 | //////////////////////////////
|
117 |
| - errorM = function errorM(error) { |
118 |
| - var relativePath = '', |
119 |
| - filePath = error.file === 'stdin' ? file.path : error.file, |
120 |
| - message = ''; |
121 |
| - |
122 |
| - filePath = filePath ? filePath : file.path; |
123 |
| - relativePath = path.relative(process.cwd(), filePath); |
124 |
| - |
125 |
| - message += gutil.colors.underline(relativePath) + '\n'; |
126 |
| - message += error.formatted; |
127 |
| - |
128 |
| - error.messageFormatted = message; |
129 |
| - error.messageOriginal = error.message; |
130 |
| - error.message = gutil.colors.stripColor(message); |
131 |
| - |
132 |
| - error.relativePath = relativePath; |
133 |
| - |
134 |
| - return cb(new gutil.PluginError( |
135 |
| - PLUGIN_NAME, error |
136 |
| - )); |
137 |
| - }; |
138 |
| - |
139 |
| - if (sync !== true) { |
140 |
| - ////////////////////////////// |
141 |
| - // Async Sass render |
142 |
| - ////////////////////////////// |
143 |
| - callback = function(error, obj) { |
144 |
| - if (error) { |
145 |
| - return errorM(error); |
146 |
| - } |
147 |
| - filePush(obj); |
148 |
| - }; |
149 |
| - |
150 |
| - gulpSass.compiler.render(opts, callback); |
| 137 | + try { |
| 138 | + filePush(gulpSass.compiler.renderSync(opts)); |
| 139 | + } catch (error) { |
| 140 | + return errorM(error); |
151 | 141 | }
|
152 |
| - else { |
153 |
| - ////////////////////////////// |
154 |
| - // Sync Sass render |
155 |
| - ////////////////////////////// |
156 |
| - try { |
157 |
| - result = gulpSass.compiler.renderSync(opts); |
158 |
| - |
159 |
| - filePush(result); |
160 |
| - } |
161 |
| - catch (error) { |
162 |
| - return errorM(error); |
163 |
| - } |
164 |
| - } |
165 |
| - }); |
166 |
| -}; |
| 142 | + } |
| 143 | +}); |
167 | 144 |
|
168 | 145 | //////////////////////////////
|
169 | 146 | // Sync Sass render
|
170 | 147 | //////////////////////////////
|
171 |
| -gulpSass.sync = function sync(options) { |
172 |
| - return gulpSass(options, true); |
173 |
| -}; |
| 148 | +gulpSass.sync = options => gulpSass(options, true); |
174 | 149 |
|
175 | 150 | //////////////////////////////
|
176 | 151 | // Log errors nicely
|
177 | 152 | //////////////////////////////
|
178 |
| -gulpSass.logError = function logError(error) { |
179 |
| - var message = new gutil.PluginError('sass', error.messageFormatted).toString(); |
180 |
| - process.stderr.write(message + '\n'); |
| 153 | +gulpSass.logError = (error) => { |
| 154 | + const message = new PluginError('sass', error.messageFormatted).toString(); |
| 155 | + process.stderr.write(`${message}\n`); |
181 | 156 | this.emit('end');
|
182 | 157 | };
|
183 | 158 |
|
|
0 commit comments