Skip to content

Commit

Permalink
fix: deal with url query in imports
Browse files Browse the repository at this point in the history
  • Loading branch information
stagas committed Jan 18, 2022
1 parent 9c69fa2 commit b9f3e1d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export const createServer = async (setup: ServerSetup) => {
try {
req.url = req.url!

if (req.url in responses) {
const [url] = req.url.split('?')

if (url in responses) {
const content = responses[req.url]
res.setHeader('Content-Type', content.type)
res.end(content.content)
return
}

let filename = path.resolve(path.join(setup.root, req.url))
let filename = path.resolve(path.join(setup.root, url))
let contents: string

if (filename.endsWith('.ts')) {
Expand Down
3 changes: 3 additions & 0 deletions src/test/fixture/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export const query = import.meta.url.split('?').pop()
11 changes: 11 additions & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('runInClient', () => {
expect(result).toEqual('foo')
})

it('server ignores query parameters', async () => {
const result = await runInClient(
{
root: path.resolve(path.join(__dirname, 'fixture')),
include: `import { query } from './query.ts?some-query'; window.query = query`,
},
() => (window as any).query
)
expect(result).toEqual('some-query')
})

it('errors', async () => {
await expect(
runInClient({}, () => {
Expand Down

0 comments on commit b9f3e1d

Please sign in to comment.