From d12890880e002681364f4a14322fa5406a596348 Mon Sep 17 00:00:00 2001 From: Keno Clayton Date: Mon, 29 Nov 2021 13:23:28 -0500 Subject: [PATCH 1/3] Resolve imports with query params --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 974b06a..cc0c35e 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,7 @@ exports.resolve = (source, file, config) => { } return ret; }, source); - const resolvedPath = resolve.sync(modifiedSource, getOptions(file, config)); + const resolvedPath = resolve.sync(stripQuery(modifiedSource).bareId, getOptions(file, config)); return { found: true, path: resolvedPath }; } catch (e) { return { found: false }; From a78d07657ad657b3376b453db43795a57a9755e2 Mon Sep 17 00:00:00 2001 From: Keno Clayton Date: Tue, 5 Dec 2023 01:41:31 -0500 Subject: [PATCH 2/3] Made strip query feature optional --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e388390..7e2e18f 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,8 @@ exports.resolve = (source, file, config) => { } return ret; }, source); - const resolvedPath = resolve.sync(stripQuery(modifiedSource).bareId, 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 }; From 27b877cc53ac447c12d67a33aebcc596dfb9a7fd Mon Sep 17 00:00:00 2001 From: Keno Clayton Date: Tue, 5 Dec 2023 02:09:29 -0500 Subject: [PATCH 3/3] Updated tests for new parameter Updated usage of deprecated `substr` to `substring` Updated some tests to use `path.normalize` for windows support --- README.md | 3 +++ spec/index.spec.js | 32 ++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) 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/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); });