使用 Webpack Dev Server 忽略文件

Ignoring files with Webpack Dev Server

我正在使用 Emacs 编辑 Webpack 开发服务器监视的目录中的文件。每次我对文件进行更改时,都会在同一目录中创建一个备份文件,例如 .#original_filename,即使我没有在 Emacs 中保存更改。这会导致服务器的观察者注册更改,即使我没有进行更改。因此,每次我在文件中进行更改时服务器都会重新加载,然后在我保存它时再次重新加载。

这有点混乱且耗时。查看 Webpack's documentation,我发现了以下选项:

For some systems, watching many file systems can result in a lot of CPU or memory usage. It is possible to exclude a huge folder like node_modules:

ignored: /node_modules/ 

It is also possible to use anymatch patterns:

ignored: "files/**/*.js"

所以我像下面这样修改了我的配置,以匹配并忽略以 .:

开头的文件
devServer: {
    ...
    watchOptions: {
        ignored: './src/app/**/.*',
    },
    ...
}

我重新启动了开发服务器,但观察者仍然将备份文件注册为对代码库所做的更改。我做错了什么?

我尝试了几次后找到了一个可行的解决方案。由于某些原因,当开头有./时它不起作用,这应该只是表示当前目录。

将匹配模式更改为以下作品:

watchOptions: {
    ignored: '**/.*',
},


正确的方法是使用节点的本地模块路径
您的 webpack.config.js 应该看起来像这样:

const path = require('path')

module.exports = {
  ...
  devServer: {
    watchOptions: {
      ignored: [
        path.resolve(__dirname, 'dist'),
        path.resolve(__dirname, 'node_modules')
      ]
    }
  },
  ...
}

另一种选择是将 Emacs 配置为将其备份文件写入其他位置。无论如何,这通常是一件好事。

配置window-setup-hook

;; deleting old backups
(defun my-delete-old-backups ()
  (let* ((backup-directory (expand-file-name
                            (concat user-emacs-directory "backups")))
         (auto-save-directory (concat backup-directory "/\1")))
    (setq backup-by-copying t)      ; don't clobber symlinks
    ;; Write backup files to own directory
    (setq backup-directory-alist `(("." . ,backup-directory)))
    ;; auto-save files to same backup-directory
    (setq auto-save-file-name-transforms `((".*" ,auto-save-directory t)))
    ;; Make backups of files, even when they're in version control
    (setq vc-make-backup-files t)
    (setq delete-old-versions t)
    (message "Deleting old backup files...")
    (let ((week (* 60 60 24 7))
          (current (float-time (current-time))))
      (dolist (file (directory-files backup-directory t)) ;
        (when (and (backup-file-name-p file)
                   (> (- current (float-time (fifth (file-attributes file))))
                      week))
          (message "%s" file)
          (delete-file file))))
    (message "Deleting old backup files...done")
    (message nil)))
(add-hook 'window-setup-hook #'my-delete-old-backups)

提取出的重要位:

(setq backup-directory (expand-file-name
                            (concat user-emacs-directory "backups"))
(setq backup-directory-alist `(("." . ,backup-directory)))