webpack-dev-server不会在启动时创建初始捆绑包

时间:2018-08-03 18:38:10

标签: node.js ubuntu webpack webpack-dev-server

我已经构建了一个基本的babel webpack入门项目,该项目是我从git克隆的。 git存储库没有dest / output / build文件夹,也没有任何初始构建文件。我知道要使用webpack,所以我假设webpack-dev-server启动时会创建一个文件夹(在我的情况下为“ build”)并进行源文件的首次编译。相反,只有在初始编译将webpack-dev-server编译更改之后,我才被迫手动启动该过程。

我想念什么?

package.json

{
  "name": "babel-webpack-starter",
  "version": "1.0.0",
  "description": "A Babel Webpack Stater Pack for compiling ES2015/ES6, ES2016/ES7 and ES2017 code doen to ES5. This will allow for the use of ES6 modules and later ECMAScript features such as async/await. Also includes a webpack-dev-server to auto load server without having to re-compile.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode=development",
    "start": "webpack-dev-server --output-public-path=/build/ --mode=development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.16.4",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  }
}

webpack.config.js

 // Load Node modules
const path = require('path');

// Export modules
module.exports = {
    entry: {
        app: './src/app.js'        
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'app.bundle.js'
    },
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: '/node_modules',
            loader: 'babel-loader',
            query: {
                presets: ['env']
            }
        }]
    }
};

1 个答案:

答案 0 :(得分:2)

webpack-dev-server不会将您的app.bundle.js放在您的工作目录中,它将在内存中编译文件。

如果要访问文件,请在输出选项中添加公共路径或在npm start脚本中已完成的操作。

output: {
    ...
    publicPath: '/build/'
}

现在,您的文件将在localhost:<port>/build/app.bundle.js中可用

您可以签出this site以获得更多信息