简体   繁体   English

带汇总的绝对导入

[英]Absolute imports with rollup

I'm trying to get imports like我正在尝试获得类似的进口

import { startup } from "applicationRoot/renderUI";

to work, from anywhere in my application.在我的应用程序的任何地方工作。 I thought the rollup-plugin-alias would be a good fit for this.我认为rollup-plugin-alias很适合这个。 I tried configuring我试过配置

alias({
  applicationRoot: "applicationRoot/"
})

in my plugins array.在我的插件数组中。 This came close, but the extension is dropped, so I get the error:这很接近,但扩展名被删除,所以我收到错误:

c:\\path\\to\\applicationRoot\\renderUI doesn't exist. c:\\path\\to\\applicationRoot\\renderUI 不存在。

Adding in加入

alias({
  resolve: [".js"],
  applicationRoot: "applicationRoot/"
}),

did not change anything.没有改变任何东西。

You can use rollup-plugin-includepaths .您可以使用rollup-plugin-includepaths

Add this to your Rollup configuration:将此添加到您的汇总配置中:

import includePaths from 'rollup-plugin-includepaths';

export default {
  ...
  plugins: [
    ...
    includePaths({ paths: ["./"] })
  ]
};

That will tell Rollup to also resolve imports from the root of your application, so things like这将告诉 Rollup 还从应用程序的根目录解析导入,所以像

import { startup } from "applicationRoot/renderUI";

will appropriately find an applicationRoot folder where it is.将适当地找到它所在的applicationRoot文件夹。

This is not the answer to the original question, but if you are coming here from search results and are using Typescript, you can set the compilerOptions.baseUrl in tsconfig and it will just work.这不是原始问题的答案,但如果您是从搜索结果来到这里并使用 Typescript,则可以在 tsconfig 中设置compilerOptions.baseUrl ,它就会起作用。

{
  ...
  compilerOptions: {
    ...
    "baseUrl": "./"
  },
}

Then if you have a file like `src/lib/util.ts' you can import it:然后,如果你有一个像 `src/lib/util.ts' 这样的文件,你可以导入它:

import util from 'src/lib/util'

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

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