Webpack 输出中的 Mimetype 错误(使用 knockout)
Mimetype Error In Webpack Output (using knockout)
这是我第一次使用 Knockout。我想将它与节点、webpack 和 es6 一起使用,但由于某种原因,我创建的包导致浏览器出错:
"Refused to execute script from 'http://localhost:8080/build/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."
我的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 放在浏览器中,包仍然存在错误:
"Uncaught SyntaxError: Unexpected token < for build/bundle.js:1"
有没有办法指定包输出的 mimetype?或者我的 webpack.config.js 有误?任何帮助将不胜感激。
对于任何 运行 遇到同样问题的人,我通过在模块 属性 下的 webpack 配置文件中包含一个 noParse 来阻止 webpack 解析淘汰构建来解决我的问题。最终的 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']
}
},
]
}
};
这是我第一次使用 Knockout。我想将它与节点、webpack 和 es6 一起使用,但由于某种原因,我创建的包导致浏览器出错:
"Refused to execute script from 'http://localhost:8080/build/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."
我的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 放在浏览器中,包仍然存在错误:
"Uncaught SyntaxError: Unexpected token < for build/bundle.js:1"
有没有办法指定包输出的 mimetype?或者我的 webpack.config.js 有误?任何帮助将不胜感激。
对于任何 运行 遇到同样问题的人,我通过在模块 属性 下的 webpack 配置文件中包含一个 noParse 来阻止 webpack 解析淘汰构建来解决我的问题。最终的 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']
}
},
]
}
};