fix-esm-import-path
fix-esm-import-path copied to clipboard
Do you plan to support glob, alias & index imports?
For example: "fixpaths": "fix-esm-import-path dist/**/*.js"
or with file structure:
src/common.components/Greeter/index.js
src/common.components/GreeterUse/use.js
GreeterUse/use.js has code with alias:
import * from '@common/Greeter'
Thanks for the suggestion. The current design require a entry file to start the fixing.
I just known esm will/has support index import, will try to implement it.
I'm not sure if it worth to implement glob, you may use an index file as the entry file. (And you may generate the index file with tools like gen-index)
I seldom use alias import, but would be interested to implement it as well. PR is also welcomed :)
In browser importmaps are not supported globs yet. That's why we forced to hardcode all aliased esm imports! =(
For TypeScript I use tsconfig paths, and there no need for your module. You can see my example project with esm for browser & SystemJS for browser support, also node (tsconfig) script run.
I'm looking solution for js-project only (without bundlers & their plugins, nodejs script utilites only).
Also you can read my translated gist (original gist)
Why would you need glob matching the files?
I'm thinking use case requiring that need to scan the js files that are not imported directly or indirectly by the entry file. For lazy imported files whose path are constructed dynamically?
Globs for matching alias-imports strings to solve them with mapping, like eslint does or webpack.
I've encountered a use case that glob may help: when publishing a npm package that as multiple bin files, each file may need to be fixed.
Workaround for above use case is to write an index js that imports each cli js files, then use this index js as the entry point for fix-esm-import-path to fix the files, but do not publish this index js, otherwise it will invoke all the cli. This is a sub-optimal solution.
I want to avoid direct support on glob to reduce the complexity. Maybe allow to use a directory as entry point will be enough for most use case? e.g. fix-esm-import-path dist/esm
Just published [email protected] which allows using directory as entry point, it recursively scan and fix all js and ts files under the given directory.
Will look into tsconfig alias support
For tsconfig alias you can investigate tsconfig-paths. My example of usage tsconfig-paths (for node & eslint). But folders with index files & file extensions I still replace manually by node script.
You can also see tsc-alias which generates relative paths and my configuration for it.
@beenotung Thanks for this great package, do you plan on supporting index imports? Or would you be able to point in the direction for me look into adding support to it in the current codebase?
@nicholasio index imports support is discussed in https://github.com/beenotung/fix-esm-import-path/issues/11 and released in [email protected] 🚀