• 使用sass开发web-components组件


    思路:借助chokidar监听变化,将scss编译成css后插入
    同时执行chokidar监听和webpack server

        "start": "npm run watch:css & webpack serve",
        "watch:css" : "node chokidarStyles.js",
    
    • 1
    • 2
    // chokidarStyles.js
    const fs = require('fs');
    const path = require('path');
    const chokidar = require('chokidar');
    const sass = require('sass');
    const autoprefixer = require('autoprefixer');
    const postcss = require('postcss');
    
    // 要监听的文件或目录路径
    const filePath = './src/styles.scss';
    const templatePath = './src/ComponentLoadingTemplate.ts';
    
    // 监听文件或目录变化
    const watcher = chokidar.watch(filePath);
    
    const changeCss = () => {
        // 编译 SCSS 文件
        const scssFilePath = path.resolve(__dirname, filePath);
        try {
            const result = sass.renderSync({file: scssFilePath, outputStyle: 'expanded'});
            const cssContent = result.css.toString();
            // 浏览器兼容
            postcss([autoprefixer({overrideBrowserslist: ['last 30 versions', '> 0.5%', 'Firefox ESR', 'not dead']})])
                .process(cssContent, {from: undefined})
                .then(_result => {
                    // 添加样式后的css
                    const prefixedCss = _result.css;
    
                    // 读取模板文件
                    const templateContent = fs.readFileSync(templatePath, 'utf-8');
    
                    const regex = /`);
                        // 更新输出文件
                        const outputFilePath = path.resolve(__dirname, templatePath);
                        fs.writeFileSync(outputFilePath, modifiedTemplateContent);
                    }
                })
                .catch(error => {
                    console.error('Error processing CSS:', error);
                });
        } catch (e) {
            console.log(e);
        }
    }
    
    // 监听文件或目录变化事件
    watcher.on('change', (path) => {
        console.log(`File ${path} has been changed`);
        changeCss();
    });
    
    watcher.on('add', (path) => {
        console.log(`File ${path} has been added`);
    });
    
    watcher.on('unlink', (path) => {
        console.log(`File ${path} has been removed`);
    });
    
    // 监听错误事件
    watcher.on('error', (error) => {
        console.error(`Watcher error: ${error}`);
    });
    
    // 在需要的时候停止监听
    // watcher.close();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    内网渗透之内网信息收集(四)
    Redis基础一(Redis认识和Redis两种安装方式)
    《深度学习进阶 自然语言处理》第一章:神经网络的复习
    2024滴滴校招面试真题汇总及其讲解(二)
    redis未授权访问漏洞的利用
    2023最新ICP备案查询系统源码 附教程 Thinkphp框架
    如何创建像 Quora 这样的问答网站:技术堆栈、用户获取等
    PHP 跟据用户IP获取所在国家高效解决方案(GEOIP)
    docker之数据卷(Data Volumes)&dockerfile
    抽象轻松的java-mybatis简单入门
  • 原文地址:https://blog.csdn.net/ligaoming_123/article/details/136192568