如何使用webpack从控制台要求()? [英] How do I require() from the console using webpack?
问题描述
如何从控制台要求()/导入模块?例如,假设我已经安装了ImmutableJS npm,我想在控制台中使用模块中的功能。
How do I require() / import modules from the console? For example, say I've installed the ImmutableJS npm, I'd like to be able to use functions from the module while I'm working in the console.
推荐答案
将这个包含在一个模块中将允许 require([modules],function)
从浏览器中使用
Including this in a module will allow require([modules], function)
to be used from a browser
window['require'] = function(modules, callback) {
var modulesToRequire = modules.forEach(function(module) {
switch(module) {
case 'immutable': return require('immutable');
case 'jquery': return require('jquery');
}
})
callback.apply(this, modulesToRequire);
}
示例用法:
require(['jquery', 'immutable'], function($, immutable) {
// immutable and $ are defined here
});
注意:每个switch-statement选项都应该是这个模块的一部分要求或由ProvidePlugin提供
Note: Each switch-statement option should either be something this module already requires, or provided by ProvidePlugin
在 此答案 上,可用于添加整个文件夹。
Based on this answer, which can be used to add an entire folder.
来自 Webpack文档 - 它允许像 require.yourModule.function()
这篇关于如何使用webpack从控制台要求()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!