Webpack 从/folder/folder.js 而不是/folder/index.js 导入

Webpack 从/folder/folder.js 而不是/folder/index.js 导入

问题描述:

使用 webpack,当我在 ./blah/中创建 index.js 然后执行 import blah from './blah' 它成功地从 index.js 获取

With webpack when I create a index.js in ./blah/ then do import blah from './blah' it succesfully gets from index.js

但是,我的代码编辑器现在充满了称为 index.js 的选项卡.并按 Ctrl+Shift+P 查找现在有一堆 index.js

However, my code editor is now FULL of tabs called index.js. And doing a Ctrl+Shift+P to find now has a BUNCH of index.js's

所以我一直把index.js的名字做的跟文件夹的名字一样.所以/folder/folder.js.但是,如果我这样做,我所有的导入语句现在都是:

Therefore I have been doing name index.js's the same as the folder name. So /folder/folder.js. However if I do this, all my import statements are now:

import blah from './blah/blah`

现在输入一个额外的 /blah 对我来说并不是太冗长.但我只是想知道,是否可以让它像对待 index.js 一样对待与文件夹同名的 js 文件?

Now it's not too verbose for me to just type an extra /blah. But I was just wondering, is it possible to make it treat the js file with the same name as the folder as it would index.js?

另一种解决此问题的方法是在每个文件夹中创建一个 package.json ,如下所示:

Another solution to work around this problem is to create a package.json inside of each folder like this:

{
   "main":"blah.js"
}