gpt4 book ai didi

node.js - Webpack 输出中的 Mimetype 错误(使用 knockout )

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:12 26 4
gpt4 key购买 nike

这是我第一次使用Knockout。我想将它与 Node、Webpack 和 es6 一起使用,但由于某种原因,我创建的包在浏览器中导致错误:

“拒绝执行来自‘http://localhost:8080/build/bundle.js’的脚本,因为其 MIME 类型(‘text/html’)不可执行,并且启用了严格的 MIME 类型检查。”

我的 webpack.config.js 是:

const path = require('path')


module.exports = {
output: {
path: path.resolve('build', './bundle.js'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.html$/, loader: 'html'
},
{
test: /\.js?$/,
exclude: /(node_modules)/,
loader: 'babel-loader',

},
]
}
};

package.json 文件是:

{
"name": "neighborhoodmap",
"version": "1.0.0",
"description": "Neighborhood Map App with API Calls",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"build": "webpack"
},
"author": "",
"license": "ISC",
"dependencies": {
"knockout": "^3.4.2"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"html-loader": "^0.5.1",
"imports-loader": "^0.7.1",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.5"
}
}

app.js 文件是:

import ko from 'knockout';


class MyApp {
constructor() {
this.message = "Hello World";
}
}

ko.applyBindings(new MyApp())

实际的index.html是:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Neighborhood Map</title>
</head>
<body>
<h1 data-bind="text: message"></h1>

<script type="text/javascript" src="build/bundle.js"></script>
</body>
</html>

我正在使用 webpack-dev-server 为应用程序提供服务,但即使我只是将 index.html 放入浏览器中, bundle 仍然存在错误:

“未捕获的语法错误:意外的标记 < for build/bundle.js:1”

有没有办法指定捆绑输出的 mimetype?或者也许我的 webpack.config.js 是错误的?任何帮助将不胜感激。

最佳答案

对于遇到同样问题的人,我通过在模块属性下的 webpack 配置文件中包含 noParse 来解决我的问题,以阻止 webpack 解析 knockout 构建。最终的 webpackconfig.js 文件如下所示:

const path = require('path');


module.exports = {
devtool: 'sourcemap',
entry: './app.js',
output: {
path: path.resolve('build', ''),
filename: 'bundle.js'
},
module: {
noParse: /node_modules\/knockout\/build\/output\/*.js/,
loaders: [
{
test: /\.html$/, loader: 'html'
},
{
test: /\.js?$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
]
}
};

关于node.js - Webpack 输出中的 Mimetype 错误(使用 knockout ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47578213/

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