逐步移除代码库中的 jQuery 依赖
盘点 jQuery 调用与插件依赖,按文件替换为原生 API,并验证交互行为和构建体积变化。
完整提示词
<context>
Removing jQuery is mostly about the parts that are not jQuery itself. Core calls map cleanly to native APIs (`querySelectorAll`, `classList`, `addEventListener`, `fetch`, `closest`, `dataset`), but the differences are where bugs hide: `$()` silently does nothing on an empty set while native code throws on `null`; `.on()` with a selector is event delegation and needs `event.target.closest()`; jQuery's `.ajax` rejects on HTTP errors and `fetch` does not; `.data()` caches and type-converts values while `dataset` returns strings; `.html()` and `.append()` with strings execute scripts and create XSS risk the same way `innerHTML` does; `.show()`/`.hide()` remember the previous display value; animations become CSS transitions or the Web Animations API; and `$(document).ready` becomes `DOMContentLoaded` or `defer`. The real blockers are plugins (date pickers, sliders, validation, DataTables) and third-party or CMS scripts that expect a global `$`. Those need a replacement library or a decision to keep jQuery on specific pages, and the global must stay until nothing on the page uses it.
</context>
<task>
Remove jQuery from: {{scope}}.
{{#notes}}
Notes:
{{notes}}
{{/notes}}
1. Inventory every jQuery use: script tags and imports, `$`/`jQuery` calls grouped by API (selectors, events and delegation, AJAX, DOM manipulation, effects, utilities like `$.each` and `$.extend`), every plugin, and any third-party script that relies on the global.
2. Record the baseline: which pages use which code, existing tests, and a manual check list of interactive behaviours (forms, menus, modals, validation, AJAX calls).
3. Write the plan: core API replacements, a replacement or keep decision for each plugin (name candidates, but do not install anything without approval), and the removal order (smallest, most isolated files first).
4. Replace calls file by file, keeping behaviour: null checks where `$()` was tolerant, delegated events with `closest()`, `fetch` with explicit `response.ok` checks and the same request format (form-encoded versus JSON), string values from `dataset` converted where needed.
5. Use `textContent` or DOM building instead of HTML strings where the content includes user data; where HTML strings are needed, keep the existing sanitising.
6. Remove the jQuery script or import only from bundles or pages where nothing, including third-party scripts, still needs it.
7. Run tests and the manual check list in each supported browser available, and compare bundle size before and after.
</task>
<constraints>
- Behaviour must stay the same; note any intentional difference.
- Do not introduce a new framework or a large utility library to replace jQuery without approval.
- Do not remove the global `$` while a plugin, inline script or third-party widget still needs it.
- Do not add `innerHTML` with unescaped user data.
{{> guardrails/no-hardcoding-to-pass-tests}}
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>
<output_format>
## Inventory
Counts by API group, plugin list, third-party dependencies on the global.
## Plan
Table: plugin or feature, decision (native, replacement, keep for now), reason.
## Changes made
Per file, with the trickier behaviour differences called out.
## Verification
Tests and manual checks per page and browser; bundle size before and after.
## Remaining jQuery
Where it is still loaded and what would remove it.
</output_format>可替换内容
- {{scope}}
- {{#notes}}
- {{notes}}
- {{/notes}}
- {{> guardrails/no-hardcoding-to-pass-tests}}
- {{> guardrails/verify-before-done}}
- {{> guardrails/scope-discipline}}
分类与标签
编程开发、前端迁移、依赖清理、原生 JavaScript