从 npm 包的子文件夹导入

本文介绍了从 npm 包的子文件夹导入的处理方法,对大家解决问题具有一定的参考价值

问题描述

我一直致力于创建一个小型 React 组件库,以用于其他几个项目.我在内部发布包(使用私有 GitHub 存储库),然后包含在另一个项目中.但是,当我从包的子目录导入时,我无法这样做,因为路径不匹配.

I've been working on creating a small library of React components for use in several other projects. I am publishing the package internally (using a private GitHub repository) and then including in another project. However, when I go to import from a subdirectory of the package I am not able to do so as the paths don't match.

使用该包的项目都使用 webpack 来捆绑/转译代码,因为我尽量避免在组件库中进行任何构建.

The projects using the package all utilize webpack to bundle/transpile code as I am trying to avoid doing any building in the component library if possible.

- package.json
- src/
  - index.js
  - Button/
    - index.js
    - Button.jsx
    - ButtonGroup.jsx
  - Header/
    - index.js
    - Header.jsx (default export)

package.json

...
"main": "./src/index.js",
"scripts": "",
...

src/Button/index.js

import Button from './Button';
import ButtonGroup from './ButtonGroup';

export default Button;

export { Button, ButtonGroup};

src/index.js

如果只从子目录导入,这个文件真的有必要吗?

Is this file actually necessary if only importing from subdirectories?

import Button from './Button';
import ButtonGroup from './Button/ButtonGroup';
import Header from './Header';

export { Button, ButtonGroup, Header };

其他项目

// This project is responsible for building/transpiling after importing
import { Button, ButtonGroup } from 'components-library/Button';

示例

Material-UI 是一个 React 组件库,通过以下方式使用 require: import { RadioButtonGroup } from 'material-ui/RadioButton.我试图弄清楚这对他们来说是如何工作的,但还没有成功.

Example

Material-UI is a library of React components that is used by requiring in the following fashion: import { RadioButtonGroup } from 'material-ui/RadioButton. I've tried to figure out how this works for them but to no avail yet.

  • 我会如何使用 webpack 在 npm 包子文件夹中导入模块?
    • 这几乎是我需要的正确方法,除了那里使用的导入路径涉及 src/ 目录,我试图避免(应该是 component-library/item,而不是 component-library/src/item(虽然目前可以工作))
    • How would I import a module within an npm package subfolder with webpack?
      • This is very nearly the correct approach I require, except that the import path used there involved the src/ directory, which I am trying to avoid (should be component-library/item, not component-library/src/item (which does work currently though))
      • 这正是我想要的,只是我希望没有构建"包中的阶段(依靠导入位置来构建/转换)
      1. 我可以在导入路径中以某种方式跳过 src/ 目录吗?
      2. 我可以跳过包中的任何类型的构建阶段吗(这样开发人员就不必在提交之前构建)?
      3. 类似于 material-ui 的包如何处理这个问题?

      推荐答案

      可能的解决方案之一是 webpack 别名系统.
      您可以创建另一个项目,例如将其命名为app-aliases",以便您的别名可以重复使用.
      该项目将有一个包含所有包路径的 js 文件:

      One of the possible solutions there is webpack aliasing system.
      You can create another project, call it for example 'app-aliases', so your aliases will be reusable.
      This project will has one js file with all of your packages paths:

      const path = require('path');
      
      module.exports = {
      '@components': path.resolve(__dirname, 'node_modules/components-library/src'),
      '@another': path.resolve(__dirname, 'node_modules/any/path/you/want'),
      }
      

      然后将其添加到任何负责构建/转译的项目中的 webpack 配置中:
      webpack.config.js

      And then add it to the webpack configuration in any project which will be responsible for building/transpiling:
      webpack.config.js

      const appAliases = require('app-aliases');
      
      const config = {
      ...
        resolve: {
          alias: {
            ...appAlises
          }
        }
      }
      

      在运行时代码中,您将能够像这样使用它:

      In the runtime code you will be able to use it like this:

      import {Button} from '@components/Button';
      import {Something} from '@another'
      

      如果您使用打字稿,则需要将相同的别名添加到 paths tsconfig 属性.
      所以你的问题的答案是:

      If you are using typescript you will need to add the same aliases to the paths tsconfig property.
      So answers to your questions are:

      1. 是的,您可以在别名中使用任何路径
      2. 是的,没有必要构建所有项目
      3. 我看到现在 mui 使用从 Directi 包(例如核心)导入,请参阅 https://material-ui.com/components/radio-buttons/import Radio from '@material-ui/core/Radio';.但我希望他们使用我在下面描述的重新导出.
      1. Yes, you can use any path in aliases
      2. Yes, it is not necessary to build all of your projects
      3. I see that now mui uses imports from directi packages (core for example), see https://material-ui.com/components/radio-buttons/ there is import Radio from '@material-ui/core/Radio';. But I hope they using re-export that I described below.

      还有关于node.js解析机制.
      当您导入某个库时,它会尝试在其中查找 node_modules/some-library/package.jsonmain 属性.此属性应通向您的主要入口点.通常它是 src/index.js (如果还没有,你应该在 package.json 中设置它).在此文件中,您可以从内部文件结构中重新导出您想要的任何内容,并且无需完整路径即可使用它.有关示例,请参阅此 repo.

      Also about node.js resolution mechanism.
      When you import some library it tries to find node_modules/some-library/package.json and then main property inside it. This property should lead to your main entry point. Usually it is src/index.js (you should set it in package.json if it is no there yet). In this file you can re-export anything you want from internals file structure and will be able to use it without the full path. Please see this repo for some examples.

      这篇关于从 npm 包的子文件夹导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1155

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1055

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:795

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:882

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:923

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:866

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:852

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:824

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1683

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1292