EasySass插件修复:s[ac]ss文件中使用import时候导致多次包含
1281 浏览2023年09月22日作者:Cellent

之前发表了一篇文章介绍关于小程序原生开发怎么使用less/sass的方法,原文链接


在之后的使用中发现这个vscode插件有一个bug:使用import语法的时候会导致其他的sass/scss文件被包含到当前文件(可能在某些场景下这是符合常理的,但是在小程序开发中这非常不合理)


因为作者已经很久没有维护这个插件了,所以我们只能自己动手修复一下:

这里还是先找到这个插件的位置(Mac)(Windows文件目录参考上方链接文章):/users/用户名/.vscode/extensions/spook.easysass-0.0.6

将整个项目拖入编辑器并找到extensions.js,添加以下代码:


  1. CompileSassExtension 方法外部声明两个变量 refContent restContent


  1. 121行的代码 增加以下两部分,具体逻辑是先将所有的 import语句 暂存,编译完scss后再将import语句放回文件最上方

这一步可以直接复制下面这段代码替换原来的 立即执行函数

// Using closure to properly pass local variables to callback
(function (path_, targetPath_, style_) {

    // fix compile error by "import"
    const fileContent = fs.readFileSync(path, "utf-8");
    fileContent.replace(/(^[\s\n]*?(@import.*?;\n?)*)(.*)/sg, (_, ref, __, rest) => {
        refContent = ref.replace(/\.s[ca]ss/g, format.extension);
        restContent = rest;
        // remove space and wrap line when mode(format.format) is "compressed"
        if (style == compileSass.Sass.style.compressed) {
            refContent = refContent.replace(/\s{2,}/g, " ").replace(/\n/sg, "");
        }
    });
    fs.writeFileSync(path_, restContent);

    compileSass(path_, { style: style_ }, function (result) {
        handleResult(targetPath_, result);

        // restore original file's content
        fs.writeFileSync(path_, fileContent);
    });

})(path, targetPath, style);


ok!现在又可以继续使用easy sass来编译sass/scss代码了!

不过更推荐的方法是使用 gulp/webpack 等打包工具配置sass的编译

最后一次编辑于 2024 年 03 月 28 日
1 条评论

相关文章

专题推荐

热门文章

热门问答