從頭整理webpack搭建流程html
entry
:入口配置output
:輸出配置module
:文件解析模塊配置plugin
:插件配置build/
:存放webpack構建配置文件src/
:項目開發目錄webpack
public/
:公共靜態文件script/
:腳本文件style/
:樣式文件view/
:頁面文件開發依賴web
babel-core
babel-loader
webpack
:這裏使用webpack3
版本在build/
文件夾下面建立webpack.dev.js
,代碼以下:npm
const path = require('path') module.exports = { entry: path.join(__dirname, '../src/script/index.js'), output: { path: path.join(__dirname, '../dist'), filename: 'js/[name].js' }, module: { loaders: [{ test: /\.js$/, use: 'babel-loader' }] } }
src/script/
文件夾下面建立index.js
,任意寫幾行代碼以便測試src/view/
文件夾下面建立index.html
,引入上面的index.js
文件npm init -y
或者yarn init -y
建立package.json
文件,安裝開發依賴在package.json
文件中添加scripts
屬性json
"scripts": { "dev": "rm -rf dist & webpack --config build/webpack.dev.js" }
打開終端執行npm run dev
命令babel
引入html-webpack-plugin
插件測試
const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') module.export = { entry: path.join(__dirname, '../src/script/index.js'), output: { path: path.join(__dirname, '../dist'), filename: 'js/[name].js' }, module: { loaders: [{ test: /\.js$/, use: 'babel-loader' }] }, plugins: [ new HtmlWebpackPlugin({ // 打包生成html文件名,該文件被放置在輸出目錄中 filename: 'index.html', // 模板文件,以該文件生成打包後的html文件 template: path.join(__dirname, '../src/view/index.html') }) ] }