使用sass开发web-components组件

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

    "start": "npm run watch:css & webpack serve",
    "watch:css" : "node chokidarStyles.js",
// 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 = /<style>([\s\S]*?)<\/style>/;

                const cssText = templateContent.match(regex);

                if (cssText && cssText[1] !== prefixedCss) {
   
                    // 将 CSS 内容插入模板中
                    const modifiedTemplateContent = templateContent.replace(regex, `<style>${
     prefixedCss}</style>`);
                    // 更新输出文件
                    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. 使用sass开发web-components

    2024-02-20 20:28:02       45 阅读
  2. 鸿蒙(HarmonyOS)应用开发——web

    2024-02-20 20:28:02       64 阅读
  3. Web框架开发-Django-form

    2024-02-20 20:28:02       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 20:28:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 20:28:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 20:28:02       87 阅读
  4. Python语言-面向对象

    2024-02-20 20:28:02       96 阅读

热门阅读

  1. git_note

    2024-02-20 20:28:02       45 阅读
  2. 智能红包分配算法:实现均衡随机金额分配

    2024-02-20 20:28:02       49 阅读
  3. Android 15的开发者预览版

    2024-02-20 20:28:02       49 阅读
  4. 【工具类】非 sudo 运行 docker

    2024-02-20 20:28:02       58 阅读
  5. 关于类模板对象做函数参数的方法

    2024-02-20 20:28:02       47 阅读
  6. 关于三色标记算法

    2024-02-20 20:28:02       50 阅读
  7. 在html中target有什么作用

    2024-02-20 20:28:02       41 阅读
  8. 【leetcode】贪心算法介绍

    2024-02-20 20:28:02       53 阅读
  9. Pinia 官网速通

    2024-02-20 20:28:02       59 阅读
  10. golang 的内存分配

    2024-02-20 20:28:02       41 阅读
  11. CP AUTOSAR的信息安全机制汇总(一)

    2024-02-20 20:28:02       48 阅读