简体   繁体   English

webpack 如何在 node_modules 中选择相对路径? 它完全引用 package.json 吗?

[英]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 时,它是否解析为distlib and which file does it pick then and why ?那么它选择哪个文件,为什么?

Well, the simple walkthrough of it will be as below:那么,它的简单演练如下:

Simple Walkthrough简单的演练

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;

Difference between lib and dist libdist区别

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.jsonmain属性指向的内容,使用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

Example #1:示例#1:

import 'xyz'
/abc/node_modules/xyz/index.js

Example #2:示例#2:

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 ,它就会遵循包本身中编写的规则( manifestpackage.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.

相关问题 依赖性甚至未在package.json和node_modules中定义 - Dependency Not found even defined in package.json and node_modules 本地 package.json 存在,但缺少 node_modules - Local package.json exists, but node_modules missing 如何在docker构建期间的yarn / npm安装期间缓存node_modules以获取未更改的package.json - How to cache node_modules for unchanged package.json during yarn / npm install during docker build? 破坏 node_modules 和 package.json 后如何重置依赖树? - How can I reset my dependency tree after ruining node_modules and package.json? script标记如何解析node_modules的路径? - How does script tag resolves path to node_modules? Webpack不会从node_modules导入包(仅适用于js) - Webpack does not import bundles from node_modules (js only) 错误:在 /app/node_modules/chart.js/package.json 中没有使用 nextjs 和 chartjs 定义“导出”主要内容 - Error: No "exports" main defined in /app/node_modules/chart.js/package.json with nextjs and chartjs Webpack 2:如何排除所有 node_modules 除了 - Webpack 2: How to exclude all node_modules except for 如何将所有已安装的节点模块保存在package.json中? - How can I save all the installed node modules in package.json? 弃用警告:在 node_modules\postcss\package.json 的 package 的“导出”字段模块解析中使用弃用的文件夹映射“./” - DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at node_modules\postcss\package.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM