Webpack-dev-server不会在dist中放置bundle

时间:2018-02-22 20:24:30

标签: webpack webpack-dev-server

我正在尝试为我想要构建的网站设置Webpack配置。我想将SASS编译为CSS并将其放入dist文件夹中。当我运行npm run build时,它工作正常,但当我运行npm run watch来触发Webpack-dev服务器时,它不会将index.js编译为bundle.js,它不会出现在dist文件夹中。我的webpack.config.js有什么问题吗?

的index.html

<html>
    <head>
        <title>Getting Started</title>
        <link rel="stylesheet" href="dist/css/main.min.css">
    </head>
    <body>
        test
        <script src="dist/scripts/bundle.js"></script>
    </body>
</html>

webpack.config.js

const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const extractSass = new ExtractTextPlugin({
    filename: "../css/main.min.css",
    disable: process.env.NODE_ENV === "development"
});

module.exports = {
  entry: './src/scripts/index.js',
  output: {
    path: path.resolve(__dirname, 'dist/scripts'),
    filename: 'bundle.js',
    publicPath: 'dist/scripts'
  },
  module: {
    rules: [
        {
            test: /\.scss$/,
            use: extractSass.extract({
                use: [{
                    loader: "css-loader"
                }, {
                    loader: "sass-loader"
                }],

                fallback: "style-loader"
            })
        }   
    ]
  },
  plugins: [
    extractSass
  ]
};

的package.json

{
  "name": "project1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --optimize-minimize",
    "watch": "webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^0.28.9",
    "extract-text-webpack-plugin": "^3.0.2",
    "node-sass": "^4.7.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.20.2",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
  },
  "dependencies": {}
}

4 个答案:

答案 0 :(得分:4)

您可以通过webpack-dev-server查看生成的URL到此URL。

http://localhost:8080/webpack-dev-server(根据需要更改主机和端口)

webpack-dev-server不会将文件写入磁盘,但你可以在那里看到它们。

答案 1 :(得分:2)

webpack-dev-server来自内存。如果您希望在使用webpack-dev-server进行开发期间查看磁盘上的文件,则需要同时运行标准webpack版本。有关详细信息,请参阅this answer

答案 2 :(得分:2)

dev服务器可以选择 writeToDisk

  

module.exports = {// ... devServer:{writeToDisk:true}};

检查此: https://webpack.js.org/configuration/dev-server/#devserverwritetodisk-

答案 3 :(得分:1)

您已经注意到dist / scripts没有更新。
就像其他人所说的那样,webpack-dev-server只将其保留在内存中。
但是您可以利用这一点。

将此添加到您的webpack.config.js:

module.exports = {
...
  devServer: {
    open: true,             // Automatically open the browser
    hot: true,              // Automatically refresh the page whenever bundle.js 
    publicPath: '/dist/scripts',
  },
...
};

publicPath:'/ dist / scripts'
这就是魔法所在。
默认情况下,webpack-dev-server在localhost:8080/(例如localhost:8080/bundle.js)上提供配置的 output.filename
我们对其进行了更改,以使其在localhost:8080/dist/scripts/(例如localhost:8080/dist/scripts/bundle.js)上提供内容。
现在,您的index.html将能够成功找到<script src="dist/scripts/bundle.js"></script>

注意:您必须通过localhost:8080访问index.html进行此项工作。
例如。从file://...或其他主机/端口进行访问将不起作用,因为它将在系统上查找实际文件(当然不会更新)。


或者,如果您使用output: { filename: '/dist/scripts/bundles.js } }而不是output: { filename: 'bundles.js } },则不需要 publicPath