babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

Doesn't work with templated strings

Open tleunen opened this issue 7 years ago • 3 comments

I finally got a usecase where I wanted to make a dynamic require() call based on a variable, like so:

require(`App/xxx/${filename}`)

But it doesn't work because we don't know the full filename (since the plugin checks for the file to exist.

Since we made resolving an alias a priority over the root configuration, I believe we could remove the checks for a valid file, and thus allowing templated strings. What do you think @fatfisz ?

I know we had a few tickets around that issue, but couldn't find them with the search terms I used :/

tleunen avatar Apr 05 '18 17:04 tleunen

Would love to see a fix for this as well, if possible.

FWIW, I found an old issue referencing the same thing: https://github.com/tleunen/babel-plugin-module-resolver/pull/31

efjacobson avatar Apr 16 '18 21:04 efjacobson

Same issue. Appreciate your plugin nonetheless, will work around. Just wanted to chime in with feedback. 🙂

// works
require('alias/file')

// doesn't work
const filename = 'file';
require('alias/' + filename);

arasmussen avatar Aug 04 '18 06:08 arasmussen

I don't think this issue is connected to template strings, it just doesn't work with expressions. Example:

const file1 = require('path/to/file.js') // Works

const filePath = 'path/to/file.js'
const file2 = require(filePath) // Doesn't work

niksajanjic avatar Oct 11 '18 12:10 niksajanjic