Search code examples
javascriptecmascript-6rollupjs

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. 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.

Adding in

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

did not change anything.


Solution

  • You can use 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

    import { startup } from "applicationRoot/renderUI";
    

    will appropriately find an applicationRoot folder where it is.