Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

随手写了一个gulp插件 #24

Open
raclen opened this issue Sep 15, 2019 · 0 comments
Open

随手写了一个gulp插件 #24

raclen opened this issue Sep 15, 2019 · 0 comments
Labels
博客搬迁 以前博客的文章迁移过来

Comments

@raclen
Copy link
Owner

raclen commented Sep 15, 2019

作用就是把px转换成rem,今天加班收获不少O(∩_∩)O哈哈~

image

var through = require('through2');
module.exports = function () {
    return through.obj(function (file, enc, cb) {
        if (file.isNull()) {
            this.push(file);
            return cb();
        }

        if (file.isStream()) {
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
            return cb();
        }
        var reg=/(\d+)px/ig;
        function med(){
            var s1=arguments[1];
            return parseFloat(s1)/100+'rem'
        }
        var content=file.contents.toString().replace(reg,med);
        file.contents = new Buffer(content);

        this.push(file);

        cb();
    });
};

file.contents.toString()这句话的意思就是获取文件的内容,然后转换成字符串

放在这https://github.com/ralcen/jsCode/blob/master/gulp_bulid/gulp-pxtworem.js
为了匹配小数的情况,更新了一下正则表达式
var reg = /(\d+\.\d+)px|(\d+)px/ig;
med函数也有所更改,具体看github里面的代码

#2015-07-12

@raclen raclen added the 博客搬迁 以前博客的文章迁移过来 label Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
博客搬迁 以前博客的文章迁移过来
Projects
None yet
Development

No branches or pull requests

1 participant