diff --git a/README.md b/README.md index 2190a8e..8cf0861 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ yarn add --dev eslint-import-resolver-custom-alias "alias": { "src": "./src" }, + "stripQuery": false, "extensions": [".js", ".jsx"], "packages": [ "packages/*" @@ -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 diff --git a/index.js b/index.js index cdee82a..7e2e18f 100644 --- a/index.js +++ b/index.js @@ -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'], @@ -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 }; diff --git a/spec/index.spec.js b/spec/index.spec.js index 5169a98..f7425b5 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -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/@'; @@ -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); });