我使用Webpack来构建我的网站Javascript代码。
一切都运行得很完美,但我想在模板中(动态添加)调用require。
我想要在构建之后能够调用一个模块。(require未定义在全局上下文中)。
是否可能?
谢谢
我使用Webpack来构建我的网站Javascript代码。
一切都运行得很完美,但我想在模板中(动态添加)调用require。
我想要在构建之后能够调用一个模块。(require未定义在全局上下文中)。
是否可能?
谢谢
window
。我使用以下代码片段取得了成功:// 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");
}
var moduleName = require("module-name")
。window
上定义一个函数(巧合的是名为“require”,但你可以取任何名字),它将使用webpack的require功能。