Rollup changes import of absolute path to relative path while bundling
Asked Answered
M

2

6

I am importing a absolute path in some files of my repo and try to bundle it with rollup My import where /api/ is absolute path:

import * from '/api/myFile.js'

But when I bundle it, rollup changes it to relative path and it looks like:

import * from  from '../../../../api/myFile.js'

And the above path doesn't exist in my app :(

My rollup config:

rollup src\\input.js -o lib\\bundle.js -f esm --inlineDynamicImports=true

Please help me resolve this issue

I tried making '/api/' path as external, that didn't change anything. And i tried using few rollup plugins - includepaths, root-import,etc nothing worked

Mould answered 16/7, 2019 at 16:5 Comment(0)
N
2

Quite late, but for anyone facing this issue: you have to use the output.paths option. Otherwise rollup tries to resolve it with a weird root path (in my case even not a CWD, HDD root?).

In this case (skipping non-relevant flags):

output: {
    paths: {
      api: '/api'
    }
}

And then just import it:

import * from 'api/myFile.js'
import * from 'api/otherFile.js'

Note no leading / - it is now a kind of absolutely mapped module.

Nonentity answered 5/2, 2021 at 15:11 Comment(0)
D
0

Use the configuration setting makeAbsoluteExternalsRelative together with external e.g.:

const config = [
  {
    input: 'main.js',
    output: {
      [...]
    },
    external: [ '/api/myFile.js' ],
    makeAbsoluteExternalsRelative: 'ifRelativeSource'
  }
];

If makeAbsoluteExternalsRelative: 'ifRelativeSource' is not enough to do the trick, you might also want to try makeAbsoluteExternalsRelative: false instead.

See also: https://github.com/rollup/rollup/pull/4021

Dysarthria answered 18/7, 2024 at 17:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.