1 |
|
2 | "use strict";
|
3 | const path = require('path');
|
4 | const chalk = require('ansi-colors');
|
5 | const through = require('through2');
|
6 |
|
7 | const PluginError = require('./error');
|
8 |
|
9 | const EXT_NAME = ".wxss";
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function replaceExtension(file, ext) {
|
16 | return path.join(path.dirname(file), path.basename(file, path.extname(file)) + ext);
|
17 | }
|
18 | const PLUGIN_NAME = 'sass';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const gulpSass = (options, sync) => through.obj((file, enc, cb) => {
|
24 | if (file.isNull()) {
|
25 | return cb(null, file);
|
26 | }
|
27 |
|
28 | if (file.isStream()) {
|
29 | return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
|
30 | }
|
31 |
|
32 | if (path.basename(file.path).indexOf('_') === 0) {
|
33 | return cb();
|
34 | }
|
35 |
|
36 | if (!file.contents.length) {
|
37 | file.path = replaceExtension(file.path, EXT_NAME);
|
38 | return cb(null, file);
|
39 | }
|
40 |
|
41 | const opts = Object.assign({}, options || {});
|
42 | opts.data = file.contents.toString();
|
43 |
|
44 |
|
45 | opts.file = file.path;
|
46 |
|
47 |
|
48 | if (path.extname(file.path) === '.sass') {
|
49 | opts.indentedSyntax = true;
|
50 | }
|
51 |
|
52 |
|
53 | if (opts.includePaths) {
|
54 | if (typeof opts.includePaths === 'string') {
|
55 | opts.includePaths = [opts.includePaths];
|
56 | } else {
|
57 | opts.includePaths = opts.includePaths.map(e => e)
|
58 | }
|
59 | } else {
|
60 | opts.includePaths = [];
|
61 | }
|
62 |
|
63 | opts.includePaths.unshift(path.dirname(file.path));
|
64 |
|
65 |
|
66 | if (file.sourceMap) {
|
67 | opts.sourceMap = true;
|
68 | opts.omitSourceMapUrl = true;
|
69 | opts.sourceMapContents = true;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | const filePush = (sassObj) => {
|
76 | let sassMap;
|
77 | let sassMapFile;
|
78 | let sassFileSrc;
|
79 | let sassFileSrcPath;
|
80 | let sourceFileIndex;
|
81 |
|
82 |
|
83 | if (sassObj.map) {
|
84 |
|
85 | sassMap = JSON.parse(sassObj.map.toString());
|
86 |
|
87 | sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
|
88 |
|
89 | sassFileSrc = file.relative;
|
90 |
|
91 | sassFileSrcPath = path.dirname(sassFileSrc);
|
92 | if (sassFileSrcPath) {
|
93 |
|
94 | sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
|
95 | sassMap.sources = sassMap.sources.map((source, index) => {
|
96 | return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source);
|
97 | });
|
98 | }
|
99 |
|
100 |
|
101 | sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);
|
102 |
|
103 |
|
104 | sassMap.file = replaceExtension(sassFileSrc, EXT_NAME);
|
105 |
|
106 |
|
107 | }
|
108 |
|
109 | file.contents = sassObj.css;
|
110 | file.path = replaceExtension(file.path, EXT_NAME);
|
111 |
|
112 | cb(null, file);
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | const errorM = (error) => {
|
119 | const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
|
120 | const relativePath = path.relative(process.cwd(), filePath);
|
121 | const message = [chalk.underline(relativePath), error.formatted].join('\n');
|
122 |
|
123 | error.messageFormatted = message;
|
124 | error.messageOriginal = error.message;
|
125 | error.message = chalk.unstyle(message);
|
126 | error.relativePath = relativePath;
|
127 |
|
128 | return cb(new PluginError(PLUGIN_NAME, error));
|
129 | };
|
130 |
|
131 | if (sync !== true) {
|
132 |
|
133 |
|
134 |
|
135 | const callback = (error, obj) => {
|
136 | if (error) {
|
137 | return errorM(error);
|
138 | }
|
139 | filePush(obj);
|
140 | };
|
141 |
|
142 | gulpSass.compiler.render(opts, callback);
|
143 | } else {
|
144 |
|
145 |
|
146 |
|
147 | try {
|
148 | filePush(gulpSass.compiler.renderSync(opts));
|
149 | } catch (error) {
|
150 | return errorM(error);
|
151 | }
|
152 | }
|
153 | });
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | gulpSass.sync = options => gulpSass(options, true);
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | gulpSass.logError = function logError(error) {
|
164 | const message = new PluginError('sass', error.messageFormatted).toString();
|
165 | process.stderr.write(`${message}\n`);
|
166 |
|
167 | this.emit('end');
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | gulpSass.compiler = require('sass');
|
174 |
|
175 | module.exports = gulpSass;
|