[英]How does webpack pick a relative path inside node_modules ? does it reference package.json at all?
When i do npm install react-slick
, i get the following in my node_modules
folder:当我执行
npm install react-slick
node_modules
,我在node_modules
文件夹中得到以下内容:
Now in my react application in a src/index.js
file, when i do the following:现在在我的
src/index.js
文件中的 react 应用程序中,当我执行以下操作时:
import Slider from "react-slick";
How does webpack know where to pick slider from ? webpack 如何知道从哪里选择滑块? will it at all look for some clue or definition inside
node_modules/react-slick/package.json
at all ?它会在
node_modules/react-slick/package.json
中寻找一些线索或定义吗?
Edit :- so here is the package.json file for webpack, when i import Slider from 'react-slick' , does it resolve to dist
or lib
?编辑:-所以这里是 webpack 的package.json文件,当我从 'react-slick' 导入 Slider 时,它是否解析为
dist
或lib
? and which file does it pick then and why ?那么它选择哪个文件,为什么?
Well, the simple walkthrough of it will be as below:那么,它的简单演练如下:
If you carefully look at the node_modules/react-slick/package.json
there is a property named main
.如果你仔细查看
node_modules/react-slick/package.json
有一个名为main
的属性。 Something like this:像这样的东西:
{
"name": "react-slick",
"main": "index.js"
}
It will tell the Webpack which file is the entry file of the whole package (It's usually referred to index.js
) .它会告诉 Webpack 哪个文件是整个包的入口文件(通常称为
index.js
) 。 All the necessary exports for the package lies in this file, so Webpack will only look for those exports and will import what you looking for.包的所有必要导出都在此文件中,因此 Webpack 只会查找这些导出并导入您要查找的内容。 In this particular case, there should be a default export for the
Slider
that you using right now.在这种特殊情况下,您现在使用的
Slider
应该有一个默认导出。 So the index.js
is probably something like this:所以
index.js
大概是这样的:
// index.js
var slider = require('./lib/slider'); // Usually all the main modules are lies under lib folder.
// other imports ...
module.exports = slider;
lib
and dist
lib
和dist
区别Usually, the dist
folder is for shipping a UMD that a user can use if they aren't using package management.通常,
dist
文件夹用于传送用户可以在不使用包管理的情况下使用的 UMD。 The lib
folder is what package.json
, main
property points to, and users that install your package using npm
will consume that directly. lib
文件夹是package.json
, main
属性指向的内容,使用npm
安装包的用户将直接使用它。 The only use of the lib
as opposed to src
is to transform your source using babel and Webpack to be more generally compatible since most build processes don't run babel transforms on packages in node_modules
.与
src
相比, lib
的唯一用途是使用 babel 和 Webpack 将源转换为更普遍的兼容,因为大多数构建过程不会在node_modules
包上运行 babel 转换。
Webpack uses aliases to target node_modules
using a shorthand. Webpack 使用别名来使用速记来定位
node_modules
。
import 'xyz'
/abc/node_modules/xyz/index.js
import 'xyz/file.js'
/abc/node_modules/xyz/file.js
Once it targets the correct folder in node_modules
, it follows the rules written in the package itself ( manifest
, package.json
)一旦它以
node_modules
的正确文件夹为node_modules
,它就会遵循包本身中编写的规则( manifest
、 package.json
)
You can also define your own aliases as such:您还可以定义自己的别名:
webpack.config.js
const path = require('path');
module.exports = {
//...
resolve: {
alias: {
xyz$: path.resolve(__dirname, 'path/to/file.js')
}
}
};
And then can be used as import xyz from $xyz
然后可以用作
import xyz from $xyz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.