Setting the “root” of a published npm project

只谈情不闲聊 提交于 2020-01-03 19:15:45

问题


I'm publishing an npm package named foo to the npm registry. I wrote the package using a compile-to-js language. For sanity, I put the compiled output into the dist/ folder of the project directory. My package.json lists the entrypoint as dist/entry.js:

{
  "name": "foo",
  "main": "dist/entry.js",
}

Sometimes, I want to use files within the package that are not part of the entry point. For example, there is a very useful export called whatever inside of dist/util.js:

import { whatever } from "foo/dist/util";

This works, but forcing the users of my package to type dist/ in all import statements is inconvenient.

Furthermore, re-exporting every possible util function is not DRY. I do not want to re-export from the entrypoint.

Ideally, I would like to import files from dist/ using the following syntax:

import { whatever } from "foo/util"

How do I configure my package.json to search for files in the dist/ folder of my project?


回答1:


This cannot be done.

This is the reason why some packages have entry point file that re-exports all public exports (not everything that resides in dist is intended to be used by end user), e.g. @angular/core.

And the reason why some packages have unsuitable file structure that is published to NPM registry and favours proper import paths, e.g. rxjs.



来源:https://stackoverflow.com/questions/51621415/setting-the-root-of-a-published-npm-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!