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