                            return t))))
       (ignore (print! (green "Doom package autoloads is up-to-date"))
               (doom-initialize-autoloads doom-package-autoload-file))
-    (doom-delete-autoloads-file doom-package-autoload-file)
-    (with-temp-file doom-package-autoload-file
-      (insert ";;; -*- lexical-binding:t -*-\n"
-              ";; This file is autogenerated by `doom//reload-package-autoloads', DO NOT EDIT !!\n\n")
-      (let (case-fold-search)
+    (let (case-fold-search)
+      (doom-delete-autoloads-file doom-package-autoload-file)
+      (with-temp-file doom-package-autoload-file
+        (doom--generate-header 'doom//reload-package-autoloads)
         (save-excursion
           ;; Cache the important and expensive-to-initialize state here.
-          (doom-initialize-packages)
-          (prin1 `(setq load-path ',load-path
-                        auto-mode-alist ',auto-mode-alist
-                        Info-directory-list ',Info-directory-list
-                        doom-disabled-packages ',doom-disabled-packages
-                        package-activated-list ',package-activated-list)
-                 (current-buffer))
+          (doom--generate-var-cache)
           (print! (green "✓ Cached package state"))
-          ;; insert package autoloads
-          (dolist (spec (doom-get-package-alist))
-            (if-let* ((pkg  (car spec))
-                      (desc (cdr spec)))
-                (unless (memq pkg doom-autoload-excluded-packages)
-                  (let ((file (concat (package--autoloads-file-name desc) ".el")))
-                    (when (file-exists-p file)
-                      (insert "(let ((load-file-name " (prin1-to-string (abbreviate-file-name file)) "))\n")
-                      (insert-file-contents file)
-                      (while (re-search-forward "^\\(?:;;\\(.*\n\\)\\|\n\\|(provide '[^\n]+\\)" nil t)
-                        (unless (nth 8 (syntax-ppss))
-                          (replace-match "" t t)))
-                      (unless (bolp) (insert "\n"))
-                      (insert ")\n"))))
-              (print! (yellow "⚠ Couldn't find package desc for %s" (car spec))))))
-        (print! (green "✓ Package autoloads included"))
+          ;; Loop through packages and concatenate all their autoloads files.
+          (doom--generate-package-autoloads)
+          (print! (green "✓ Package autoloads included")))
         ;; Remove `load-path' and `auto-mode-alist' modifications (most of them,
         ;; at least); they are cached later, so all those membership checks are
         ;; unnecessary overhead.
-        (while (re-search-forward "^\\s-*\\((\\(?:add-to-list\\|\\(?:when\\|if\\) (boundp\\)\\s-+'\\(?:load-path\\|auto-mode-alist\\)\\)" nil t)
-          (goto-char (match-beginning 1))
-          (kill-sexp))
+        (doom--cleanup-package-autoloads)
         (print! (green "✓ Removed load-path/auto-mode-alist entries"))))
     (doom--byte-compile-file doom-package-autoload-file)
     (when (and noninteractive (not (daemonp)))

it exists."
  (cl-check-type file string)
  (when (file-exists-p file)
    (delete-file file)
    (ignore-errors (delete-file (byte-compile-dest-file file)))
    (message "Deleted old %s" (file-name-nondirectory file))))

This should be run whenever your `doom!' block, or a module autoload file, is
modified."
  (interactive)
  (let* ((default-directory doom-emacs-dir)
         (doom-modules (doom-modules))
         (targets
          (file-expand-wildcards
           (expand-file-name "autoload/*.el" doom-core-dir)))
         (enabled-targets (copy-sequence targets))
         case-fold-search)
    (dolist (path (doom-module-load-path t))
      (let* ((auto-dir  (expand-file-name "autoload" path))
             (auto-file (expand-file-name "autoload.el" path))
             (module    (doom-module-from-path auto-file))
             (module-p  (or (doom-module-p (car module) (cdr module))
                            (file-equal-p path doom-private-dir))))
        (when (file-exists-p auto-file)
          (push auto-file targets)
          (if module-p (push auto-file enabled-targets)))
        (dolist (file (doom-files-in auto-dir :match "\\.el$" :full t))
          (push file targets)
          (if module-p (push file enabled-targets)))))
    (if (and (not force-p)
             (not doom-emacs-changed-p)
             (file-exists-p doom-autoload-file)
