webpack 如何解析来自 node_modules 的导入? [英] How does webpack resolve imports from node_modules?

查看:52
本文介绍了webpack 如何解析来自 node_modules 的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要 node_modules 文件夹中的库时,我会执行以下操作:

When I need a library from my node_modules folder I do something like this:

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

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

How does webpack know what file(s) it really has to import? Guess there's some kind of strategy what files it's going to check?

推荐答案

Webpack 循环遍历 resolvers 找到您请求的文件.它通过解析模板找出确切路径.

Webpack loops over resolvers to find the file you requested. It goes over resolve templates to figure out the exact path.

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

If you try to import a module that doesn't exist you'll see the error trace outlining all the paths it tried to use to find the file but failed.

解析器是一种强大的配置工具,可以帮助您开发更好的代码.我非常感谢解析器允许我删除相对路径并使用更多 开发者友好的 ES6 导入.

Resolvers are a powerful configuration tool that could help you develop better code. I really appreciate that resolvers allow me to drop relative paths and use more developer-friendly ES6 imports.

希望能帮到你.

这篇关于webpack 如何解析来自 node_modules 的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆