简体   繁体   English

在 create-react-library 库中配置相对于根目录的导入

[英]Configure imports relative to root directory in create-react-library library

I am trying to setup a react component library with create-react-library (which uses rollup under the hood) and port over our application's existing component library so that we can share it between applications.我正在尝试使用 create-react-library (在后台使用汇总)设置一个反应组件库,并移植到我们应用程序的现有组件库上,以便我们可以在应用程序之间共享它。 I am able to create the library, publish to a private git registry, and consume it in other applications.我能够创建库,发布到私有 git 注册表,并在其他应用程序中使用它。 The issue is that I have had to change all of my imports to relative imports which is rather annoying as I am planning on porting over a large amount of components, hocs and utils.问题是我不得不将所有导入更改为相对导入,这很烦人,因为我正计划移植大量组件、临时程序和实用程序。

The entry point of the package is the src dir. package 的入口点是src目录。 Say I have a component in src/components/Text.js and a hoc in src/hoc/auth.js .假设我在src/components/Text.js中有一个组件,在src/hoc/auth.js中有一个 hoc。 If I want to import withAuthentication from src/hoc/auth.js into my Text component, I have to import it like import { withAuthentication } from "../hoc/auth" but I'd like to be able to import with the same paths I have in my existing application so it's easy to port over components, like import { withAuthentication } from "hoc/auth"如果我想将withAuthenticationsrc/hoc/auth.js导入到我的 Text 组件中,我必须像import { withAuthentication } from "../hoc/auth"一样导入它,但我希望能够使用我在现有应用程序中的路径相同,因此很容易移植组件,例如import { withAuthentication } from "hoc/auth"

I have tried a lot of config options, jsconfig.json the same as my create-react-app application, manually building my library with rollup rather then using create-react-library so I'd have more config options but to no avail.我已经尝试了很多配置选项,jsconfig.json 与我的 create-react-app 应用程序相同,使用汇总手动构建我的库而不是使用 create-react-library 所以我会有更多配置选项但无济于事。

Below are the relevant bits from my package.json as well as my jsconfig.json, any help would be greatly appreciated, I am sure I am not the only person who's had this issue.以下是我的 package.json 以及我的 jsconfig.json 中的相关位,非常感谢任何帮助,我相信我不是唯一遇到这个问题的人。

Here's the package.json这是 package.json

{
  "main": "dist/index.js",
  "module": "dist/index.modern.js",
  "source": "src/index.js",
  "files": [
    "dist"
  ],
  "engines": {
    "node": ">=10"
  },
  "scripts": {
    "build": "microbundle-crl --no-compress --format modern,cjs",
    "start": "microbundle-crl watch --no-compress --format modern,cjs",
    "prepare": "run-s build",
    "test": "run-s test:unit test:lint test:build",
    "test:build": "run-s build",
    "test:lint": "eslint .",
    "test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
    "test:watch": "react-scripts test --env=jsdom",
    "predeploy": "cd example && npm install && npm run build",
    "deploy": "gh-pages -d example/build"
  },
  "peerDependencies": {
    "react": "^16.0.0",
    "react-html-parser": "^2.0.2",
    "lodash": "^4.17.19",
    "@material-ui/core": "^4.11.0",
    "react-redux": "^7.1.1",
    "redux": "^4.0.1",
    "redux-localstorage": "^0.4.1",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "react-router-dom": "^5.1.1",
    "react-dom": "^16.13.1",
    "react-scripts": "^3.4.1",
    "react-svg": "^12.0.0",
    "reselect": "^4.0.0"
  },
  "devDependencies": {
    "microbundle-crl": "^0.13.10",
    "babel-eslint": "^10.0.3",
    "cross-env": "^7.0.2",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.7.0",
    "eslint-config-standard": "^14.1.0",
    "eslint-config-standard-react": "^9.2.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-node": "^11.0.0",
    "eslint-plugin-prettier": "^3.1.1",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-react": "^7.17.0",
    "eslint-plugin-standard": "^4.0.1",
    "gh-pages": "^2.2.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.0.4"
  },
  "dependencies": {
    "node-sass": "^7.0.0"
  }
}

and here's the jsconfig:这是jsconfig:

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

The example from the following link maybe you should navigate to your src file from the BaseUrl, or if you "src" is in the same directory as your config it should be only "."以下链接中的示例可能您应该从 BaseUrl 导航到您的 src 文件,或者如果您的“src”与您的配置位于同一目录中,则它应该只是“。”

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

Example from link above:来自上面链接的示例:

{
  "compilerOptions": {
    "baseUrl": ".", // This must be specified if "paths" is.
    "paths": {
      "jquery": ["node_modules/jquery/dist/jquery"] // This mapping is relative to "baseUrl"
    }
  }
}

You need to do 3 things你需要做3件事

tsconfig.json this enables typescript autocomplete tsconfig.json这将启用 typescript 自动完成

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src/*": ["./src/*"],
      "src": ["./src"]
    }
  }
}

babel.config.js this plugin resolves relative modules babel.config.js这个插件解析相关模块

module.exports = {
  plugins: [
    [
      'module-resolver',
      {
        alias: {
          src: './src'
        }
      }
    ]
  ]
}

package.json // install babel-plugin-module-resolver package.json // 安装 babel-plugin-module-resolver

{
  //...
  "devDependencies": {
    //...
    "babel-plugin-module-resolver": "^3.2.0",
    // ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM