简体   繁体   English

在RequireJS缓存中制作Webpack注册模块

[英]Making Webpack register modules in RequireJS cache

Currently I'm using RequireJS for all my modules. 目前,我所有模块都使用RequireJS。 I'm considering using Webpack for my main project but need to load modules not known during build time. 我正在考虑将Webpack用于我的主要项目,但需要加载在构建期间未知的模块。 Like plugins. 像插件。

One approach would be to use Webpack at build time and then use RequireJS at runtime. 一种方法是在构建时使用Webpack,然后在运行时使用RequireJS。 The only problem is that files loaded from Webpack bundle won't be found in the RequireJS cache. 唯一的问题是,在RequireJS缓存中找不到从Webpack捆绑包加载的文件。

If I manually register them it works: 如果我手动注册它们,它将起作用:

import jQuery from 'jquery';
define('jquery', [], function() { return jQuery; });

But is there some easier way? 但是有没有更简单的方法? Like Webpack generating code that does this? 就像Webpack生成的代码那样吗?

I ended up writing a custom loader that added the code at the end of each file. 我最终编写了一个自定义加载器,该加载器在每个文件的末尾添加了代码。 Using window.define instead of define makes Webpack leave it intact for RequireJS. 使用window.define而不是define可以使Webpack对于RequireJS保持完整。

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

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