简体   繁体   English

如何在 NPM 包中使用 webpack 动态导入?

[英]How do I use webpack dynamic imports inside an NPM package?

I have a navbar npm package that I import into other projects.我有一个导航栏 npm 包,可以导入到其他项目中。 It dynamically imports FontAwesome icons based on a JSON config file loaded from a web server at runtime.它根据运行时从 Web 服务器加载的 JSON 配置文件动态导入 FontAwesome 图标。

When I build the navbar package, the entire FontAwesome library ends up as chunks in the build directory, as I would expect.当我构建 navbar 包时,整个 FontAwesome 库在构建目录中以块的形式结束,正如我所期望的那样。

When I import that navbar module in another project, that project's build does not include the FontAwesome chunks.当我在另一个项目中导入该导航栏模块时,该项目的构建不包含 FontAwesome 块。 So the dynamic import HTTP calls get a 404.所以动态导入 HTTP 调用得到 404。

I can't find much about this use case online, possibly because I don't know the terms to look for, possibly because this is a terrible idea in the first place.我在网上找不到很多关于这个用例的信息,可能是因为我不知道要查找的术语,可能是因为这首先是一个糟糕的主意。

How can I get webpack to see that a package has dynamic imports and to copy over the necessary chunks to the build directory?我怎样才能让 webpack 看到一个包有动态导入并将必要的块复制到构建目录?

You can try running npm publish --dry-run to see what gets published and if its missing anything compared to your build folder.您可以尝试运行npm publish --dry-run以查看发布的内容以及与您的构建文件夹相比是否缺少任何内容。

You can also try running npm publish <path to build folder> .您也可以尝试运行npm publish <path to build folder>

Full documentation here: https://docs.npmjs.com/cli/v7/commands/npm-publish完整文档在这里: https : //docs.npmjs.com/cli/v7/commands/npm-publish

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM