A suggestion was made by Wex in their answer to rename your files to have the basename index
with the appropriate extension and use the mapping "*"*: ["*", "packages/*"]
. However, you expressed a preference to avoid renaming the files. Apart from avoiding renames, having many files named index.<some_extension>
can be distracting when working. It requires extra effort to differentiate between files with similar basenames by looking at the directory structure or typing out the full path. This can be bothersome and time-consuming.
Let's address some obstacles first. There's no indication that ~
and $1
have any special treatment within paths
. Checking the source code of tsc
didn't reveal any handling of such patterns. It seems that these symbols are not treated as special characters. Also, disregarding extensions for now, your desired mapping seems to be
packages/<package_name>/<package_name>.ext
, which includes the package name twice. Although setting a mapping like
"*": ["packages/*/*.ext"]
might seem logical, TypesScript does not allow this due to the presence of two asterisks in the mapping, resulting in an error from
tsc
.
Utilize package.json
To avoid the renaming issue, consider adding a package.json
to each package with a "main"
field pointing to the desired entry file. For example, packages/jsx/package.json
could contain:
{
"main": "jsx.jsx"
}
By providing similar files for all packages, you can simplify the configuration to:
"baseUrl": "",
"paths": {
"*": ["packages/*", "*"]
},
Alternatively, you can use "baseUrl"
to specify your packages and omit "paths"
entirely:
"baseUrl": "packages/",
Ensure the package.json
files are linted as any syntax errors will be disregarded by tsc
.
Include index
Files for Reexporting
Another approach is to use index files that simply reexport the desired entry files of your packages. The existing files would remain, but they would be referenced by these index files. For example, packages/tsx/index.ts
could contain:
export { default } from "./tsx";
If all packages have a default export, they can follow this pattern. For packages with multiple exports wanting to reexport everything, you can use:
export * from "./myModule";
By implementing this for all packages, you can meet the "*": ["*", "packages/*"]
mapping requirement without renaming any files.
In a comment, you mentioned using tools like Barrelsby to generate index files. Consider the impact on development, as these generated files would coexist with manually authored files. If adding index
files, aim for simplicity without relying on code generators.
Individually Map The Packages
If previous methods are impractical for your project, consider providing individual mappings for each package:
"paths": {
"jsx": ["packages/jsx/jsx.jsx"],
"tjs": ["packages/tjs/tjs.js"],
"tscript": ["packages/tscript/tscript.js"],
"tsx": ["packages/tsx/tsx.tsx"],
},
You could also set "baseUrl": "packages/"
and update paths accordingly to reduce verbosity while still needing one mapping per package.