0

I have the following folder structure:

src/
   Foo/
      index.ts
   Bar/
      index.ts
   index.ts

I want import the module Foo like this import Foo from "./Foo"; in my src/index.ts file. How is this done? Because webpack doesn't autoimport the index.ts file from Foo/ and Bar/ when I want to "import folder".

1
  • Hi @ZulusK , did my answer solve your problem? Commented May 2, 2018 at 18:15

1 Answer 1

2

You don't have to import folder as you mentioned. It's actually done by webpack resolve https://webpack.js.org/configuration/resolve/#resolve-extensions

By default webpack is looking for .js and .json extensions that means when you import import Foo from './Foo' it will try to look ./Foo/index.js or ./Foo/index.json if it is found, it is automatically imported. If you need to extend this to typescript you need to specify

resolve: {
 extensions: ['.js', '.json', '.ts']
}

of course in this way you have to specify ts-loader to handle typescript files https://github.com/TypeStrong/ts-loader which will take care of transpilation from typescript when it is imported.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.