Skip to content

Commit

Permalink
feat: remove any querystring from imports (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimbeng authored Feb 5, 2021
1 parent 2029cae commit 82ef357
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs",
"test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension",
"test:withPaths": "eslint --ext ts,tsx tests/withPaths",
"test:withQuerystring": "eslint --ext ts,tsx tests/withQuerystring",
"test:withoutPaths": "eslint --ext ts,tsx tests/withoutPaths",
"type-coverage": "type-coverage --cache --detail --ignore-catch --strict --update"
},
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export function resolve(

log('looking for:', source)

source = removeQuerystring(source)

// don't worry about core node modules
if (isCore(source)) {
log('matched core:', source)
Expand Down Expand Up @@ -136,6 +138,15 @@ function tsResolve(id: string, opts?: SyncOpts): string {
}
}

/** Remove any trailing querystring from module id. */
function removeQuerystring(id: string) {
const querystringIndex = id.lastIndexOf('?')
if (querystringIndex >= 0) {
return id.slice(0, querystringIndex)
}
return id
}

/** Remove .js or .jsx extension from module id. */
function removeJsExtension(id: string) {
return id.replace(/\.jsx?$/, '')
Expand Down
1 change: 1 addition & 0 deletions tests/withQuerystring/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../baseEslintConfig')(__dirname)
3 changes: 3 additions & 0 deletions tests/withQuerystring/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tests/withQuerystring/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import svg without querystring
import './image.svg'
import './subfolder/image.svg'

// import svg with querystring
import './image.svg?raw'
import './subfolder/image.svg?raw'
3 changes: 3 additions & 0 deletions tests/withQuerystring/subfolder/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/withQuerystring/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"files": ["index.ts"]
}

0 comments on commit 82ef357

Please sign in to comment.