Add option to keep extension suffix
import { resolve } from 'path';
to
import { resolve } from '../path/to/module/path.js';
with .babelrc:
{
"plugins": [
["module-resolver", {
suffix: true /* with false by default */
/*....*/
}]
]
}
Hi, could you describe your usecase in detail?
Currently having the extension attached to the end is possible by using the resolvePath option:
import { resolvePath } from 'babel-plugin-module-resolver';
['module-resolver', {
resolvePath(...args) {
const resolved = resolvePath(...args);
const pathWithExtension = isRelative(resolved) ? attachExtension(resolved) : resolved;
return pathWithExtension;
}
}]
This doesn't mean that we wouldn't be interested in adding this feature, I'd just like to know why you need it :)
Currently, I would like to use vanilla javascript to import modules natively (see support here). And some libraries (like D3, loadash and etc) use the UMD form to import modules which is not necessarily compatible with native method. These libraries use some kind of plugin to transform into UMD (like gulp, webpack , babel or rollup). And this allows (before of UMD form) the source code to be a kind of native code only with imports without the relative path. With this plugin (module-resolve), I may be able to resolve the paths and finally use the native path, however I would need the extension.
Examples:
import { select } from 'select';
And with plugin with extension:
import { select } from '../node_modules/d3-select/src/selection/select.js'
Hmm I think it would be sensible to have the option here. Would you be interested in contributing to the plugin?
@SandHilt Are you saying that the extension is required for the browser to work properly? Because in node, the .js and .json extention are the defaults and not needed at all.
If we do this, I'd probably prefer an option named explicitExtensions or something more explicit than suffix :)
For issue https://github.com/tleunen/babel-plugin-module-resolver/issues/201 I've created a PR https://github.com/tleunen/babel-plugin-module-resolver/pull/247 for adding a stripExtensions option that does something similar to what the OP wants.
The feature is on master (shout out to @amosyuen, great work!) and will most probably be released soon.