UNPKG

6.08 kBJavaScriptView Raw
1///@ts-check
2"use strict";
3var path = require("path");
4var gulp = require("gulp");
5// var sourcemaps = require("gulp-sourcemaps");
6// const rename = require("gulp-rename");
7
8const sass = require("../lib/sass");
9const cleanCSS = require("../lib/clean-css");
10var inline = require("../lib/inline");
11// var 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 sourceMap: false,
70 inline: ['none'],
71 format: config.release ? "minify" : "beautify",
72 level: {
73 1: {
74 all: true,
75 specialComments: config.release ? 0 : "all",
76 },
77 2: {
78 restructureRules: true, // controls rule restructuring;
79 },
80 },
81 compatibility: {
82 colors: {
83 opacity: true, // controls `rgba()` / `hsla()` color support
84 },
85 properties: {
86 backgroundClipMerging: true, // controls background-clip merging into shorthand
87 backgroundOriginMerging: true, // controls background-origin merging into shorthand
88 backgroundSizeMerging: true, // controls background-size merging into shorthand
89 colors: true, // controls color optimizations
90 ieBangHack: false, // controls keeping IE bang hack
91 ieFilters: false, // controls keeping IE `filter` / `-ms-filter`
92 iePrefixHack: false, // controls keeping IE prefix hack
93 ieSuffixHack: false, // controls keeping IE suffix hack
94 merging: true, // controls property merging based on understandability
95 shorterLengthUnits: false, // controls shortening pixel units into `pc`, `pt`, or `in` units
96 spaceAfterClosingBrace: false, // controls keeping space after closing brace - `url() no-repeat` into `url()no-repeat`
97 urlQuotes: true, // controls keeping quoting inside `url()`
98 zeroUnits: true, // controls removal of units `0` value
99 },
100 selectors: {
101 adjacentSpace: false, // controls extra space before `nav` element
102 ie7Hack: false, // controls removal of IE7 selector hacks, e.g. `*+html...`
103 // mergeablePseudoClasses: [":active"], // controls a whitelist of mergeable pseudo classes
104 // mergeablePseudoElements: ["::after"], // controls a whitelist of mergeable pseudo elements
105 mergeLimit: 8191, // controls maximum number of selectors in a single rule (since 4.1.0)
106 multiplePseudoMerging: true, // controls merging of rules with multiple pseudo classes / elements (since 4.1.0)
107 },
108 units: {
109 ch: false, // controls treating `ch` as a supported unit
110 in: false, // controls treating `in` as a supported unit
111 pc: false, // controls treating `pc` as a supported unit
112 pt: false, // controls treating `pt` as a supported unit
113 rem: true, // controls treating `rem` as a supported unit
114 vh: true, // controls treating `vh` as a supported unit
115 vm: true, // controls treating `vm` as a supported unit
116 vmax: true, // controls treating `vmax` as a supported unit
117 vmin: true, // controls treating `vmin` as a supported unit
118 rpx: true, // controls treating `vmin` as a supported unit
119 },
120 },
121 }),
122 )
123 .pipe(
124 replace(/@import url\(["']?([\w\/\.\-\_]*)["']?\)/g, ($1, $2) => {
125 return '@import "' + $2 + '"';
126 }),
127 )
128 // .pipe(config.release ? empty() : sourcemaps.write())
129 // .pipe(rename({ extname: ".wxss" }))
130 .pipe(gulp.dest(config.dist))
131 .pipe(size({ title: TITLE, showFiles: true }));
132}
133
134module.exports = compileScss;