为什么 webpack 需要一个空的扩展 [英] Why does webpack need an empty extension

查看:28
本文介绍了为什么 webpack 需要一个空的扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚为什么 webpack 需要这个空扩展.

I'm trying to figure out why webpack requires this empty extension.

resolve.extensions里面总是有这样的配置:

Inside resolve.extensions there's always this kind of configuration:

extensions: ['', '.js', '.jsx']

为什么不能只是这样:

extensions: ['.js', '.jsx']

推荐答案

来自 文档:

设置此选项将覆盖默认值,这意味着 webpack 将不再尝试使用默认扩展名解析模块.如果您希望正确解析扩展名所需的模块(例如 require('./somefile.ext')),您必须在您的大批.类似地,如果您希望将不需要扩展名的模块(例如 require('underscore'))解析为带有.js"扩展名的文件,您必须包括 ".js" 在你的数组中.

Setting this option will override the default, meaning that webpack will no longer try to resolve modules using the default extensions. If you want modules that were required with their extension (e.g. require('./somefile.ext')) to be properly resolved, you must include an empty string in your array. Similarly, if you want modules that were required without extensions (e.g. require('underscore')) to be resolved to files with ".js" extensions, you must include ".js" in your array.

换句话说,如果你没有包含空字符串并且需要一个模块作为 ./foo.js,webpack 会寻找 ./foo.js.js./foo.js.jsx 代替.

In other words, if you didn't include the empty string and required a module as ./foo.js, webpack would look for ./foo.js.js and ./foo.js.jsx instead.

这篇关于为什么 webpack 需要一个空的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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