diff --git a/package.json b/package.json index a896da6..c739c5b 100644 --- a/package.json +++ b/package.json @@ -55,18 +55,16 @@ "del": "^4.1.1", "gulp": "^4.0.2", "gulp-better-rollup": "^4.0.1", - "gulp-clean-css": "^4.0.0", "gulp-rename": "^1.4.0", "gulp-sourcemaps": "^2.6.5", "gulp-typescript": "^5.0.1", "imagemin": "^6.1.0", "json5": "^2.1.0", - "mime": "^2.4.2", - "rollup": "^1.11.3", + "mime": "^2.4.3", + "rollup": "^1.12.2", "sass": "^1.20.1", "through2": "^3.0.1", "ts-transform-paths": "^2.0.0", - "vinyl-sourcemaps-apply": "^0.2.1", "yargs": "^13.2.4" }, "optionalDependencies": { @@ -87,4 +85,4 @@ "tslib": "^1.9.3", "typescript": "^3.4.3" } -} \ No newline at end of file +} diff --git a/src/compiler/compile-wxss.js b/src/compiler/compile-wxss.js index 249878c..1f5f6ef 100644 --- a/src/compiler/compile-wxss.js +++ b/src/compiler/compile-wxss.js @@ -2,11 +2,11 @@ "use strict"; var path = require("path"); var gulp = require("gulp"); -var sourcemaps = require("gulp-sourcemaps"); +// var sourcemaps = require("gulp-sourcemaps"); const rename = require("gulp-rename"); const sass = require("../lib/sass"); -const cleanCSS = require("gulp-clean-css"); +const cleanCSS = require("../lib/clean-css"); var inline = require("../lib/inline"); var empty = require("../lib/empty"); var wxssImporter = require("../lib/wxss-importer"); @@ -37,7 +37,7 @@ function compileScss(config, scssFile) { // } return gulp .src(scssFile, { base: config.src }) - .pipe(config.release ? empty() : sourcemaps.init()) + // .pipe(config.release ? empty() : sourcemaps.init()) .pipe( debug({ title: TITLE, @@ -66,6 +66,7 @@ function compileScss(config, scssFile) { .pipe(inline(config)) .pipe( cleanCSS({ + sourceMap: false, inline: ['none'], format: config.release ? "minify" : "beautify", level: { @@ -124,7 +125,7 @@ function compileScss(config, scssFile) { return '@import "' + $2 + '"'; }), ) - .pipe(config.release ? empty() : sourcemaps.write()) + // .pipe(config.release ? empty() : sourcemaps.write()) .pipe(rename({ extname: ".wxss" })) .pipe(gulp.dest(config.dist)) .pipe(size({ title: TITLE, showFiles: true })); diff --git a/src/lib/clean-css.js b/src/lib/clean-css.js new file mode 100644 index 0000000..dcd0f92 --- /dev/null +++ b/src/lib/clean-css.js @@ -0,0 +1,70 @@ +///@ts-check +"use strict"; +const path = require('path'); +const through = require('through2'); + +// const applySourceMap = require('vinyl-sourcemaps-apply'); +const CleanCSS = require('../../vendor/clean-css/clean'); +const PluginError = require('./error'); + +module.exports = (options, callback) => { + + let _callback = callback || (o => undefined); + + return through.obj(function (file, enc, cb) { + + let _options = Object.assign({}, options || {}); + + if (file.isNull()) { + return cb(null, file); + } + if (file.isStream()) { + this.emit('error', new PluginError('gulp-clean-css', 'Streaming not supported!')); + return cb(null, file); + } + + if (file.sourceMap) { + _options.sourceMap = JSON.parse(JSON.stringify(file.sourceMap)); + } + + const content = { + [file.path]: {styles: file.contents ? file.contents.toString() : ''} + }; + if (!_options.rebaseTo && _options.rebase !== false) { + _options.rebaseTo = path.dirname(file.path); + } + + // @ts-ignore + new CleanCSS(_options).minify(content, (errors, css) => { + + if (errors) { + return cb(errors.join(' ')); + } + + let details = { + 'stats': css.stats, + 'errors': css.errors, + 'warnings': css.warnings, + 'path': file.path, + 'name': file.path.split(file.base)[1] + }; + + if (css.sourceMap) { + details['sourceMap'] = css.sourceMap; + } + _callback(details); + + file.contents = Buffer.from(css.styles); + + if (css.sourceMap) { + const iMap = JSON.parse(css.sourceMap); + const oMap = Object.assign({}, iMap, { + file: path.relative(file.base, file.path), + sources: iMap.sources.map(mapSrc => path.relative(file.base, mapSrc)) + }); + // applySourceMap(file, oMap); + } + cb(null, file); + }); + }); +}; diff --git a/src/lib/sass.js b/src/lib/sass.js index 3afac75..0644e3d 100644 --- a/src/lib/sass.js +++ b/src/lib/sass.js @@ -3,7 +3,7 @@ const path = require('path'); const chalk = require('ansi-colors'); const through = require('through2'); -const applySourceMap = require('vinyl-sourcemaps-apply'); +// const applySourceMap = require('vinyl-sourcemaps-apply'); const PluginError = require('./error'); /** @@ -102,7 +102,7 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d // Replace the map file with the original file name (but new extension) sassMap.file = replaceExtension(sassFileSrc, '.css'); // Apply the map - applySourceMap(file, sassMap); + // applySourceMap(file, sassMap); } file.contents = sassObj.css; // eslint-disable-line no-param-reassign diff --git a/test-empty/src/app.js b/test-empty/src/app.js index 6241c06..e69de29 100644 --- a/test-empty/src/app.js +++ b/test-empty/src/app.js @@ -1 +0,0 @@ -App({}) diff --git a/test-empty/src/pages/index.wxss b/test-empty/src/pages/index.wxss new file mode 100644 index 0000000..31647bb --- /dev/null +++ b/test-empty/src/pages/index.wxss @@ -0,0 +1,3 @@ +.a { + border: 2rpx solid green; +} diff --git a/test/src/wxss/inline.scss b/test/src/wxss/inline.scss new file mode 100644 index 0000000..3424eeb --- /dev/null +++ b/test/src/wxss/inline.scss @@ -0,0 +1,6 @@ +.b{ + padding-top: 0%; + padding-left: 0%; + padding-right: 0%; + padding-bottom: 0%; +} \ No newline at end of file