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

Add option to keep extension suffix

Open sandhilt opened this issue 8 years ago • 6 comments

import { resolve } from 'path';

to

import { resolve } from '../path/to/module/path.js';

with .babelrc:

{
    "plugins": [
        ["module-resolver", {
                suffix: true /* with false by default */
                /*....*/
        }]
    ]
}

sandhilt avatar Nov 18 '17 16:11 sandhilt

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

fatfisz avatar Nov 18 '17 19:11 fatfisz

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'

sandhilt avatar Nov 18 '17 19:11 sandhilt

Hmm I think it would be sensible to have the option here. Would you be interested in contributing to the plugin?

fatfisz avatar Nov 18 '17 20:11 fatfisz

@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 :)

tleunen avatar Nov 20 '17 03:11 tleunen

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.

amosyuen avatar Dec 10 '17 23:12 amosyuen

The feature is on master (shout out to @amosyuen, great work!) and will most probably be released soon.

fatfisz avatar Dec 22 '17 22:12 fatfisz