Skip to content

Commit

Permalink
fix: filenames like component.utils.ts are not resolved properly, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Aug 15, 2022
1 parent 14c0b99 commit fde5950
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, statSync } from 'fs'
import { extname, resolve, dirname, join, basename } from 'path'
import { extname, resolve, dirname, join } from 'path'
import { Plugin as RollupPlugin } from 'rollup'
import { transform, Loader, TransformOptions } from 'esbuild'
import { MarkOptional } from 'ts-essentials'
Expand Down Expand Up @@ -83,9 +83,11 @@ export default ({
)

const resolveFile = (resolved: string, index: boolean = false) => {
const fileWithoutExt = join(dirname(resolved), basename(resolved, extname(resolved)))
const fileWithoutExt = resolved.replace(/\.[jt]sx?$/, '')
for (const ext of extensions) {
const file = index ? join(resolved, `index${ext}`) : `${fileWithoutExt}${ext}`
const file = index
? join(resolved, `index${ext}`)
: `${fileWithoutExt}${ext}`
if (existsSync(file)) return file
}
return null
Expand Down
12 changes: 9 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,24 +285,30 @@ describe('esbuild plugin', () => {
console.log(Foo)
`,
'./fixture/foo.tsx': `
import {util} from './some.util'
export default class Foo {
render() {
return <div className="hehe">hello there!!!</div>
return <div className="hehe">hello there!!!{util}</div>
}
}
`,
'./fixture/some.util.ts': `
export const util = 42
`,
})

const output = await build({
dir,
rollupPlugins: [esbuild({})],
})
expect(output[0].code).toMatchInlineSnapshot(`
"class Foo {
"const util = 42;
class Foo {
render() {
return /* @__PURE__ */ React.createElement(\\"div\\", {
className: \\"hehe\\"
}, \\"hello there!!!\\");
}, \\"hello there!!!\\", util);
}
}
Expand Down

0 comments on commit fde5950

Please sign in to comment.