Skip to content

[labs/analyzer] Add ability to customize the output location of a given source file #3578

Description

@kevinpschaaf

The analyzer currently implements a getJSPathFromSourcePath() function using ts.getOutputFileNames().

However, just because we can determine that src/foo.ts gets output by tsc to build/foo.js doesn't mean that's where the file is ultimately importable from (which is what jsPath intends to capture), since additional transforms outside of ts can (and often do) further process that into another location that is ultimately published (i.e. we output to ./development/*.js and process that further with rollup to ./*.js).

This is something we're going to hit pretty quickly when we start testing against real-world packages, and will likely need to solve by allowing the caller to the analyzer to effectively configure the getJSPathFromSourcePath() function.

Activity

  1. converted this from a draft issue on Jan 18, 2023
  2. justinfagnani commented on Jan 19, 2023

    @justinfagnani
    Collaborator

    I think there are two approaches the can help associate published files with compiler output:

    1. Reverse the export mapping. Once we know all the output paths for all the source files, we can run those through the export map and see what file path(s) they match against, and then reverse the wildcard replacement to generate the export path. Because exports only support a single * wildcard, I believe this is possible.

      There's a change of ambiguity with multiple matches, but I think that could be solved by running each export path (if there are multiple) back through the export patterns can choosing the first match (as Node does). Hopefully then there would only be one left.

    2. Use source maps. If the project has been built, then we can try to import every file we can that mapped from exports. This should be possible similar to above by using the export's file path as a glob against the file system. Then we can see if those files have source maps and use the files mapped into the output to look up source files in the TS program.

      This might be good enough to map input to output even through other build steps like minification and bundling, with the downside of it requiring the project to be built.

    3. A third escape-hatch solution is to have a config file and let people specify the reverse export mapping themselves. So it'd be a mapping of source file globs to published file globs. To support replacement I think we'd need to restrict globs to a single wildcard like Node exports. Otherwise authors would use a complex glob but map to a fixed output.

      This would essentially be a little duplicative of the project's build / bundler config, but hopefully not too bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions