[英]Exporting a module built by webpack
I am writing 3 modules to be published as npm modules 我正在编写3个模块作为npm模块发布
package.json in Core: core中的package.json:
{
...
"main":"dist/bundle.js"
}
usage of core in Admin: 管理员中核心的使用情况:
import core from "core"
All modules are built by webpack. 所有模块都是由webpack构建的。
The problem is that if I use webpack to build Core
, generating bundle.js
, and have Admin
to import bundle.js
lots of libraries will be duplicated. 问题是,如果我使用webpack构建
Core
,生成bundle.js
并让Admin
导入bundle.js
很多库将被复制。
Eg Angular, bootstrap etc.. 例如Angular,bootstrap等。
What I am doing now is to import the entry file src/index.js
in admin
我现在正在做的是在
admin
导入条目文件src/index.js
package.json in Core: core中的package.json:
{
...
"main":"src/index.js"
}
usage of core in Admin: 管理员中核心的使用情况:
import core from "core"
but webpack cannot resolve some dependencies in core
correctly, for example, I have set up more than one root in webpack.config.js
in core
module, so that some short hand import is possible. 但是webpack无法正确解析
core
某些依赖关系,例如,我在core
模块中的webpack.config.js
中设置了多个根目录,以便可以进行一些简短的导入。
root: [
path.resolve(__dirname, './src/'),
path.resolve(__dirname, './src/directives')
]
// instead of relative path
import "../../myDirective.js"
// I can use
import "myDirective.js"; // where myDirective.js is under "src/directives/myDirective.js", but this fails when I try to build `admin` module.
Is there a simpler way for webpack to build npm-modules so that it is easy to import? webpack是否有更简单的方法来构建npm-modules以便于导入?
Core: depends on angular, bootstrap, core.js
Admin: depends on angular, bootstrap, admin.js, includes core as a module
It's interesting to me too. 这对我来说也很有趣。 I suggest it can be done by setting webpack's library options .
我建议可以通过设置webpack的库选项来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.