webpack 如何解析来自 node_modules 的导入?

How does webpack resolve imports from node_modules?

当我需要我的 node_modules 文件夹中的库时,我会这样做:

import angular from 'angular';
import $ from 'jquery;

webpack 如何知道它真正需要导入的文件是什么?猜猜有某种策略它将检查哪些文件?

Webpack 遍历 resolvers to find the file you requested. It goes over resolve templates 以找出确切的路径。

如果您尝试导入一个不存在的模块,您将看到错误跟踪概述了它试图用来查找文件但失败的所有路径。

解析器是一个强大的配置工具,可以帮助您开发更好的代码。我真的很感激解析器允许我删除相对路径并使用更多 developer-friendly ES6 imports.

希望对您有所帮助。