Skip to content

Commit

Permalink
fix: Fix strip extensions for RN support (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuLemoine authored and tleunen committed Aug 17, 2017
1 parent d4558ae commit f22cb68
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ To use the backslash character (`\`) just escape it like so: `'\\\\'` (double es

If you're using ESLint, you should use [eslint-plugin-import][eslint-plugin-import], and [eslint-import-resolver-babel-module][eslint-import-resolver-babel-module] to remove falsy unresolved modules.

## Usage with React Native

To let the packager resolve the right module for each platform, you have to add the ```.ios.js```and ```.android.js``` extensions :

```json
{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"extensions": [".js", ".ios.js", ".android.js"]
}
]
]
}
```

## Usage with Flow

To allow Flow to find your modules, add configuration options
Expand Down
1 change: 1 addition & 0 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
return toLocalPath(toPosixPath(replaceExtension(
mapToRelative(opts.cwd, currentFile, absFileInRoot),
ext,
opts,
)));
}

Expand Down
17 changes: 15 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ export function toLocalPath(modulePath) {
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
}

export function replaceExtension(modulePath, ext) {
const filename = path.basename(modulePath, path.extname(modulePath)) + ext;
export function stripExtension(modulePath, extensions) {
const [name, ...splits] = path.basename(modulePath).split('.');
const fileExtension = `.${splits.join('.')}`;
return extensions.reduce((filename, extension) => {
// To allow filename to contain a dot
if (extension === fileExtension) {
// Strip extension
return name;
}
return filename;
}, path.basename(modulePath, path.extname(modulePath)));
}

export function replaceExtension(modulePath, ext, opts) {
const filename = stripExtension(modulePath, opts.extensions) + ext;
return path.join(path.dirname(modulePath), filename);
}

Expand Down
28 changes: 28 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ describe('module-resolver', () => {
});
});

describe('non-standard double extensions', () => {
const rootTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './test/testproject/src',
extensions: ['.js', '.ios.js', '.android.js'],
}],
],
};

it('should not resolve the file path with an unknown extension', () => {
testWithImport(
'text',
'text',
rootTransformerOpts,
);
});

it('should resolve the file path with a known defined extension & strip the extension', () => {
testWithImport(
'rn',
'./test/testproject/src/rn',
rootTransformerOpts,
);
});
});

describe('root and alias', () => {
const rootTransformerOpts = {
babelrc: false,
Expand Down
Empty file.
Empty file.

0 comments on commit f22cb68

Please sign in to comment.