Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External relative paths should be resolved relative to the output dir #3141

Open
marijnh opened this issue Oct 1, 2019 · 3 comments
Open

Comments

@marijnh
Copy link
Contributor

marijnh commented Oct 1, 2019

Currently (as of 1.22.0), if you have an input file src/foo/foo.js that imports "../../dep" (marked as external in the configuration), and you have rollup bundle it into dist/foo.js, the output file, that file will also reference ../../dep, though that means a different thing from its directory than it did from the source file's directory. So that file will generally fail to run.

Would it make sense to have the library re-resolve external relative paths relative to the output directory?

@lukastaegert
Copy link
Member

Not sure about this, but I would be open to more opinions here. At the moment, you should be able to fix that manually via output.paths.

@lukastaegert
Copy link
Member

Also note that when using the JS API, dir is not a required parameter when generating an in-memory bundle, which is also how the browser bundle works. So in such a situation, it would not be possible to resolve relative to dir.

@darionco
Copy link

darionco commented Apr 12, 2020

output.paths is not enough in my situation:

I have an external library that needs to be imported in two files where I am using code splitting, for the sake of organization the chunks are being rendered in two different folders, my project structure looks something like this:

root/
├── src/
│   ├── moduleA/
|   |   └── ...
│   └── otherModules/
|       ├── moduleB/
|       |   └── ...
|       └── ...
└── build/
    ├── static_module/
    |   └── ...
    ├── moduleA/
    |   └── [rollup renders the moduleA chunk here]
    └── otherModules/
        ├── moduleB/
        |   └── [rollup renders the moduleB chunk here]
        └── ...

In the example above both moduleA and moduleB import static_module, if rollups build is untouched, the import paths for static_module end up being incorrect and output.paths is not enough as I only get the absolute path to the static_module but I don't know which module is importing it so it's impossible to calculate the correct path to the files.

For the time being I wrote a small plugin that hooks resolveId and returns an object like:

{
  id: [correct relative path to `static_module` after the importing file is built],
  external: true,
}

but that only works if the entry point of the chunk is importing the file, otherwise it would be impossible to compute the proper relative path to static_module from the final location of the chunk.

I think that the best solution would be to add chunkPath or similar as a second argument to output.paths when it is a function, that way users would have all the information needed to generate a proper path to external libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants