Skip to content

Resolve imports with query params #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn add --dev eslint-import-resolver-custom-alias
"alias": {
"src": "./src"
},
"stripQuery": false,
"extensions": [".js", ".jsx"],
"packages": [
"packages/*"
Expand All @@ -41,6 +42,8 @@ Here, `alias` is a key-value pair, where `key` represents the alias, and `value`
it's actual path. Relative path is allowed for `value`. When used, it's relative to project
root, where command line is running. (i.e. root path will be `process.cwd()`)

`stripQuery` is a boolean value. If set to `true`, the resolver will strip query parameters from an imported string.

`extensions` is an array of possible suffix. If not provided, default value will be `[".js"]`.

`packages` is an optional configuration. When using lerna to manage packages and use eslint at
Expand Down
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ const fs = require('fs');
const resolve = require('resolve'); // eslint-disable-line
const globParent = require('glob-parent');

/**
* Strip Query Params from Imports
*/
function stripQuery(id) {
const [bareId, query] = id.split('?');
const suffix = `${query ? `?${query}` : ''}`;
return {
bareId,
query,
suffix,
};
}

function getOptions(file, config) {
return {
extensions: config.extensions || ['.js'],
Expand Down Expand Up @@ -54,7 +67,8 @@ exports.resolve = (source, file, config) => {
}
return ret;
}, source);
const resolvedPath = resolve.sync(modifiedSource, getOptions(file, config));
const resolvedSource = config.stripQuery ? stripQuery(modifiedSource).bareId : modifiedSource;
const resolvedPath = resolve.sync(resolvedSource, getOptions(file, config));
return { found: true, path: resolvedPath };
} catch (e) {
return { found: false };
Expand Down
32 changes: 28 additions & 4 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ describe('resolver plugin', () => {
});
it('should use config to resolve file', () => {
plugin.resolve(defaultSource, defaultFile, defaultConfig);
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substr(1)}`;
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substring(1)}`;

const mostRecent = resolve.sync.calls.mostRecent();

expect(mostRecent.args[0]).toBe(modifiedSource);
expect(mostRecent.args[0]).toBe(path.normalize(modifiedSource));
expect(mostRecent.args[1].extensions).toEqual(['.js', '.jsx']);
expect(mostRecent.args[1].basedir).toBe(defaultBasedir);
});
it('should replace alias', () => {
plugin.resolve(defaultSource, defaultFile, defaultConfig);
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substr(1)}`;
const modifiedSource = `${path.resolve(process.cwd(), './src')}${defaultSource.substring(1)}`;

const mostRecent = resolve.sync.calls.mostRecent();
expect(mostRecent.args[0]).toBe(modifiedSource);
expect(mostRecent.args[0]).toBe(path.normalize(modifiedSource));
});
it('should not replace alias when not starts with it', () => {
const source = '@@/path/@/file/@';
Expand Down Expand Up @@ -182,6 +182,30 @@ describe('resolver plugin', () => {
plugin.resolve(defaultSource, fileInPackage, config);
const modifiedSource = path.resolve(process.cwd(), './src', 'path/to/file');

const mostRecent = resolve.sync.calls.mostRecent();
expect(mostRecent.args[0]).toBe(modifiedSource);
});
it('should strip query parameters when configured', () => {
const config = Object.assign({}, defaultConfig, {
stripQuery: true,
});
const sourceWithQuery = '@/path/to/file?foo=bar';
const fileInPackage = path.resolve(process.cwd(), defaultFile);
plugin.resolve(sourceWithQuery, fileInPackage, config);
const modifiedSource = path.resolve(process.cwd(), './src', 'path/to/file');

const mostRecent = resolve.sync.calls.mostRecent();
expect(mostRecent.args[0]).toBe(modifiedSource);
});
it('should leave query parameters intact when not configured', () => {
const config = Object.assign({}, defaultConfig, {
stripQuery: false,
});
const sourceWithQuery = '@/path/to/file?foo=bar';
const fileInPackage = path.resolve(process.cwd(), defaultFile);
plugin.resolve(sourceWithQuery, fileInPackage, config);
const modifiedSource = path.resolve(process.cwd(), './src', 'path/to/file?foo=bar');

const mostRecent = resolve.sync.calls.mostRecent();
expect(mostRecent.args[0]).toBe(modifiedSource);
});
Expand Down