UNPKG

6.02 kBJavaScriptView Raw
1///@ts-check
2"use strict";
3var path = require("path");
4var gulp = require("gulp");
5var sourcemaps = require("gulp-sourcemaps");
6const rename = require("gulp-rename");
7
8const sass = require("../lib/sass");
9const cleanCSS = require("gulp-clean-css");
10var inline = require("../lib/inline");
11var empty = require("../lib/empty");
12var wxssImporter = require("../lib/wxss-importer");
13var replace = require("../lib/multi-replace");
14var error = require("../log/error");
15const debug = require("../log/compile");
16const size = require("../log/size");
17var TITLE = "wxss:";
18/**
19 * 编译scss
20 * @param {object} config
21 * @param {string|string[]} scssFile 编译源
22 * @returns {NodeJS.ReadWriteStream}
23 */
24function compileScss(config, scssFile) {
25 // var postCssPlgins = [
26 // // autoprefixer({
27 // // browsers: [
28 // // // ios
29 // // "iOS >= 8",
30 // // // android
31 // // "ChromeAndroid >= 53",
32 // // ],
33 // // }),
34 // ];
35 // if (config.release) {
36 // postCssPlgins.push(cssnano());
37 // }
38 return gulp
39 .src(scssFile, { base: config.src })
40 .pipe(config.release ? empty() : sourcemaps.init())
41 .pipe(
42 debug({
43 title: TITLE,
44 // dist: config.dist,
45 distExt: ".wxss",
46 }),
47 )
48 .pipe(
49 sass({
50 ///@ts-ignore
51 importer: wxssImporter,
52 // functions: {
53 // 'import-wxss($path)': function (path) { return '@import "' + path + '"' }
54 // },
55 errLogToConsole: true,
56 outputStyle: "expanded",
57 includePaths: [path.join(config.src, config.assets || "./")],
58 }),
59 )
60 .on("error", error("wxss"))
61 // .pipe(
62 // replace(/@import url\(["']?([\w\/\.\-\_]*)["']?\)/g, ($1, $2) => {
63 // return '@import "' + $2 + '"';
64 // }),
65 // )
66 .pipe(inline(config))
67 .pipe(
68 cleanCSS({
69 inline: ['none'],
70 format: config.release ? "minify" : "beautify",
71 level: {
72 1: {
73 all: true,
74 specialComments: config.release ? 0 : "all",
75 },
76 2: {
77 restructureRules: true, // controls rule restructuring;
78 },
79 },
80 compatibility: {
81 colors: {
82 opacity: true, // controls `rgba()` / `hsla()` color support
83 },
84 properties: {
85 backgroundClipMerging: true, // controls background-clip merging into shorthand
86 backgroundOriginMerging: true, // controls background-origin merging into shorthand
87 backgroundSizeMerging: true, // controls background-size merging into shorthand
88 colors: true, // controls color optimizations
89 ieBangHack: false, // controls keeping IE bang hack
90 ieFilters: false, // controls keeping IE `filter` / `-ms-filter`
91 iePrefixHack: false, // controls keeping IE prefix hack
92 ieSuffixHack: false, // controls keeping IE suffix hack
93 merging: true, // controls property merging based on understandability
94 shorterLengthUnits: false, // controls shortening pixel units into `pc`, `pt`, or `in` units
95 spaceAfterClosingBrace: false, // controls keeping space after closing brace - `url() no-repeat` into `url()no-repeat`
96 urlQuotes: true, // controls keeping quoting inside `url()`
97 zeroUnits: true, // controls removal of units `0` value
98 },
99 selectors: {
100 adjacentSpace: false, // controls extra space before `nav` element
101 ie7Hack: false, // controls removal of IE7 selector hacks, e.g. `*+html...`
102 // mergeablePseudoClasses: [":active"], // controls a whitelist of mergeable pseudo classes
103 // mergeablePseudoElements: ["::after"], // controls a whitelist of mergeable pseudo elements
104 mergeLimit: 8191, // controls maximum number of selectors in a single rule (since 4.1.0)
105 multiplePseudoMerging: true, // controls merging of rules with multiple pseudo classes / elements (since 4.1.0)
106 },
107 units: {
108 ch: false, // controls treating `ch` as a supported unit
109 in: false, // controls treating `in` as a supported unit
110 pc: false, // controls treating `pc` as a supported unit
111 pt: false, // controls treating `pt` as a supported unit
112 rem: true, // controls treating `rem` as a supported unit
113 vh: true, // controls treating `vh` as a supported unit
114 vm: true, // controls treating `vm` as a supported unit
115 vmax: true, // controls treating `vmax` as a supported unit
116 vmin: true, // controls treating `vmin` as a supported unit
117 rpx: true, // controls treating `vmin` as a supported unit
118 },
119 },
120 }),
121 )
122 .pipe(
123 replace(/@import url\(["']?([\w\/\.\-\_]*)["']?\)/g, ($1, $2) => {
124 return '@import "' + $2 + '"';
125 }),
126 )
127 .pipe(config.release ? empty() : sourcemaps.write())
128 .pipe(rename({ extname: ".wxss" }))
129 .pipe(gulp.dest(config.dist))
130 .pipe(size({ title: TITLE, showFiles: true }));
131}
132
133module.exports = compileScss;