如何测试具有外部依赖项的Webpack捆绑包? [英] How to test a Webpack bundle that has external dependencies?

查看:153
本文介绍了如何测试具有外部依赖项的Webpack捆绑包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Webpacker和Rails的新手.最近,我转向使用Webpacker捆绑React组件.由于应用程序的一部分仍在使用资产管道,因此某些依赖项仍存储在vendor/assets/javascript中,并且可以作为全局变量进行访问. Webpack捆绑包将那些依赖关系声明为外部依赖关系,并且在服务该应用程序时,一切工作都很好,因为Rails提供了依赖关系,然后由捆绑包拾取这些依赖关系. 现在,我想开始使用jsdom之类的工具测试捆绑包.我正在使用具有与开发中相同配置的mocha-webpack.

I'm new to Webpacker and Rails in general. I recently moved to using Webpacker to bundle React components. Since part of the application still uses the asset pipeline, some dependencies are still stored in vendor/assets/javascript, and accessible as globals. The Webpack bundle declares those dependencies as external, and when serving the app, everything works great, as Rails serves the dependencies, which are then picked up by the bundle. Now I want to start testing the bundle using something like jsdom. I'm using mocha-webpack with the same config as in development.

在这种情况下如何加载外部依赖关系?如果我理解正确,则捆绑包可以很好地构建,但是在运行测试时,将不会加载依赖项.

How can I load the external dependencies in this case? If I understand correctly, the bundle builds fine, but when the tests run, the dependencies are not loaded.

推荐答案

考虑将另一个 webpack.config.js 用于测试版本,例如创建一个 webpack.config.test.js ,它不使用外部组件,而是将所有供应商文件捆绑为npm依赖项.

Consider using another webpack.config.js just for the test build, e.g. create a webpack.config.test.js that does not use externals, but bundles all vendor files as npm dependencies.

这篇关于如何测试具有外部依赖项的Webpack捆绑包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆