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');
|
9 |
|
10 | const PLUGIN_NAME = 'gulp-sass';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const gulpSass = (options, sync) => through.obj((file, enc, cb) => {
|
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');
|
30 | return cb(null, file);
|
31 | }
|
32 |
|
33 | const opts = clonedeep(options || {});
|
34 | opts.data = file.contents.toString();
|
35 |
|
36 |
|
37 | opts.file = file.path;
|
38 |
|
39 |
|
40 | if (path.extname(file.path) === '.sass') {
|
41 | opts.indentedSyntax = true;
|
42 | }
|
43 |
|
44 |
|
45 | if (opts.includePaths) {
|
46 | if (typeof opts.includePaths === 'string') {
|
47 | opts.includePaths = [opts.includePaths];
|
48 | }
|
49 | } else {
|
50 | opts.includePaths = [];
|
51 | }
|
52 |
|
53 | opts.includePaths.unshift(path.dirname(file.path));
|
54 |
|
55 |
|
56 | if (file.sourceMap) {
|
57 | opts.sourceMap = file.path;
|
58 | opts.omitSourceMapUrl = true;
|
59 | opts.sourceMapContents = true;
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | const filePush = (sassObj) => {
|
66 | let sassMap;
|
67 | let sassMapFile;
|
68 | let sassFileSrc;
|
69 | let sassFileSrcPath;
|
70 | let sourceFileIndex;
|
71 |
|
72 |
|
73 | if (sassObj.map) {
|
74 |
|
75 | sassMap = JSON.parse(sassObj.map.toString());
|
76 |
|
77 | sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
|
78 |
|
79 | sassFileSrc = file.relative;
|
80 |
|
81 | sassFileSrcPath = path.dirname(sassFileSrc);
|
82 | if (sassFileSrcPath) {
|
83 |
|
84 | sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
|
85 | sassMap.sources = sassMap.sources.map((source, index) => {
|
86 | return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source);
|
87 | });
|
88 | }
|
89 |
|
90 |
|
91 | sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);
|
92 |
|
93 |
|
94 | sassMap.file = replaceExtension(sassFileSrc, '.css');
|
95 |
|
96 | applySourceMap(file, sassMap);
|
97 | }
|
98 |
|
99 | file.contents = sassObj.css;
|
100 | file.path = replaceExtension(file.path, '.css');
|
101 |
|
102 | if (file.stat) {
|
103 | file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
|
104 | }
|
105 |
|
106 | cb(null, file);
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | const errorM = (error) => {
|
113 | const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
|
114 | const relativePath = path.relative(process.cwd(), filePath);
|
115 | const message = [chalk.underline(relativePath), error.formatted].join('\n');
|
116 |
|
117 | error.messageFormatted = message;
|
118 | error.messageOriginal = error.message;
|
119 | error.message = stripAnsi(message);
|
120 | error.relativePath = relativePath;
|
121 |
|
122 | return cb(new PluginError(PLUGIN_NAME, error));
|
123 | };
|
124 |
|
125 | if (sync !== true) {
|
126 |
|
127 |
|
128 |
|
129 | const callback = (error, obj) => {
|
130 | if (error) {
|
131 | return errorM(error);
|
132 | }
|
133 | filePush(obj);
|
134 | };
|
135 |
|
136 | gulpSass.compiler.render(opts, callback);
|
137 | } else {
|
138 |
|
139 |
|
140 |
|
141 | try {
|
142 | filePush(gulpSass.compiler.renderSync(opts));
|
143 | } catch (error) {
|
144 | return errorM(error);
|
145 | }
|
146 | }
|
147 | });
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | gulpSass.sync = options => gulpSass(options, true);
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | gulpSass.logError = function logError(error) {
|
158 | const message = new PluginError('sass', error.messageFormatted).toString();
|
159 | process.stderr.write(`${message}\n`);
|
160 | this.emit('end');
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | gulpSass.compiler = require('node-sass');
|
167 |
|
168 | module.exports = gulpSass;
|