一、打開命令行,使用npm包管理工具建立項目,執行npm init 。
二、執行 npm webpack —save-dev。
三、建立js文件,而且require該js文件,
四、執行打包:webpack hello.js hello.bundle.js;
webpack會自動建立hello.bundle.js;
下面是一個可用的webpack.config.js的模版:
var webpack = require('webpack');//加載webpack依賴包; var path = require('path'); module.exports = { //文件入口並添加熱加載; entry: ['webpack/hot/dev-server',path.resolve(_dirname,'./development/main.js')], output: { path: path.resolve(_dirname,'./build'), filename: 'bundle.js' //輸出文件 }, module: { loaders: [ { test: /\.js?$/, exclude: /(node_modules | bower_components)/, loader: 'babel', query: { presets: ['es2015','react'] } //將react編譯成js }, {test: /\.less$/,loader: 'style-loader!css-loader!postcss-loader!less-loader'},//打包css {test: /\.scss$/,loader: 'style!css!sass?sourceMap'}, //編譯sass文件 {test: /\.(png | jpg)$/,loader: 'url-loader?limit=8192'} //對圖片進行打包; ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ], resolve: { //自動擴展名文件後綴名 extensions: ['','.js','.json','.scss','.ts'] } }