网络包 bundle.js 未找到

webpack bundle.js not found

我使用 webpack 开发服务器,但无法访问我的 bundle.js 文件。

编辑:我正在使用 this webpack config 没有 bower-webpack-plugin.

package.json

{
  "name": "react_modules",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "npm run serve | npm run dev",
    "serve": "./node_modules/.bin/http-server -p 8080",
    "dev": "webpack-dev-server -d --progress --colors --port 8090"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.0.20",
    "babel-loader": "^6.0.1",
    "babel-preset-es2015": "^6.0.15",
    "bootstrap": "^3.3.5",
    "bower-webpack-plugin": "^0.1.9",
    "css-loader": "^0.21.0",
    "events": "^1.1.0",
    "extract-text-webpack-plugin": "^0.9.0",
    "file-loader": "^0.8.4",
    "history": "^1.13.0",
    "http-server": "^0.8.5",
    "jquery": "^2.1.4",
    "jquery-ui": "^1.10.5",
    "json-markup": "^0.1.6",
    "jsx-loader": "^0.13.2",
    "less": "^2.5.3",
    "less-loader": "^2.2.1",
    "lodash": "^3.10.1",
    "node-sass": "^3.4.1",
    "object-assign": "^4.0.1",
    "path": "^0.12.7",
    "react": "^0.14.2",
    "react-dom": "^0.14.2",
    "react-hot-loader": "^1.3.0",
    "react-router": "^1.0.0-rc4",
    "sass-loader": "^3.1.1",
    "style-loader": "^0.13.0",
    "svg-sprite-loader": "0.0.11",
    "url-loader": "^0.5.6",
    "webpack": "^1.12.2",
    "webpack-dev-server": "^1.12.1"
  }
}

webpack.config.js

const BowerWebpackPlugin = require("bower-webpack-plugin");

module.exports = {
    entry: './src/index.jsx',
    output: {
        filename: 'bundle.js', 
        sourceMapFilename: "[file].map",
        publicPath: 'http://localhost:8090/assets'
    },
    debug: true,
    devtool: 'inline-source-map',
    module: {
        loaders: [
          {   
              test: /\.js[x]?$/, 
              loaders: ['react-hot', 'babel'],
              exclude: /node_modules/ 
            },
            {
              test: /\.scss$/,
              loaders: [ 'style', 'css?sourceMap', 'sass?sourceMap' ]
            },
              {
                test: /\.less$/,
                loaders: [ 'style', 'css?sourceMap', 'less?sourceMap' ]
              },
              {
                test: /\.css$/,
                loaders: [ 'style', 'css']
              },
              { test: /\.woff$/,   loader: "url-loader?limit=10000&mimetype=application/font-woff" },
              { test: /\.woff2$/,   loader: "url-loader?limit=10000&mimetype=application/font-woff2" },
              { test: /\.(eot|ttf|svg|gif|png)$/, loader: "file-loader" }
        ]
    },
    plugins: [
        new BowerWebpackPlugin()
    ],
    resolve: {
        extensions: ['', '.js', '.jsx']
    }
};

index.html

<!DOCTYPE html>
<html>
<head>
    <title>App</title>
</head>
<body>
    <div id="content">
        <!-- this is where the root react component will get rendered -->
    </div>
    <!-- include the webpack-dev-server script so our scripts get reloaded when we make a change -->
    <!-- we'll run the webpack dev server on port 8090, so make sure it is correct -->
    <script src="http://localhost:8090/webpack-dev-server.js"></script>
    <!-- include the bundle that contains all our scripts, produced by webpack -->
    <!-- the bundle is served by the webpack-dev-server, so serve it also from localhost:8090 -->
    <script type="text/javascript" src="http://localhost:8090/assets/bundle.js"></script>
</body>
</html>

控制台输出

and:react_modules and$ npm run start

> react_modules@1.0.0 start /Users/and/devel/react_modules
> npm run serve | npm run dev


> react_modules@1.0.0 dev /Users/and/devel/react_modules
> webpack-dev-server -d --progress --colors --port 8090

  0% compilehttp://localhost:8090/webpack-dev-server/
webpack result is served from http://localhost:8090/assets
content is served from /Users/and/devel/react_modules
Hash: dd2f28f3fef16a34048b  
Version: webpack 1.12.2
Time: 612ms

ERROR in ./src/index.jsx
Module build failed: SyntaxError: /Users/and/devel/react_modules/src/index.jsx: Unexpected token (5:2)
  3 | 
  4 | ReactDOM.render(
> 5 |   <h1>Hello, world!</h1>,
    |   ^
  6 |   document.getElementById('content')
  7 | );
  8 | 
    at Parser.pp.raise (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:24:13)
    at Parser.pp.unexpected (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:91:8)
    at Parser.pp.parseExprAtom (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:507:12)
    at Parser.pp.parseExprSubscripts (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:260:19)
    at Parser.pp.parseMaybeUnary (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:240:19)
    at Parser.pp.parseExprOps (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:171:19)
    at Parser.pp.parseMaybeConditional (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:153:19)
    at Parser.pp.parseMaybeAssign (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:120:19)
    at Parser.pp.parseExprListItem (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:966:16)
    at Parser.pp.parseCallExpressionArguments (/Users/and/devel/react_modules/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:336:20)
webpack: bundle is now VALID.

当我 运行 开发服务器时,我收到 404 Not Found http://localhost:8090/assets/bundle.js

我会尝试将 index.html 文件中的脚本路径更改为:

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

根据您的 package.json,您正在使用新的 babel 6 release. However you don't have all the required dependencies for this new release. According to babel-loader,您还需要安装 babel-preset-es2015

npm install babel-loader babel-core babel-preset-es2015 --save-dev

由于你也在使用React,你也需要安装react preset。所以同时安装:

npm install --save-dev babel-preset-es2015 babel-preset-react

在您的包文件夹中创建一个名为 .babelrc 的文件以保留 babel configuration 并启用两个预设:

.babelrc

{
  "presets": ["es2015", "react"]
}

然后再次 运行 服务器:

npm run dev

然后 http://localhost:8090/assets/bundle.js 应该显示捆绑的模块。

我的诊断:

您无法获得 bundle.js,因为您的 npm run dev 在尝试应用 babel 加载程序时抛出一些错误,因为它没有正确配置。然后,当您尝试请求 bundle.js 文件时,您会收到 404 错误,因为 它尚未生成

问题主要与指向 index.html 中的 bundle js 有关。找不到 webpack bundle.js 的原因是因为你需要在 index.html 中指定绝对路径。假设你的 bundle.js 和 index.html 是在 dist 文件夹下生成的,那么它应该像下面这样

<script src="/bundle.js"></script>

.env 文件中指定主页

PUBLIC_URL=https://example.com

这对我有用。