简体   繁体   English

Webpack 编译但不提供文件

[英]Webpack compiling but not serving files

I am currently having trouble getting my index.js file to be served on localhost through Webpack.我目前无法通过 Webpack 在本地主机上提供我的index.js文件。

Webpack seems to compile without issue, and the webpack serve script does start the webpack-dev-server when used and serves my index.html file. Webpack 似乎编译没有问题,并且webpack serve脚本在使用时会启动 webpack-dev-server 并提供我的index.html文件。 However, after I created the webpack.config.js file, the problem is that the webpack script compiles the files but does not start the server.但是,在我创建了webpack.config.js文件后,问题是webpack脚本编译了文件但没有启动服务器。

package.json

{
  "name": "react-architecture",
  "version": "1.0.0",
  "description": "A tutorial on React Architecture",
  "main": "index.js",
  "scripts": {
    "serve": "webpack serve",
    "webpack": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Nick Norris",
  "license": "MIT",
  "devDependencies": {
    "webpack": "^5.46.0",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^3.11.2"
  }
}

webpack.config.js

const path = require("path");

module.exports = {
  mode: "development",
  entry: "./src/index.js",
  watch: true,
  output: {
    path: path.resolve(__dirname, "public/js"),
    filename: "main.bundle.js",
  },
};

Console output: npm run serve控制台输出: npm run serve

    $ npm run serve
    
    > react-architecture@1.0.0 serve C:\Users\18502\Documents\React Architecture
    > webpack serve
    
    [webpack-cli] No need to use the 'serve' command together with '{ watch: true }' configuration, it does not make sense.
    i 「wds」: Project is running at http://localhost:8080/
    i 「wds」: webpack output is served from /
    i 「wds」: Content not from webpack is served from C:\Users\18502\Documents\React Architecture
    i 「wdm」: asset main.bundle.js 368 KiB [emitted] (name: main)
    runtime modules 1010 bytes 5 modules
    modules by path ./node_modules/ 336 KiB
      modules by path ./node_modules/webpack-dev-server/client/ 20.9 KiB 10 modules
      modules by path ./node_modules/html-entities/lib/*.js 61 KiB 5 modules
      modules by path ./node_modules/webpack/hot/ 1.58 KiB 3 modules
      modules by path ./node_modules/url/ 37.4 KiB 3 modules
      modules by path ./node_modules/querystring/*.js 4.51 KiB
        ./node_modules/querystring/index.js 127 bytes [built] [code generated]
        ./node_modules/querystring/decode.js 2.34 KiB [built] [code generated]
        ./node_modules/querystring/encode.js 2.04 KiB [built] [code generated]
    modules by path ./src/ 131 bytes
      ./src/index.js 66 bytes [built] [code generated]
      ./src/components/add/add.js 65 bytes [built] [code generated]
    webpack 5.46.0 compiled successfully in 339 ms
    i 「wdm」: Compiled successfully.

Console output: npm run webpack.控制台输出: npm run webpack.

$ npm run webpack

> react-architecture@1.0.0 webpack C:\Users\18502\Documents\React Architecture
> webpack

asset main.bundle.js 4.24 KiB [compared for emit] (name: main) 
runtime modules 670 bytes 3 modules
cacheable modules 131 bytes
  ./src/index.js 66 bytes [built] [code generated]
  ./src/components/add/add.js 65 bytes [built] [code generated]
webpack 5.46.0 compiled successfully in 76 ms
assets by status 4.24 KiB [cached] 1 asset
cached modules 131 bytes (javascript) 670 bytes (runtime) [cached] 5 modules
webpack 5.46.0 compiled successfully in 10 ms

I have tried a few things, like renaming the files and moving them around in the file explorer to see if the output wasn't pointing to the correct file, but that did not work.我尝试了一些方法,例如重命名文件并在文件资源管理器中移动它们以查看输出是否未指向正确的文件,但这不起作用。 When using the webpack script, nothing I do to the files stops the compilation, even if I completely delete and then save code blocks.使用webpack脚本时,我对文件所做的任何事情都不会停止编译,即使我完全删除然后保存代码块也是如此。

This issue seems to be very similar, and I am getting the same "ERR_CONNECTION_REFUSED" message, but the local server runs fine when using webpack serve , which makes me think I am just missing something obvious in one of my configuration files. 这个问题似乎非常相似,我收到了相同的“ERR_CONNECTION_REFUSED”消息,但是在使用webpack serve时本地服务器运行良好,这让我觉得我只是在我的配置文件之一中遗漏了一些明显的东西。

You need to add the devServer attribute DevServer webpack-dev-server can be used to quickly develop an application.需要添加 devServer 属性 DevServer webpack-dev-server 可以用来快速开发应用。 See the development guide to get started.请参阅开发指南以开始使用。

This page describes the options that affect the behavior of webpack-dev-server (short: dev-server).本页描述了影响 webpack-dev-server(简称:dev-server)行为的选项。

Tip Options that are compatible with webpack-dev-middleware have 🔑 next to them.与 webpack-dev-middleware 兼容的选项旁边有🔑。 devServer object开发服务器对象

This set of options is picked up by webpack-dev-server and can be used to change its behavior in various ways.这组选项由 webpack-dev-server 选取,可用于以各种方式更改其行为。 Here's a rudimentary example that gzips and serves everything from our dist/ directory in the project root:这是一个基本示例,它对项目根目录中的 dist/ 目录中的所有内容进行 gzip 压缩和提供服务:

webpack.config.js

var path = require('path');

module.exports = {
  //...
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000,
  },
};

When the server is started, there will be a message prior to the list of resolved modules:当服务器启动时,在解析模块列表之前会有一条消息:

http://localhost:9000/ webpack output is served from /build/ Content not from webpack is served from /path/to/dist/ that will give some background on where the server is located and what it's serving. http://localhost:9000/ webpack 输出是从 /build/ 提供的而不是来自 webpack 的内容是从 /path/to/dist/ 提供的,这将提供有关服务器所在位置及其服务内容的一些背景信息。

If you're using dev-server through the Node.js API, the options in devServer will be ignored.如果您通过 Node.js API 使用 dev-server,则 devServer 中的选项将被忽略。 Pass the options as a second parameter instead: new WebpackDevServer(compiler, {...})将选项作为第二个参数传递: new WebpackDevServer(compiler, {...})

You are right that webpack will not start the server based on the build we configured.您是对的,webpack 不会根据我们配置的构建启动服务器。 Here is the code for the tutorial you watched.这是您观看的教程的代码。

https://github.com/chawk/react_architecture_tutorial https://github.com/chawk/react_architecture_tutorial

As Ernesto mentioned, you can have all of this done at once.正如 Ernesto 所提到的,您可以一次性完成所有这些工作。 In the tutorial, I just kept them separate to make it easier.在教程中,我只是将它们分开以使其更容易。 I used 2 separate command prompts, one to build and one to run the dev server.我使用了 2 个单独的命令提示符,一个用于构建,另一个用于运行开发服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM