entwicklerstube / babel-plugin-root-import

Add the opportunity to import modules by the root path
https://npmjs.com/babel-plugin-root-import
MIT License
1.18k stars 76 forks source link

Converting dynamic import #141

Closed enigma1 closed 4 years ago

enigma1 commented 4 years ago

In cases where the path is dynamically I have a bit of hard time to convert it. For example

const importView = page => {
  lazy(() => import(`^/Pages/${page}`).catch(() => import('^/Pages/Error')));
}

As long as the parameters can be pre-calculated is not a problem but if they are not it can't be used. So in this example here, I can handle the second instance of import by adding an import function in the functions array of this plugin, but for the first instance direct processing is aborted because it can't simply evaluate the path.

I could handle it in the bundler it just would been good if there was a way from within the plugin to cover that case too.

brigand commented 4 years ago

I agree that this would be good to support, however dynamic import has the entirety of JS available for constructing the path, so we'd have to decide on specific patterns to match.

Starting with non-tagged template literals with a leading non-empty literal section seems good. I'll take a stab at this later today.

enigma1 commented 4 years ago

Yea pattern support would be awesome :+1:

brigand commented 4 years ago

The code in your sample should work now with version 6.6.0. Feel free to open another issue if it doesn't work as you expect.

enigma1 commented 4 years ago

Just to say thanks, everything works as expected now.