1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const { Transform } = require('stream');
|
5 | const picocolors = require('picocolors');
|
6 | const PluginError = require('plugin-error');
|
7 | const replaceExtension = require('replace-ext');
|
8 | const stripAnsi = require('strip-ansi');
|
9 | const clonedeep = require('lodash.clonedeep');
|
10 | const applySourceMap = require('vinyl-sourcemaps-apply');
|
11 |
|
12 | const PLUGIN_NAME = 'gulp-sass';
|
13 |
|
14 | const MISSING_COMPILER_MESSAGE = `
|
15 | gulp-sass no longer has a default Sass compiler; please set one yourself.
|
16 | Both the "sass" and "node-sass" packages are permitted.
|
17 | For example, in your gulpfile:
|
18 |
|
19 | const sass = require('gulp-sass')(require('sass'));
|
20 | `;
|
21 |
|
22 | const transfob = (transform) => new Transform({ transform, objectMode: true });
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const filePush = (file, sassObject, callback) => {
|
28 |
|
29 | if (sassObject.map) {
|
30 |
|
31 | const sassMap = JSON.parse(sassObject.map.toString());
|
32 |
|
33 | const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
|
34 |
|
35 | const sassFileSrc = file.relative;
|
36 |
|
37 | const sassFileSrcPath = path.dirname(sassFileSrc);
|
38 |
|
39 | if (sassFileSrcPath) {
|
40 | const sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
|
41 |
|
42 | sassMap.sources = sassMap.sources.map((source, index) => (
|
43 | index === sourceFileIndex
|
44 | ? source
|
45 | : path.join(sassFileSrcPath, source)
|
46 | ));
|
47 | }
|
48 |
|
49 |
|
50 | sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src);
|
51 |
|
52 |
|
53 | sassMap.file = replaceExtension(sassFileSrc, '.css');
|
54 |
|
55 | applySourceMap(file, sassMap);
|
56 | }
|
57 |
|
58 | file.contents = sassObject.css;
|
59 | file.path = replaceExtension(file.path, '.css');
|
60 |
|
61 | if (file.stat) {
|
62 | file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
|
63 | }
|
64 |
|
65 | callback(null, file);
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | const handleError = (error, file, callback) => {
|
72 | const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
|
73 | const relativePath = path.relative(process.cwd(), filePath);
|
74 | const message = `${picocolors.underline(relativePath)}\n${error.formatted}`;
|
75 |
|
76 | error.messageFormatted = message;
|
77 | error.messageOriginal = error.message;
|
78 | error.message = stripAnsi(message);
|
79 | error.relativePath = relativePath;
|
80 |
|
81 | return callback(new PluginError(PLUGIN_NAME, error));
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const gulpSass = (options, sync) => {
|
90 | return transfob((file, encoding, callback) => {
|
91 | if (file.isNull()) {
|
92 | callback(null, file);
|
93 | return;
|
94 | }
|
95 |
|
96 | if (file.isStream()) {
|
97 | callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
|
98 | return;
|
99 | }
|
100 |
|
101 | if (path.basename(file.path).startsWith('_')) {
|
102 | callback();
|
103 | return;
|
104 | }
|
105 |
|
106 | if (!file.contents.length) {
|
107 | file.path = replaceExtension(file.path, '.css');
|
108 | callback(null, file);
|
109 | return;
|
110 | }
|
111 |
|
112 | const opts = clonedeep(options || {});
|
113 | opts.data = file.contents.toString();
|
114 |
|
115 |
|
116 | opts.file = file.path;
|
117 |
|
118 |
|
119 | if (path.extname(file.path) === '.sass') {
|
120 | opts.indentedSyntax = true;
|
121 | }
|
122 |
|
123 |
|
124 | if (opts.includePaths) {
|
125 | if (typeof opts.includePaths === 'string') {
|
126 | opts.includePaths = [opts.includePaths];
|
127 | }
|
128 | } else {
|
129 | opts.includePaths = [];
|
130 | }
|
131 |
|
132 | opts.includePaths.unshift(path.dirname(file.path));
|
133 |
|
134 |
|
135 | if (file.sourceMap) {
|
136 | opts.sourceMap = file.path;
|
137 | opts.omitSourceMapUrl = true;
|
138 | opts.sourceMapContents = true;
|
139 | }
|
140 |
|
141 | if (sync !== true) {
|
142 | |
143 |
|
144 |
|
145 | gulpSass.compiler.render(opts, (error, obj) => {
|
146 | if (error) {
|
147 | handleError(error, file, callback);
|
148 | return;
|
149 | }
|
150 |
|
151 | filePush(file, obj, callback);
|
152 | });
|
153 | } else {
|
154 | |
155 |
|
156 |
|
157 | try {
|
158 | filePush(file, gulpSass.compiler.renderSync(opts), callback);
|
159 | } catch (error) {
|
160 | handleError(error, file, callback);
|
161 | }
|
162 | }
|
163 | });
|
164 | };
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | gulpSass.sync = (options) => gulpSass(options, true);
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | gulpSass.logError = function logError(error) {
|
175 | const message = new PluginError('sass', error.messageFormatted).toString();
|
176 | process.stderr.write(`${message}\n`);
|
177 | this.emit('end');
|
178 | };
|
179 |
|
180 | module.exports = (compiler) => {
|
181 | if (!compiler || !compiler.render) {
|
182 | const message = new PluginError(
|
183 | PLUGIN_NAME,
|
184 | MISSING_COMPILER_MESSAGE,
|
185 | { showProperties: false },
|
186 | ).toString();
|
187 | process.stderr.write(`${message}\n`);
|
188 | process.exit(1);
|
189 | }
|
190 |
|
191 | gulpSass.compiler = compiler;
|
192 | return gulpSass;
|
193 | };
|