安装
二、安装webpack
使用webpack之前需要安装webpack,在这里我们需要在两个地方安装:全局目录和项目目录,在项目目录下执行以下命令:
//全局安装的作用是直接在命令行中使用
npm install -g webpack
//安装到项目目录,使用webpack的功能
npm install --save-dev webpack
完整的基础案例
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const webpack = require('webpack'); // 用于访问内置插件
const path = require('path');
const config = {
entry: './path/to/my/entry/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'my-first-webpack.bundle.js'
},
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' }
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
]
};
module.exports = config;