我有一个项目,使用多个入口点来创建一些不同的应用程序。 目录结构如下所示:
. ├── app-1 │ └── components │ ├── a.js │ └── b.js ├── app-2 │ └── components ├── app-3 │ └── components ├── package.json └── webpack.config.js在任何应用程序中,我希望能够在不引用应用程序名称的情况下导入文件。 例如,在app-1/components/a.js我希望能够导入b.js
import B from 'components/b'
并让webpack知道这是指app-1因为那是文件存在的地方,而不是说app-3 ,以防在那里存在相同的路径。
通过webpack.config.js的以下设置:
... resolve: { modules: [ path.resolve(__dirname, 'app-3'), path.resolve(__dirname, 'app-2'), path.resolve(__dirname, 'app-1'), 'node_modules', ], } ...app-3将具有最高优先级。 我真正喜欢的是没有优先权,而是根据它所在的应用搜索一个模块。
不知道我是否正确地解释了这一点,但我已经搜索了网络寻找解决方案并找不到一个解决方案。 会爱上一个简单的解决方案和一个暴躁的“你甚至谷歌”
I have a project that uses multiple entry points to create a few distinct apps. The directory structure looks something like:
. ├── app-1 │ └── components │ ├── a.js │ └── b.js ├── app-2 │ └── components ├── app-3 │ └── components ├── package.json └── webpack.config.jsWithin any of the apps, I would like to be able to import files without referencing the app name. For example, in app-1/components/a.js I would like to be able to import b.js with
import B from 'components/b'
and have webpack know that this is referring to app-1 because that's where the file lives, and not, say, app-3 in case the same path exists there.
With the following setup in webpack.config.js:
... resolve: { modules: [ path.resolve(__dirname, 'app-3'), path.resolve(__dirname, 'app-2'), path.resolve(__dirname, 'app-1'), 'node_modules', ], } ...app-3 will have the highest priority. What I would really like is to have no priority, but instead search for a module based on the app it lives in.
Not sure if I'm explaining this properly, but I've scoured the web for a solution and can't find one. Would love for someone to come along with a easy fix and a snarky "do you even google"
最满意答案
这个插件可以帮助: https : //www.npmjs.com/package/resolve-entry-modules-webpack-plugin 。
它完全符合你的要求,将条目添加到解析路径中,同时保持不同的条目不会彼此导入。
This plugin could help: https://www.npmjs.com/package/resolve-entry-modules-webpack-plugin.
It does exactly what you want, add the entry to the resolution path while keeping distinct entries not importing from each other.
更多推荐
发布评论