[TOC]
entry
1 2 3 module .exports={ entry:'./path/to/my/entry/file.js' };
默认值是./src/index.js;入口起点
output
1 2 3 4 5 6 7 8 const path = require ('path' );module .exports = { entry: './path/to/my/entry/file.js' , output: { path: path.resolve(__dirname, 'dist' ), filename: 'my-first-webpack.bundle.js' } };
path定义导入的模块,filename指bunble生成的名件名称及地址。输出文件默认放入./dist
文件夹中
loader
loader 能够import
任何类型的模块,例如css
。
1 2 3 4 5 6 7 8 9 10 11 12 const path = require ('path' );module .exports = { output: { filename: 'my-first-webpack.bundle.js' }, module : { rules: [ { test : /\.txt$/ , use : 'raw-loader' } ] } };
“嘿,webpack 编译器,当你碰到「在 require()/import 语句中被解析为 ‘.txt’ 的路径」时,在你对它打包之前,先 >使用 raw-loader 转换一下。”
plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 const HtmlWebpackPlugin = require ('html-webpack-plugin' ); const webpack = require ('webpack' ); module .exports = { module : { rules: [ { test : /\.txt$/ , use : 'raw-loader' } ] }, plugins: [ new HtmlWebpackPlugin({template : './src/index.html' }) ] };
该插件为应用程序生成html文件,并注入所有生成的bundle。
以加载images图像 为例
1 2 3 4 5 6 7 8 9 10 11 12 13 module .exports = { .... module : { rules: [ { test: /\.(png|svg|jpg|gif)$/ , use: [ 'file-loader' } ] } }
src/icon.jpg
src/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import _ from 'lodash' ;import './style.css' ;import Icon from './icon.png' ;function component ( ) { var element = document .createElement('div' ); element.innerHTML = _.join(['Hello' , 'webpack' ], ' ' ); element.classList.add('hello' ); var myIcon = new Image(); myIcon.src = Icon; element.appendChild(myIcon);
1 2 3 .hello{ background: url('./icon.png' ); }
其他加载,css,fonts,data。
以下插件/操作均为方便管理dist文件夹,不需要手动更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const path = require ('path' ); module .exports = { - entry: './src/index.js' , + entry: { + app: './src/index.js' , + print: './src/print.js' + }, output: { - filename: 'bundle.js' , + filename: '[name].bundle.js' , path: path.resolve(__dirname, 'dist' ) } };
npm install --save-dev html-webpack-plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const path = require ('path' ); + const HtmlWebpackPlugin = require ('html-webpack-plugin' ); module .exports = { entry: { app: './src/index.js' , print: './src/print.js' }, + plugins: [ + new HtmlWebpackPlugin({ + title: '管理输出' + }) + ], output: { filename: '[name].bundle.js' , path: path.resolve(__dirname, 'dist' ) } };
执行npm run build
该插件会默认生成index.html
文件,覆盖/更改原index.html
文件
npm install --save-dev clean-webpack-plugin
1 2 3 4 5 6 + const CleanWebpackPlugin = require ('clean-webpack-plugin' ); .... plugins: [ + new CleanWebpackPlugin(),
作用为清理dist文件夹中之前生成的不需要的旧文件。这一节管理输出一直有报错,未能正常显示页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 Uncaught ReferenceError : ducument is not defined at app.bundle.js:9 at Module.<anonymous> (app.bundle.js:9 ) at r (app.bundle.js:1 ) at app.bundle.js:1 at app.bundle.js:1 (anonymous) @ app.bundle.js:9 (anonymous) @ app.bundle.js:9 r @ app.bundle.js:1 (anonymous) @ app.bundle.js:1 (anonymous) @ app.bundle.js:1 Entrypoint undefined = index.html
按照官网操作来的,这报错真的不明白,之后再看看怎么解决唔,看看github相关插件的issue
吐血!document拼写错误,见devtool: 'inline-source-map',
可跟踪错误至源文件