【发布时间】:2017-09-03 06:53:25
【问题描述】:
我有一个非常简单的 webapp,其中 WebPack 将 javascript 捆绑到一个 bundle.js 文件中,供各种 html 页面使用。
不幸的是,即使我在 webpack 配置文件中指定我想将它用作脚本标签可以使用的独立库(libraryTarget 和library),它也不起作用。一切似乎都封装在模块中,所以我的功能不可用。
index.html
<!DOCTYPE html>
<html lang="EN">
<head>
<title>Play! Webpack</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<app>
Loading...
</app>
<script type="text/javascript" src="/bundles/bundle.js" charset="utf-8"></script>
<button type="button" onclick="ui.helloWorld()">Click Me!</button>
</body>
</html>
我的 webpack.base.config.js 的入口和输出部分
entry: [
'./app/main.js'
],
output: {
path: buildPath,
filename: 'bundle.js',
sourceMapFilename: "bundle.map",
publicPath: '/bundles/',
libraryTarget: 'var',
library: 'ui'
},
main.js(入口点)
function helloWorld() {
alert( 'Hello, world!' );
}
单击我的按钮时,我在控制台中收到此错误:
Uncaught TypeError: ui.helloWorld is not a function
at HTMLButtonElement.onclick (localhost/:14)
对于附加信息,我的 bundle.js 文件的内容如下所示:
var ui = ...
(stuff here)
function(module, exports, __webpack_require__) {
__webpack_require__(79);
function helloWorld() {
alert('Hello, world!');
}
/***/ }
【问题讨论】:
标签: javascript module webpack