问题描述
我用WebPACK中,以建立我的我的网站的JavaScript。
I use Webpack in order to build my javascript of my website.
一切工作完美,但我想打电话要求到模板(动态添加)。
Everything work perfectly but I would like to call require into a template (added dynamically).
我希望能够为需要构建后模块。 (要求没有被定义为全球范围内)。
I want to be able to require a module after the build. (require is not defined into the global context).
这可能吗?
THX
推荐答案
这是提供给你现在的选择是创建的您全局露出窗口
。我用下面的代码片段获得了成功:
An option which is available to you now is to create a context which you expose globally on window
. I've had success using the following snippet:
// Create a `require` function in the global scope so that scripts that have
// not been webpack'd yet can still access them.
window["require"] = function (module) {
return require("./public_modules/" + module + ".js");
}
基本上你正在做的是暴露一个文件夹的WebPack并告诉它打包到一个块该文件夹中的所有文件。然后,您可以键入 VAR MODULENAME =要求(模块名称)
A webpack'd脚本之外。
Basically what you're doing is exposing a folder to webpack and telling it to pack all the files in that folder in to a chunk. You can then type var moduleName = require("module-name")
outside of a webpack'd script.
只要上面的代码是被捆绑和评估一个文件中,你将有窗口
(巧合命名定义的函数规定,但你可以叫任何事情),这将使用的WebPack的需要的功能。
As long as the above snippet is inside a file which gets bundled and evaluated, you will have a function defined on window
(coincidentally named "require" but you can call it anything) which will use webpack's require functionality.
这篇关于需要使用的WebPack模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!