跳到正文
Skillpedia
通用编程75k · #2836

es-modules

它能做什么

这项技能检查使用旧模块语法的 JavaScript 文件,并建议改用现代标准。适用于希望代码更精简、更清晰的开发者。

适用场景

  • ✓当你的 JavaScript 文件仍在使用 require() 或 module.exports 的时候。
  • ✓当你的打包结果中包含你从未使用的代码的时候。
  • ✓当你希望脚本作为原生浏览器模块加载的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/es-modules/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/es-modules/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请把我 JavaScript 文件中的 require 语句转换为 import 语法。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出 require 和 module.exports 的行,并建议对应的 import 和 export 语句。在合适的地方,它还会推荐具名导出。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Use ES modules (import/export)

ES modules are statically analyzable — bundlers can determine at build time what code is actually used and eliminate the rest (tree-shaking). CommonJS require() is dynamic and prevents this optimization. ES modules are also the browser-native standard, reducing the need for build-time transformation.

Quick Reference

  • Use import/export syntax instead of require()/module.exports
  • Named exports improve IDE autocompletion and enable tree-shaking
  • Add type="module" to script tags or use a bundler to load ES modules in browsers
  • Default exports are fine but named exports scale better in large codebases

Check

Identify any use of require(), module.exports, or exports in this JavaScript file that should be converted to ES module syntax.

Fix

Convert all require() and module.exports statements to ES module import/export syntax.

Explain

Explain the benefits of ES modules over CommonJS, including tree-shaking, static analysis, and browser support.

Code Review

Review scripts, client components, and browser execution paths related to Use ES modules (import/export). Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/es-modules

常见问题

什么是 tree-shaking?

它是指打包工具移除从未被使用的代码,从而使文件变小。现代模块语法使这一点成为可能。

我应该使用具名导出吗?

这项技能说明,具名导出在大型代码库中更易扩展,不过默认导出仍然可以使用。

如何在浏览器中加载模块?

这项技能建议在 script 标签上添加 type=module,或使用打包工具。

详情

作者的原始描述

Use when reviewing scripts, client components, bundles, or runtime behavior related to Use ES modules (import/export). Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2836
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能