gpt4 book ai didi

javascript - 使用 webpack 和 two.js 的问题

转载 作者:搜寻专家 更新时间:2023-11-01 00:05:13 25 4
gpt4 key购买 nike

我正在使用 webpack 编写网络应用程序和 two.js .为了完整起见,这些是我的依赖项:

"devDependencies": {
"jasmine-core": "^2.8.0",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.0",
"karma-webpack": "^2.0.4",
"webpack": "^3.5.5"
},
"dependencies": {
"two.js": "^0.6.0"
}

我有以下 webpack.config.js:

const path = require('path');
module.exports = {
entry: './myentry.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'out')
}
};

入口文件 myentry.js 只是导入了 two.js:

import * as two from "two.js";

绑定(bind)成功...

two.js 是我的 npm 依赖项的一部分,因此可以在 node_modules 文件夹中的本地 Node 模块中找到它。当我继续创建 bundle 时:

webpack --config webpack.config.js

成功,我得到以下输出:

Hash: 5e762def59fa65ff8487
Version: webpack 3.5.5
Time: 468ms
Asset Size Chunks Chunk Names
bundle.js 258 kB 0 [emitted] [big] main
[0] ./myentry.js 271 bytes {0} [built]
+ 1 hidden module

生成的包可用 here .

但是……运行失败:

所以我在我的 HTML 页面中使用了 bundle:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My stuff</title>
<script type="module" src="./out/bundle.js"></script>
</head>
<body></body>
</html>

当我在 Chrome 中打开页面时(我使用 http-server 来避免与 CORS 相关的问题),我在F12 工具中遇到这些错误:

bundle.js:1674 Uncaught TypeError: Cannot read property 'Backbone' of undefined
at bundle.js:1674
at Object.<anonymous> (bundle.js:1831)
at __webpack_require__ (bundle.js:20)
at Object.root (bundle.js:72)
at __webpack_require__ (bundle.js:20)
at Object.defineProperty.value (bundle.js:63)
at bundle.js:66

发生了什么事?

最佳答案

看起来问题是我在 HTML 中引用包的方式:

<script type="module" src="./out/bundle.js"></script>

这是不正确的,因为生成的包不是 ES6 模块 :(

<script type="application/javascript" src="./out/bundle.js"></script>

这会使错误消失。它实际上会导致其他错误,但它们与主干完全无关。

关于javascript - 使用 webpack 和 two.js 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com