使用node-sass和autprefixer的npm脚本会导致浏览器同步运行两次

时间:2017-05-14 14:45:48

标签: bash node-sass autoprefixer npm-scripts

我有以下npm脚本设置来完成我的所有scss和js linting,编译和捆绑。它们确实有效,但似乎还有改进的余地。

"scripts": {
  "lint-scss": "stylelint ./styles/**/*.scss --cache --syntax scss",
  "scss": "node-sass --omit-source-map-url --recursive --output-style compressed --output ./styles ./styles",
  "autoprefixer": "postcss --use autoprefixer --replace ./styles/style.css --no-map",
  "build:css": "yarn run lint-scss --silent | yarn run scss --silent | yarn run autoprefixer --silent",
  "serve": "browser-sync start --https --no-notify --proxy 'project.local' --files './styles/**/*.css, ./views/**/*.php, ./**/*.html, !node_modules/**/*.html'",
  "watch:css": "onchange './styles/**/*.scss' -- yarn run build:css --silent",
  "watch:all": "parallelshell 'yarn run serve --silent' 'yarn run watch:css --silent'",
  "postinstall": "yarn run watch:all --silent"
}

目前,当我查看更改并保存scss文件时,浏览器同步会触发两次。这是因为node-sass正在运行并更改文件,然后autoprefixer正在运行并更改同一文件。 (完整性输出如下)

Rendering Complete, saving .css file...
Wrote CSS to /Users/Matt/Sites/Project/styles/style.css
Wrote 1 CSS files to /Users/Matt/Sites/Project/styles
[BS] File event [change] : styles/style.css
✔ Finished styles/style.css (192ms)
[BS] File event [change] : styles/style.css

当然我可以合并这些并更改文件一次并使浏览器同步一次?

由于

1 个答案:

答案 0 :(得分:0)

您可以尝试在"scripts": { ... "serve": "browser-sync start --reload-debounce 200 ...", ... } 脚本中包含BrowserSync --reload-debounce 选项/标记,并在几毫秒内提供适当的短暂延迟。

  

__attribute__((interrupt))限制浏览器:重新加载事件可以发送到连接客户端的频率

void set(IDTDescr *descriptor, void(*handler)(void), uint8_t flags)
{
    descriptor->selector = 0x08;

    uintptr_t handler_addr = (uintptr_t)handler;
    descriptor->offset_1 = handler_addr & 0xFFFF;
    descriptor->offset_2 = (handler_addr >> 16) & 0xFFFF;
    descriptor->offset_3 = (handler_addr >> 32) & 0xFFFFFFFF;

    descriptor->flags = flags;
}
void init(void)
{
    memset(descriptors, 0, sizeof(descriptors));

    //Faults:
    set(&descriptors[0x00], &DivideByZero,              IDT_PRESENT | IDT_INTERRUPT);
    set(&descriptors[0x01], &Debug,                     IDT_PRESENT | IDT_INTERRUPT);