fix-esm-import-path icon indicating copy to clipboard operation
fix-esm-import-path copied to clipboard

Do you plan to support glob, alias & index imports?

Open viT-1 opened this issue 3 years ago • 14 comments
trafficstars

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'

viT-1 avatar Dec 21 '21 07:12 viT-1

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)

beenotung avatar Dec 21 '21 18:12 beenotung

I seldom use alias import, but would be interested to implement it as well. PR is also welcomed :)

beenotung avatar Dec 21 '21 18:12 beenotung

In browser importmaps are not supported globs yet. That's why we forced to hardcode all aliased esm imports! =(

Node has new feature: subpath imports & patterns.

viT-1 avatar Dec 23 '21 09:12 viT-1

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).

viT-1 avatar Dec 23 '21 10:12 viT-1

Also you can read my translated gist (original gist)

viT-1 avatar Dec 23 '21 10:12 viT-1

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?

beenotung avatar Dec 24 '21 08:12 beenotung

Globs for matching alias-imports strings to solve them with mapping, like eslint does or webpack.

viT-1 avatar Dec 24 '21 13:12 viT-1

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.

beenotung avatar Dec 26 '21 09:12 beenotung

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

beenotung avatar Dec 26 '21 09:12 beenotung

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

beenotung avatar Dec 26 '21 10:12 beenotung

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.

viT-1 avatar Jan 11 '22 10:01 viT-1

You can also see tsc-alias which generates relative paths and my configuration for it.

viT-1 avatar Jan 11 '22 13:01 viT-1

@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 avatar Oct 24 '23 01:10 nicholasio

@nicholasio index imports support is discussed in https://github.com/beenotung/fix-esm-import-path/issues/11 and released in [email protected] 🚀

beenotung avatar Jun 19 '24 20:06 beenotung