【问题标题】:Mimetype Error In Webpack Output (using knockout)Webpack 输出中的 Mimetype 错误(使用敲除)
【发布时间】:2017-11-30 16:32:47
【问题描述】:

这是我第一次使用 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 放在浏览器中,捆绑包仍然存在错误:

"Uncaught SyntaxError: Unexpected token

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

【问题讨论】:

    标签: node.js knockout.js webpack mime-types webpack-dev-server


    【解决方案1】:

    对于遇到相同问题的任何人,我通过在模块属性下的 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']
                }
              },
            ]
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 2015-05-26
      • 1970-01-01
      • 2012-11-23
      • 2014-10-19
      • 1970-01-01
      • 2013-02-27
      • 2017-07-15
      • 2016-03-04
      相关资源
      最近更新 更多