Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not resolve './foo' from example/index.js #4

Closed
ws456999 opened this issue May 12, 2020 · 8 comments
Closed

Could not resolve './foo' from example/index.js #4

ws456999 opened this issue May 12, 2020 · 8 comments

Comments

@ws456999
Copy link

when i move foo.tsx to foo/index.tsx

cound't resolve module correctly

@aelbore
Copy link
Contributor

aelbore commented May 12, 2020

@ws456999 can you try this?

foo/index.tsx

export default class Foo {
  render() {
    return <div className="hehe">hello there!!!</div>
  }
}

index.js

import Foo from './foo/index'
console.log(Foo)

@ws456999
Copy link
Author

ws456999 commented May 12, 2020

@aelbore

foo/index.tsx

export default class Foo {
  render() {
    return <div className="hehe">hello there!!!</div>
  }
}

index.js

import Foo from './foo'
console.log(Foo)

works in rollup with rollup-plugin-typescript

my project have too much such case,
Locate modules using the Node resolution algorithm in webpack,how can i use same rule with some config or plugins?

@aelbore
Copy link
Contributor

aelbore commented May 12, 2020

@ws456999 the thing is if you have foo/index.tsx and foo.tsx exist

@ws456999
Copy link
Author

ws456999 commented May 12, 2020

@aelbore It's two question,

  1. esbuild don't resolve module like Node resolution algorithm;

  2. the priority of location modules

I only care about the first question,Do you have any ideas?

@aelbore
Copy link
Contributor

aelbore commented May 12, 2020

@aelbore
not sure if this is work around or this is the fixed
if foo.tsx exist load foo.tsx even if foo/index.tsx exist
else load the foo/index.tsx

and yes esbuild doesnt resolve like Node resolution

  const coreModule = (resolved: string) => {
    if (existsSync(resolved) && statSync(resolved).isDirectory()) {
      for (const loader of loaders) {
        const file = join(resolved, `index.${loader}`)
        if (existsSync(file)) {
          return file
        }
      }
    }
    return null
  }

  const loadFile = (resolved: string) => {
    for (const loader of loaders) {
      const file = `${resolved}.${loader}`
      if (existsSync(file)) {
        return file
      }
    }
    return null
  }

        let file = loadFile(resolved)
        if (file) return file
        if (!file) {
          file = coreModule(resolved)
          if (file) return file
        }

@ws456999
Copy link
Author

@aelbore
it works for me! thanks a lot.

@ws456999
Copy link
Author

looking forward to your next version

@aelbore
Copy link
Contributor

aelbore commented May 12, 2020

looking forward to your next version
@ws456999
#5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants