# 开发

# mode

设置开发模式

const path = require('path');

module.exports = {
    entry: {
        app: './src/index.js',
        print: './src/print.js'
    },
    output: {
        filename: '[name].bundle.[hash:8].js',
        path: path.resolve(__dirname, '..', 'dist')
    },
    mode: "development"
};

# source map

将编译后的代码映射回原始源代码

const path = require('path');

module.exports = {
    entry: {
        app: './src/index.js',
        print: './src/print.js'
    },
    output: {
        filename: '[name].bundle.[hash:8].js',
        path: path.resolve(__dirname, '..', 'dist')
    },
    mode: "development",
    devtool: 'source-map'
};

# watch

{
    "scripts": {
         "watch": "webpack --watch"
    }
}

# webpack-dev-server

提供了一个简单的 web 服务器,并且能够实时重新加载(live reloading)。让我们设置以下:

刚刚遇到一个问题,Error: Cannot find module 'webpack-cli/bin/config-yargs',与 webpack-cli 版本不兼容,降级 webpack-cli@3.3.12

webpack 配置

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, '..', 'dist')
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        })
    ]
};

package.json

{
    "scripts": {
       "dev": "webpack-dev-server --config config/webpack.config.js"
    }
}